Compare commits

...

17 Commits

Author SHA1 Message Date
klmp200 b781ae9f53
Add specific chat data and pass chaos to an entire day 2018-07-27 16:50:13 +02:00
klmp200 31c4a24de0
Limit Chaos to once an hour 2018-07-26 22:53:09 +02:00
klmp200 368ac57b04
Bring Chaos 2018-07-26 22:36:04 +02:00
klmp200 22060613ad
Merge branch 'features' 2018-07-26 22:23:00 +02:00
klmp200 ead73b25e1
Twitters Trends in Franceç 2018-07-26 22:22:34 +02:00
Antoine Bartuccio ddea4620ae Merge branch 'features' of ALFRED/ALFRED into master 2018-07-25 20:55:10 +00:00
klmp200 68f6e1ffa0
Fix empty gender 2018-07-25 01:30:13 +02:00
klmp200 747e58099e
Removed @ from genders 2018-07-25 01:05:54 +02:00
klmp200 54517b8e99
Remove \ when cleaning Genders 2018-07-25 00:19:45 +02:00
Antoine Bartuccio 5ba31d37cb Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 19:03:38 +00:00
Antoine Bartuccio 4a1a904d65 Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 18:31:26 +00:00
Antoine Bartuccio abaf36c6e3 Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 18:11:59 +00:00
Antoine Bartuccio 7ec9a97b74 Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 15:53:21 +00:00
Antoine Bartuccio 62188a005d Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 15:32:41 +00:00
Antoine Bartuccio efa9b54ffa Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 13:25:25 +00:00
Antoine Bartuccio 04ad6d33e7 Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 10:58:50 +00:00
Antoine Bartuccio 99a6cd2a14 Merge branch 'features' of ALFRED/ALFRED into master 2018-07-24 01:08:06 +00:00
6 changed files with 150 additions and 11 deletions

3
.gitignore vendored
View File

@ -16,4 +16,5 @@
settings_custom.json
history.json
users.json
users.json
chat_data.json

View File

@ -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-27 16:13:32
*/
package main
@ -27,6 +27,8 @@ func main() {
"/gender": commands.Gender,
"/roll": commands.Dice,
"/trump": commands.LastTrumpTweet,
"/trends": commands.TwitterTrends,
"/chaos": commands.TwitterSJW,
}
if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil {
@ -38,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{

View File

@ -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 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+" ».")
}
@ -52,13 +56,12 @@ 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
}

View File

@ -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-27 16:49:59
*/
package commands
@ -15,6 +15,7 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
"strconv"
"strings"
"time"
)
var client *twitter.Client
@ -39,16 +40,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 +69,46 @@ 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)
}
func TwitterSJW(m *tb.Message) {
testOrInitTwitter()
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
}
}
shared.ChatData.Set(m.Chat.ID, "last chaos use", time.Now())
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)
}
}

View File

@ -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"
}

82
shared/chat.go Normal file
View File

@ -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)
}