From 7030d6d595766a111a14d63a871ebb5c300e105a Mon Sep 17 00:00:00 2001 From: klmp200 Date: Tue, 24 Jul 2018 15:24:09 +0200 Subject: [PATCH] Add Users infos and gender registration --- .gitignore | 3 +- alfred.go | 16 +++++---- commands/gender.go | 30 +++++++++++++++++ settings.json | 3 +- shared/users.go | 82 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+), 8 deletions(-) create mode 100644 commands/gender.go create mode 100644 shared/users.go diff --git a/.gitignore b/.gitignore index b868d05..ee19125 100644 --- a/.gitignore +++ b/.gitignore @@ -15,4 +15,5 @@ .glide/ settings_custom.json -history.json \ No newline at end of file +history.json +users.json \ No newline at end of file diff --git a/alfred.go b/alfred.go index cbe1889..46337e8 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-24 12:44:38 +* @Last Modified time: 2018-07-24 15:21:53 */ package main @@ -18,11 +18,13 @@ import ( func main() { registered_commands := map[string]func(*tb.Message){ - tb.OnText: commands.OnText, - "/hello": commands.Hello, - "/sponge": commands.Sponge, - "/git": commands.Git, - "/framapad": commands.Framapad, + tb.OnText: commands.OnText, + "/hello": commands.Hello, + "/sponge": commands.Sponge, + "/git": commands.Git, + "/framapad": commands.Framapad, + "/setgender": commands.SetGender, + "/gender": commands.Gender, } if err := settings.LoadSettings("settings.json", "settings_custom.json"); err != nil { @@ -32,6 +34,8 @@ func main() { log.Println("Initialize history") shared.InitHistory(int(settings.Settings["history size"].(float64)), settings.Settings["history file"].(string)) + log.Println("Initialize users infos") + shared.InitUsers(settings.Settings["users file"].(string)) log.Println("Bot initialisation") b, err := tb.NewBot(tb.Settings{ diff --git a/commands/gender.go b/commands/gender.go new file mode 100644 index 0000000..356ab7a --- /dev/null +++ b/commands/gender.go @@ -0,0 +1,30 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 14:55:33 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 15:10:37 + */ + +package commands + +import ( + "../shared" + tb "gopkg.in/tucnak/telebot.v2" + "strings" +) + +func SetGender(m *tb.Message) { + split := strings.Split(m.Text, " ") + data := strings.Join(split[1:], " ") + shared.Users.Set(m.Sender.ID, "gender", data) + shared.Bot.Send(m.Chat, "Votre genre est enregistré, je vous considère maintenant comme « "+data+" ».") +} + +func Gender(m *tb.Message) { + data, exists := shared.Users.Get(m.Sender.ID, "gender") + if !exists { + shared.Bot.Send(m.Chat, "Vous n'avez pas enregistré votre genre, je ne voudrais pas l'assumer.") + } else { + shared.Bot.Send(m.Chat, data) + } +} diff --git a/settings.json b/settings.json index 587f67d..a43accc 100644 --- a/settings.json +++ b/settings.json @@ -1,5 +1,6 @@ { "token": "INSERT TOKEN HERE", "history size": 10, - "history file": "history.json" + "history file": "history.json", + "users file": "users.json" } \ No newline at end of file diff --git a/shared/users.go b/shared/users.go new file mode 100644 index 0000000..ed9573c --- /dev/null +++ b/shared/users.go @@ -0,0 +1,82 @@ +/* +* @Author: Bartuccio Antoine +* @Date: 2018-07-24 14:41:03 +* @Last Modified by: klmp200 +* @Last Modified time: 2018-07-24 15:23:06 + */ + +package shared + +import ( + "encoding/json" + "io/ioutil" + "sync" +) + +type users struct { + mutex sync.Mutex + data map[int]map[string]string +} + +type usersFile struct { + mutex sync.Mutex + path string +} + +var Users users +var uf usersFile + +func InitUsers(users_file_path string) { + uf = usersFile{} + uf.path = users_file_path + Users = users{} + Users.mutex.Lock() + defer Users.mutex.Unlock() + Users.data = make(map[int]map[string]string) + uf.read() +} + +// Get an info about a given user +func (u users) Get(id int, key string) (string, bool) { + u.mutex.Lock() + defer u.mutex.Unlock() + user, exists := u.data[id] + if !exists { + return "", false + } + if _, exists = user[key]; !exists { + return "", false + } + return user[key], true +} + +// Add an info about a given user +func (u users) Set(id int, key, data string) { + u.mutex.Lock() + defer u.mutex.Unlock() + if _, exists := u.data[id]; !exists { + u.data[id] = make(map[string]string) + } + u.data[id][key] = data + go uf.write() +} + +func (u usersFile) read() { + u.mutex.Lock() + defer u.mutex.Unlock() + data, err := ioutil.ReadFile(u.path) + if err != nil { + // File doesn't exist, skip import + return + } + json.Unmarshal(data, &Users.data) +} + +func (u usersFile) write() { + u.mutex.Lock() + defer u.mutex.Unlock() + Users.mutex.Lock() + defer Users.mutex.Unlock() + data, _ := json.Marshal(Users.data) + ioutil.WriteFile(u.path, data, 0770) +}