mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-01-18 10:36:44 +01:00
Now use username to store user infos to go beyond Telegram Bot API limitation
This commit is contained in:
parent
1edf6ef81a
commit
3128e836ea
@ -2,7 +2,7 @@
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-24 14:55:33
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-24 17:26:57
|
||||
* @Last Modified time: 2018-07-24 17:50:19
|
||||
*/
|
||||
|
||||
package commands
|
||||
@ -14,18 +14,22 @@ import (
|
||||
)
|
||||
|
||||
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")
|
||||
return
|
||||
}
|
||||
split := cleanGender(strings.Split(m.Text, " ")[1:])
|
||||
if len(split) == 0 {
|
||||
shared.Bot.Send(m.Chat, "Désolé, mais je n'ai pas compris.")
|
||||
return
|
||||
}
|
||||
data := strings.Join(split, " ")
|
||||
shared.Users.Set(m.Sender.ID, "gender", data)
|
||||
shared.Users.Set(m.Sender.Username, "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")
|
||||
data, exists := shared.Users.Get(m.Sender.Username, "gender")
|
||||
if !exists {
|
||||
shared.Bot.Send(m.Chat, "Vous n'avez pas enregistré votre genre, je ne voudrais pas l'assumer.")
|
||||
} else {
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @Author: Bartuccio Antoine
|
||||
* @Date: 2018-07-24 14:41:03
|
||||
* @Last Modified by: klmp200
|
||||
* @Last Modified time: 2018-07-24 15:23:06
|
||||
* @Last Modified time: 2018-07-24 17:49:51
|
||||
*/
|
||||
|
||||
package shared
|
||||
@ -15,7 +15,7 @@ import (
|
||||
|
||||
type users struct {
|
||||
mutex sync.Mutex
|
||||
data map[int]map[string]string
|
||||
data map[string]map[string]string
|
||||
}
|
||||
|
||||
type usersFile struct {
|
||||
@ -32,15 +32,15 @@ func InitUsers(users_file_path string) {
|
||||
Users = users{}
|
||||
Users.mutex.Lock()
|
||||
defer Users.mutex.Unlock()
|
||||
Users.data = make(map[int]map[string]string)
|
||||
Users.data = make(map[string]map[string]string)
|
||||
uf.read()
|
||||
}
|
||||
|
||||
// Get an info about a given user
|
||||
func (u users) Get(id int, key string) (string, bool) {
|
||||
func (u users) Get(username string, key string) (string, bool) {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
user, exists := u.data[id]
|
||||
user, exists := u.data[username]
|
||||
if !exists {
|
||||
return "", false
|
||||
}
|
||||
@ -51,13 +51,13 @@ func (u users) Get(id int, key string) (string, bool) {
|
||||
}
|
||||
|
||||
// Add an info about a given user
|
||||
func (u users) Set(id int, key, data string) {
|
||||
func (u users) Set(username string, key, data string) {
|
||||
u.mutex.Lock()
|
||||
defer u.mutex.Unlock()
|
||||
if _, exists := u.data[id]; !exists {
|
||||
u.data[id] = make(map[string]string)
|
||||
if _, exists := u.data[username]; !exists {
|
||||
u.data[username] = make(map[string]string)
|
||||
}
|
||||
u.data[id][key] = data
|
||||
u.data[username][key] = data
|
||||
go uf.write()
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user