Now use username to store user infos to go beyond Telegram Bot API limitation

This commit is contained in:
klmp200 2018-07-24 17:52:04 +02:00
parent 1edf6ef81a
commit 3128e836ea
No known key found for this signature in database
GPG key ID: E7245548C53F904B
2 changed files with 16 additions and 12 deletions

View file

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