From 54517b8e994e6438245ee46497adc45e7bff1833 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 25 Jul 2018 00:19:45 +0200 Subject: [PATCH 1/7] Remove \ when cleaning Genders --- commands/gender.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/gender.go b/commands/gender.go index b126dbf..238ddae 100644 --- a/commands/gender.go +++ b/commands/gender.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-24 14:55:33 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 20:29:36 +* @Last Modified time: 2018-07-25 00:19:03 */ package commands @@ -52,6 +52,7 @@ func Gender(m *tb.Message) { func cleanGender(slice []string) []string { for i := range slice { + slice[i] = strings.Replace(slice[i], "\\", "", -1) clean := false for !clean { clean = true From 747e58099ed6eb6efcf861a60c05bf3078c08c51 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 25 Jul 2018 01:05:54 +0200 Subject: [PATCH 2/7] Removed @ from genders --- commands/gender.go | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/commands/gender.go b/commands/gender.go index 238ddae..1861e87 100644 --- a/commands/gender.go +++ b/commands/gender.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-24 14:55:33 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-25 00:19:03 +* @Last Modified time: 2018-07-25 01:05:08 */ package commands @@ -53,13 +53,11 @@ func Gender(m *tb.Message) { func cleanGender(slice []string) []string { for i := range slice { slice[i] = strings.Replace(slice[i], "\\", "", -1) + slice[i] = strings.Replace(slice[i], "@", "", -1) clean := false for !clean { clean = true - if strings.HasPrefix(slice[i], "@") { - slice[i] = strings.Replace(slice[i], "@", "", 1) - clean = false - } else if strings.HasPrefix(slice[i], "/") { + if strings.HasPrefix(slice[i], "/") { slice[i] = strings.Replace(slice[i], "/", "", 1) clean = false } From 68f6e1ffa0418baac1704e9a0cd5563d210cd8a4 Mon Sep 17 00:00:00 2001 From: klmp200 Date: Wed, 25 Jul 2018 01:30:13 +0200 Subject: [PATCH 3/7] Fix empty gender --- commands/gender.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/commands/gender.go b/commands/gender.go index 1861e87..26d1d80 100644 --- a/commands/gender.go +++ b/commands/gender.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-24 14:55:33 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-25 01:05:08 +* @Last Modified time: 2018-07-25 01:29:53 */ package commands @@ -24,6 +24,10 @@ func SetGender(m *tb.Message) { return } data := strings.Join(split, " ") + if data == "" { + shared.Bot.Send(m.Chat, "Attention, votre genre est vide. Ce n'est pas enregistrable.") + return + } shared.Users.Set(m.Sender.Username, "gender", data) shared.Bot.Send(m.Chat, "Votre genre est enregistré, je vous considère maintenant comme « "+data+" ».") } From ead73b25e1c8625bbb25d64f73d34f2c9dc1915c Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 26 Jul 2018 22:22:34 +0200 Subject: [PATCH 4/7] =?UTF-8?q?Twitters=20Trends=20in=20France=C3=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- alfred.go | 3 ++- commands/twitter.go | 24 +++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/alfred.go b/alfred.go index 1bd01ff..4bc15d3 100644 --- a/alfred.go +++ b/alfred.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-25 19:27:21 +* @Last Modified time: 2018-07-26 18:59:19 */ package main @@ -27,6 +27,7 @@ func main() { "/gender": commands.Gender, "/roll": commands.Dice, "/trump": commands.LastTrumpTweet, + "/trends": commands.TwitterTrends, } if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil { diff --git a/commands/twitter.go b/commands/twitter.go index 0a2353a..a893eb4 100644 --- a/commands/twitter.go +++ b/commands/twitter.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-25 18:51:38 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-25 22:51:50 +* @Last Modified time: 2018-07-26 22:21:29 */ package commands @@ -39,16 +39,20 @@ func testOrInitTwitter() { } } +func twitterCommunicationError(m *tb.Message) { + shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.") +} + func LastTrumpTweet(m *tb.Message) { testOrInitTwitter() user, _, err := client.Users.Show(&twitter.UserShowParams{ScreenName: "realDonaldTrump"}) if err != nil { - shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.") + twitterCommunicationError(m) return } timeline, _, err := client.Timelines.UserTimeline(&twitter.UserTimelineParams{ScreenName: "realDonaldTrump"}) if err != nil { - shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.") + twitterCommunicationError(m) return } response := []string{ @@ -64,3 +68,17 @@ func LastTrumpTweet(m *tb.Message) { } shared.Bot.Send(m.Chat, strings.Join(response, " ")) } + +func TwitterTrends(m *tb.Message) { + testOrInitTwitter() + trends, _, err := client.Trends.Place(int64(615702), nil) + if err != nil { + twitterCommunicationError(m) + return + } + message := "Voici les dernières tendances en France" + for _, trend := range trends[0].Trends { + message += "\n" + trend.Name + } + shared.Bot.Send(m.Chat, message) +} From 368ac57b04107236786d6fa4bd4cebac4da7ef0c Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 26 Jul 2018 22:36:04 +0200 Subject: [PATCH 5/7] Bring Chaos --- alfred.go | 3 ++- commands/twitter.go | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/alfred.go b/alfred.go index 4bc15d3..e765490 100644 --- a/alfred.go +++ b/alfred.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-26 18:59:19 +* @Last Modified time: 2018-07-26 22:32:59 */ package main @@ -28,6 +28,7 @@ func main() { "/roll": commands.Dice, "/trump": commands.LastTrumpTweet, "/trends": commands.TwitterTrends, + "/chaos": commands.TwitterSJW, } if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil { diff --git a/commands/twitter.go b/commands/twitter.go index a893eb4..00b1903 100644 --- a/commands/twitter.go +++ b/commands/twitter.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-25 18:51:38 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-26 22:21:29 +* @Last Modified time: 2018-07-26 22:35:14 */ package commands @@ -82,3 +82,17 @@ func TwitterTrends(m *tb.Message) { } shared.Bot.Send(m.Chat, message) } + +func TwitterSJW(m *tb.Message) { + testOrInitTwitter() + tweets, _, err := client.Search.Tweets(&twitter.SearchTweetParams{ + Query: "#SJW", + }) + if err != nil { + twitterCommunicationError(m) + return + } + for _, tweet := range tweets.Statuses { + shared.Bot.Send(m.Chat, tweet.Text) + } +} From 31c4a24de0d4380212dedd2d244f6373a62a55fe Mon Sep 17 00:00:00 2001 From: klmp200 Date: Thu, 26 Jul 2018 22:53:09 +0200 Subject: [PATCH 6/7] Limit Chaos to once an hour --- commands/twitter.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/commands/twitter.go b/commands/twitter.go index 00b1903..cac9b4f 100644 --- a/commands/twitter.go +++ b/commands/twitter.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-25 18:51:38 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-26 22:35:14 +* @Last Modified time: 2018-07-26 22:52:51 */ package commands @@ -15,9 +15,17 @@ import ( tb "gopkg.in/tucnak/telebot.v2" "strconv" "strings" + "sync" + "time" ) +type sjw struct { + usable bool + mutex sync.Mutex +} + var client *twitter.Client +var sjw_usable sjw func initTwitter() { config := oauth1.NewConfig( @@ -31,6 +39,7 @@ func initTwitter() { http_client := config.Client(oauth1.NoContext, token) client = twitter.NewClient(http_client) + sjw_usable = sjw{usable: true} } func testOrInitTwitter() { @@ -43,6 +52,13 @@ func twitterCommunicationError(m *tb.Message) { shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.") } +func unlockSjw() { + time.Sleep(time.Hour) + sjw_usable.mutex.Lock() + sjw_usable.usable = true + sjw_usable.mutex.Unlock() +} + func LastTrumpTweet(m *tb.Message) { testOrInitTwitter() user, _, err := client.Users.Show(&twitter.UserShowParams{ScreenName: "realDonaldTrump"}) @@ -85,6 +101,13 @@ func TwitterTrends(m *tb.Message) { func TwitterSJW(m *tb.Message) { testOrInitTwitter() + sjw_usable.mutex.Lock() + defer sjw_usable.mutex.Unlock() + if !sjw_usable.usable { + shared.Bot.Send(m.Chat, "Arioch ne répondra pas à votre appel.") + return + } + sjw_usable.usable = false tweets, _, err := client.Search.Tweets(&twitter.SearchTweetParams{ Query: "#SJW", }) @@ -95,4 +118,5 @@ func TwitterSJW(m *tb.Message) { for _, tweet := range tweets.Statuses { shared.Bot.Send(m.Chat, tweet.Text) } + go unlockSjw() } From b781ae9f53bac1ab615302d03c91f300c334f5cb Mon Sep 17 00:00:00 2001 From: klmp200 Date: Fri, 27 Jul 2018 16:50:13 +0200 Subject: [PATCH 7/7] Add specific chat data and pass chaos to an entire day --- .gitignore | 3 +- alfred.go | 4 ++- commands/twitter.go | 38 +++++++++------------ settings.json | 3 +- shared/chat.go | 82 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 26 deletions(-) create mode 100644 shared/chat.go diff --git a/.gitignore b/.gitignore index ee19125..eb5b34a 100644 --- a/.gitignore +++ b/.gitignore @@ -16,4 +16,5 @@ settings_custom.json history.json -users.json \ No newline at end of file +users.json +chat_data.json \ No newline at end of file diff --git a/alfred.go b/alfred.go index e765490..b8d4956 100644 --- a/alfred.go +++ b/alfred.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-26 22:32:59 +* @Last Modified time: 2018-07-27 16:13:32 */ package main @@ -40,6 +40,8 @@ func main() { settings.Settings["history file"].(string)) log.Println("Initialize users infos") shared.InitUsers(settings.Settings["users file"].(string)) + log.Println("Initialize chat data") + shared.InitChatData(settings.Settings["chat data file"].(string)) log.Println("Bot initialisation") b, err := tb.NewBot(tb.Settings{ diff --git a/commands/twitter.go b/commands/twitter.go index cac9b4f..42e9ad8 100644 --- a/commands/twitter.go +++ b/commands/twitter.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-25 18:51:38 * @Last Modified by: klmp200 -* @Last Modified time: 2018-07-26 22:52:51 +* @Last Modified time: 2018-07-27 16:49:59 */ package commands @@ -15,17 +15,10 @@ import ( tb "gopkg.in/tucnak/telebot.v2" "strconv" "strings" - "sync" "time" ) -type sjw struct { - usable bool - mutex sync.Mutex -} - var client *twitter.Client -var sjw_usable sjw func initTwitter() { config := oauth1.NewConfig( @@ -39,7 +32,6 @@ func initTwitter() { http_client := config.Client(oauth1.NoContext, token) client = twitter.NewClient(http_client) - sjw_usable = sjw{usable: true} } func testOrInitTwitter() { @@ -52,13 +44,6 @@ func twitterCommunicationError(m *tb.Message) { shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.") } -func unlockSjw() { - time.Sleep(time.Hour) - sjw_usable.mutex.Lock() - sjw_usable.usable = true - sjw_usable.mutex.Unlock() -} - func LastTrumpTweet(m *tb.Message) { testOrInitTwitter() user, _, err := client.Users.Show(&twitter.UserShowParams{ScreenName: "realDonaldTrump"}) @@ -101,13 +86,21 @@ func TwitterTrends(m *tb.Message) { func TwitterSJW(m *tb.Message) { testOrInitTwitter() - sjw_usable.mutex.Lock() - defer sjw_usable.mutex.Unlock() - if !sjw_usable.usable { - shared.Bot.Send(m.Chat, "Arioch ne répondra pas à votre appel.") - return + last_use, exists := shared.ChatData.Get(m.Chat.ID, "last chaos use") + if exists { + var date time.Time + if _, is_string := last_use.(string); is_string { + date, _ = time.Parse(time.RFC3339, last_use.(string)) + } else { + date = last_use.(time.Time) + } + if time.Now().Before(date.Add(time.Hour * 24)) { + shared.Bot.Send(m.Chat, "Arioch ne répondra pas à votre appel.") + return + } } - sjw_usable.usable = false + shared.ChatData.Set(m.Chat.ID, "last chaos use", time.Now()) + tweets, _, err := client.Search.Tweets(&twitter.SearchTweetParams{ Query: "#SJW", }) @@ -118,5 +111,4 @@ func TwitterSJW(m *tb.Message) { for _, tweet := range tweets.Statuses { shared.Bot.Send(m.Chat, tweet.Text) } - go unlockSjw() } diff --git a/settings.json b/settings.json index df3e4a7..de9860a 100644 --- a/settings.json +++ b/settings.json @@ -6,5 +6,6 @@ "twitter consumer secret": "INSERT TOKEN HERE", "history size": 10, "history file": "history.json", - "users file": "users.json" + "users file": "users.json", + "chat data file": "chat_data.json" } \ No newline at end of file diff --git a/shared/chat.go b/shared/chat.go new file mode 100644 index 0000000..6158a28 --- /dev/null +++ b/shared/chat.go @@ -0,0 +1,82 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-27 15:37:59 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-27 16:06:51 + */ + +package shared + +import ( + "encoding/json" + "io/ioutil" + "sync" +) + +// General purpose chat info storage +type chatData struct { + mutex sync.Mutex + data map[int64]map[string]interface{} +} + +type chatDataFile struct { + mutex sync.Mutex + path string +} + +var ChatData chatData +var cdf chatDataFile + +// Init chat data meant to store infos about a chat. +func InitChatData(path string) { + cdf = chatDataFile{path: path} + ChatData = chatData{data: make(map[int64]map[string]interface{})} + ChatData.mutex.Lock() + defer ChatData.mutex.Unlock() + cdf.read() + +} + +func (c chatData) Set(chat int64, key string, data interface{}) { + c.mutex.Lock() + defer c.mutex.Unlock() + if _, exists := c.data[chat]; !exists { + c.data[chat] = make(map[string]interface{}) + } + c.data[chat][key] = data + go cdf.write() +} + +func (c chatData) Get(chat int64, key string) (interface{}, bool) { + c.mutex.Lock() + defer c.mutex.Unlock() + m, exists := c.data[chat] + if !exists { + return nil, false + } + data, ok := m[key] + if !ok { + return nil, false + } + return data, true +} + +func (c chatDataFile) read() { + c.mutex.Lock() + defer c.mutex.Unlock() + data, err := ioutil.ReadFile(c.path) + if err != nil { + // File doesn't exist, skip import + return + } + json.Unmarshal(data, &ChatData.data) +} + +func (c chatDataFile) write() { + c.mutex.Lock() + defer c.mutex.Unlock() + ChatData.mutex.Lock() + defer ChatData.mutex.Unlock() + data, _ := json.Marshal(ChatData.data) + ioutil.WriteFile(c.path, data, 0770) +}