diff --git a/alfred.go b/alfred.go index bfead6d..476ca53 100644 --- a/alfred.go +++ b/alfred.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-23 15:24:22 * @Last Modified by: Bartuccio Antoine -* @Last Modified time: 2019-01-02 22:58:03 +* @Last Modified time: 2019-01-04 10:52:37 */ package main @@ -11,9 +11,10 @@ import ( "log" "time" - "./commands" - "./settings" - "./shared" + "git.klmp200.net/ALFRED/ALFRED/commands" + "git.klmp200.net/ALFRED/ALFRED/settings" + "git.klmp200.net/ALFRED/ALFRED/shared" + tb "gopkg.in/tucnak/telebot.v2" ) diff --git a/commands/apero.go b/commands/apero.go index 5a2a265..22aa788 100644 --- a/commands/apero.go +++ b/commands/apero.go @@ -1,19 +1,22 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-27 19:53:09 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-28 13:17:05 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:36:16 */ package commands import ( - "../shared" + "net/http" + + "git.klmp200.net/ALFRED/ALFRED/shared" + "github.com/PuerkitoBio/goquery" tb "gopkg.in/tucnak/telebot.v2" - "net/http" ) +// AperoTime checks if it's time to drink on an external website func AperoTime(m *tb.Message) { response, err := http.Get("http://estcequecestbientotlapero.fr") if err != nil { @@ -27,13 +30,4 @@ func AperoTime(m *tb.Message) { return } shared.Bot.Send(m.Chat, doc.Find("h2").First().Text()) - // doc.Find("h2").Each(func(i int, s *goquery.Selection) { - // log.Println(s.Text()) - // }) - // node, err := html.Parse(response.Body) - // if err != nil { - // shared.Bot.Send(m.Chat, "La réponse qui m'a été fournit est incohérente.") - // return - // } - // log.Println(node.NextSibling) } diff --git a/commands/dice.go b/commands/dice.go index 5a30a68..9c9a94c 100644 --- a/commands/dice.go +++ b/commands/dice.go @@ -1,20 +1,23 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 20:50:04 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 21:00:59 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:37:39 */ package commands import ( - "../shared" - tb "gopkg.in/tucnak/telebot.v2" "math/rand" "strconv" "strings" + + "git.klmp200.net/ALFRED/ALFRED/shared" + + tb "gopkg.in/tucnak/telebot.v2" ) +// Dice rolls a dice func Dice(m *tb.Message) { split := strings.Split(m.Text, " ") if len(split) < 2 { diff --git a/commands/framapad.go b/commands/framapad.go index 31fc4c2..b0bd996 100644 --- a/commands/framapad.go +++ b/commands/framapad.go @@ -1,17 +1,18 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 12:11:26 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 12:12:58 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:38:07 */ package commands import ( - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" tb "gopkg.in/tucnak/telebot.v2" ) +// Framapad send a link to the ALFRED framapad func Framapad(m *tb.Message) { shared.Bot.Send(m.Chat, "Venez participer à mon développement en posant vos idées ici : https://mensuel.framapad.org/p/ALFRED2LERETOUR.") } diff --git a/commands/gender.go b/commands/gender.go index 26d1d80..66a0b9e 100644 --- a/commands/gender.go +++ b/commands/gender.go @@ -1,18 +1,21 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 14:55:33 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-25 01:29:53 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:38:32 */ package commands import ( - "../shared" - tb "gopkg.in/tucnak/telebot.v2" "strings" + + "git.klmp200.net/ALFRED/ALFRED/shared" + + tb "gopkg.in/tucnak/telebot.v2" ) +// SetGender register your gender on ALFRED func SetGender(m *tb.Message) { if m.Sender.Username == "" { shared.Bot.Send(m.Chat, "Il faut avoir enregistré un username pour pouvoir utiliser cette fonction") diff --git a/commands/git.go b/commands/git.go index 29a9c4a..56c592e 100644 --- a/commands/git.go +++ b/commands/git.go @@ -1,17 +1,19 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 12:07:34 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 12:08:49 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:39:07 */ package commands import ( - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" + tb "gopkg.in/tucnak/telebot.v2" ) +// Git sends a link to the git repo func Git(m *tb.Message) { shared.Bot.Send(m.Chat, "Mon code source est accessible librement à l'adresse https://git.klmp200.net/ALFRED/ALFRED. Venez contribuer :)") } diff --git a/commands/hello.go b/commands/hello.go index bbb0f70..2ed1336 100644 --- a/commands/hello.go +++ b/commands/hello.go @@ -1,17 +1,19 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 12:05:45 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 12:06:39 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:39:24 */ package commands import ( - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" + tb "gopkg.in/tucnak/telebot.v2" ) +// Hello makes ALFRED say hello func Hello(m *tb.Message) { shared.Bot.Send(m.Chat, "Bonjour "+m.Sender.Username) } diff --git a/commands/on_text.go b/commands/on_text.go index c5db4a8..29b3109 100644 --- a/commands/on_text.go +++ b/commands/on_text.go @@ -1,17 +1,19 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 12:09:37 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 12:10:26 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:39:50 */ package commands import ( - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" + tb "gopkg.in/tucnak/telebot.v2" ) +// OnText saves chat history func OnText(m *tb.Message) { shared.History.AddMessage(m.Chat.ID, m.Text) } diff --git a/commands/quote.go b/commands/quote.go index e6e1a59..302f49c 100644 --- a/commands/quote.go +++ b/commands/quote.go @@ -1,8 +1,8 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-11-14 00:15:43 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-11-14 00:31:48 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:40:01 */ package commands @@ -13,7 +13,7 @@ import ( "math/rand" "sync" - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" tb "gopkg.in/tucnak/telebot.v2" ) diff --git a/commands/register_private.go b/commands/register_private.go index a40e16b..4efbb59 100644 --- a/commands/register_private.go +++ b/commands/register_private.go @@ -2,13 +2,14 @@ * @Author: Bartuccio Antoine * @Date: 2019-01-02 22:46:05 * @Last Modified by: Bartuccio Antoine -* @Last Modified time: 2019-01-03 22:00:09 +* @Last Modified time: 2019-01-04 10:40:28 */ package commands import ( - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" + tb "gopkg.in/tucnak/telebot.v2" ) diff --git a/commands/sponge.go b/commands/sponge.go index 009ba5c..ff3a1eb 100644 --- a/commands/sponge.go +++ b/commands/sponge.go @@ -1,16 +1,18 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 11:52:11 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 11:58:42 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:40:38 */ package commands import ( - "../shared" - tb "gopkg.in/tucnak/telebot.v2" "strings" + + "git.klmp200.net/ALFRED/ALFRED/shared" + + tb "gopkg.in/tucnak/telebot.v2" ) func Sponge(m *tb.Message) { diff --git a/commands/subscribe.go b/commands/subscribe.go index 44c378b..0d92e0c 100644 --- a/commands/subscribe.go +++ b/commands/subscribe.go @@ -15,7 +15,8 @@ import ( "strconv" "strings" - "../shared" + "git.klmp200.net/ALFRED/ALFRED/shared" + tb "gopkg.in/tucnak/telebot.v2" ) diff --git a/commands/twitter.go b/commands/twitter.go index 42e9ad8..24acd80 100644 --- a/commands/twitter.go +++ b/commands/twitter.go @@ -1,21 +1,23 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-25 18:51:38 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-27 16:49:59 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:47:25 */ package commands import ( - "../settings" - "../shared" - "github.com/dghubble/go-twitter/twitter" - "github.com/dghubble/oauth1" - tb "gopkg.in/tucnak/telebot.v2" "strconv" "strings" "time" + + "git.klmp200.net/ALFRED/ALFRED/settings" + "git.klmp200.net/ALFRED/ALFRED/shared" + + "github.com/dghubble/go-twitter/twitter" + "github.com/dghubble/oauth1" + tb "gopkg.in/tucnak/telebot.v2" ) var client *twitter.Client @@ -44,6 +46,7 @@ func twitterCommunicationError(m *tb.Message) { shared.Bot.Send(m.Chat, "Désolé, les serveurs de twitter sont injoignables.") } +// LastTrumpTweet sends the last tweet of Donald Trump func LastTrumpTweet(m *tb.Message) { testOrInitTwitter() user, _, err := client.Users.Show(&twitter.UserShowParams{ScreenName: "realDonaldTrump"}) @@ -70,6 +73,7 @@ func LastTrumpTweet(m *tb.Message) { shared.Bot.Send(m.Chat, strings.Join(response, " ")) } +// TwitterTrends sends the french twitter trends func TwitterTrends(m *tb.Message) { testOrInitTwitter() trends, _, err := client.Trends.Place(int64(615702), nil) @@ -84,17 +88,24 @@ func TwitterTrends(m *tb.Message) { shared.Bot.Send(m.Chat, message) } +// TwitterSJW sends lasts messages with #SJW func TwitterSJW(m *tb.Message) { testOrInitTwitter() - last_use, exists := shared.ChatData.Get(m.Chat.ID, "last chaos use") + lastUse, 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) + var date *time.Time + if lastUseConverted, isString := lastUse.(string); isString { + parsedDate, err := time.Parse(time.RFC3339, lastUseConverted) + if err == nil { + date = &parsedDate + } } - if time.Now().Before(date.Add(time.Hour * 24)) { + + if lastUseConverted, isDate := lastUse.(time.Time); isDate { + date = &lastUseConverted + } + + if date != nil || time.Now().Before(date.Add(time.Hour*24)) { shared.Bot.Send(m.Chat, "Arioch ne répondra pas à votre appel.") return } diff --git a/shared/bot.go b/shared/bot.go index 9880987..9486cb4 100644 --- a/shared/bot.go +++ b/shared/bot.go @@ -1,8 +1,8 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 11:56:47 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 11:58:34 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:48:26 */ package shared @@ -11,4 +11,5 @@ import ( tb "gopkg.in/tucnak/telebot.v2" ) +// Bot contains telebot instance var Bot *tb.Bot diff --git a/shared/chat.go b/shared/chat.go index 6158a28..c5b29a1 100644 --- a/shared/chat.go +++ b/shared/chat.go @@ -1,8 +1,8 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-27 15:37:59 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-27 16:06:51 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:49:45 */ package shared @@ -24,10 +24,11 @@ type chatDataFile struct { path string } +// ChatData manage access to stored data about a chat var ChatData chatData var cdf chatDataFile -// Init chat data meant to store infos about a chat. +// InitChatData is meant to store infos about a chat. func InitChatData(path string) { cdf = chatDataFile{path: path} ChatData = chatData{data: make(map[int64]map[string]interface{})} @@ -37,6 +38,7 @@ func InitChatData(path string) { } +// Set stores data about a chat func (c chatData) Set(chat int64, key string, data interface{}) { c.mutex.Lock() defer c.mutex.Unlock() @@ -47,6 +49,7 @@ func (c chatData) Set(chat int64, key string, data interface{}) { go cdf.write() } +// Get retrieves data about a chat func (c chatData) Get(chat int64, key string) (interface{}, bool) { c.mutex.Lock() defer c.mutex.Unlock() diff --git a/shared/history.go b/shared/history.go index 406392b..3be9c46 100644 --- a/shared/history.go +++ b/shared/history.go @@ -1,8 +1,8 @@ /* * @Author: Bartuccio Antoine * @Date: 2018-07-24 01:27:11 -* @Last Modified by: klmp200 -* @Last Modified time: 2018-07-24 12:54:21 +* @Last Modified by: Bartuccio Antoine +* @Last Modified time: 2019-01-04 10:51:32 */ package shared @@ -24,10 +24,11 @@ type historyFile struct { path string } +// History manages acces to chat history var History history var hf historyFile -// Init a chat history of a given size +// InitHistory init a chit history of a given size func InitHistory(size int, history_file_path string) { hf = historyFile{} hf.path = history_file_path @@ -39,14 +40,14 @@ func InitHistory(size int, history_file_path string) { hf.read() } -// Get the number of messages saved in the history +// Size get the number of messages saved in the history func (h history) Size() int { h.mutex.Lock() defer h.mutex.Unlock() return h.size } -// Get a selected message in a chat history +// Message get a selected message in a chat history func (h history) Message(chatID int64, n int) string { h.mutex.Lock() defer h.mutex.Unlock() @@ -57,7 +58,7 @@ func (h history) Message(chatID int64, n int) string { return "" } -// Append a message to a given chat +// AddMessage append a message to a given chat func (h history) AddMessage(chatID int64, m string) { h.mutex.Lock() defer h.mutex.Unlock() @@ -67,7 +68,7 @@ func (h history) AddMessage(chatID int64, m string) { h.append(chatID, m) } -// Get the last message of a given chat +// LastMessage get the last message of a given chat func (h history) LastMessage(chatID int64) string { h.mutex.Lock() defer h.mutex.Unlock() @@ -77,7 +78,7 @@ func (h history) LastMessage(chatID int64) string { return "" } -// Get a copy of a given chat history +// ChatHistory get a copy of a given chat history func (h history) ChatHistory(chatID int64) []string { h.mutex.Lock() defer h.mutex.Unlock() diff --git a/shared/users.go b/shared/users.go index 03eaa34..118cd31 100644 --- a/shared/users.go +++ b/shared/users.go @@ -2,7 +2,7 @@ * @Author: Bartuccio Antoine * @Date: 2018-07-24 14:41:03 * @Last Modified by: Bartuccio Antoine -* @Last Modified time: 2019-01-02 22:37:58 +* @Last Modified time: 2019-01-04 10:52:13 */ package shared @@ -30,6 +30,7 @@ type usersFile struct { var Users users var uf usersFile +// InitUsers inits the User info storage func InitUsers(users_file_path string) { uf = usersFile{} uf.path = users_file_path @@ -54,7 +55,7 @@ func (u users) Get(username string, key string) (string, bool) { return user[key], true } -// Add an info about a given user +// Set add an info about a given user func (u users) Set(username string, key, data string) { u.mutex.Lock() defer u.mutex.Unlock() @@ -65,12 +66,12 @@ func (u users) Set(username string, key, data string) { go uf.write() } -// GetUsernames all usernames stored in settings +// GetUsernames get all usernames stored in settings func (u users) GetUsernames() []string { u.mutex.Lock() defer u.mutex.Unlock() var usernames []string - for username, _ := range u.data { + for username := range u.data { usernames = append(usernames, username) } return usernames