This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
botUserHandler.go
169 lines (144 loc) · 3.59 KB
/
botUserHandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package wabot
import (
"encoding/json"
"io/ioutil"
"github.com/Rhymen/go-whatsapp"
)
var users = BotUserList{}
// BotUserList saves the BotUser-Array - easy to save&load
type BotUserList struct {
BotUsers []*BotUser
}
// BotUser contains the contact and his personal settings
type BotUser struct {
Contact whatsapp.Contact
Nickname string
Settings interface{}
}
var standardSettings interface{}
// addUser adds a new member to the group and prepares a Settings struct for him
func addUser(user whatsapp.Contact) {
users.BotUsers = append(users.BotUsers, &BotUser{Contact: user, Nickname: "", Settings: standardSettings})
}
// CreateNewSettingsOption adds the interface to the general BotUser struct
func CreateNewSettingsOption(settings interface{}) {
standardSettings = settings
}
// GetUserNickname returns a users nickname
func GetUserNickname(jid string) string {
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
return u.Nickname
}
}
return ""
}
// SetUserNickname sets a users nickname
func SetUserNickname(jid string, nickname string) {
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
u.Nickname = nickname
return
}
}
// User isn't registered yet. Do it now!
AddUserByJid(jid)
// Return the settings
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
u.Nickname = nickname
}
}
}
// IsUserRegistered checks if the given jid exists in the array
func IsUserRegistered(jid string) bool {
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
return true
}
}
return false
}
// AddUserByJid - AddUser alternative
func AddUserByJid(jid string) {
if !IsUserRegistered(jid) {
for _, c := range contacList {
if c.Jid == jid {
users.BotUsers = append(users.BotUsers, &BotUser{Contact: c, Nickname: "", Settings: standardSettings})
break
}
}
}
}
// ChangeUserSettings a users settings
func ChangeUserSettings(jid string, settings interface{}) {
// Return the settings
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
u.Settings = settings
}
}
// User isn't registered yet. Do it now!
AddUserByJid(jid)
// Return the settings
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
u.Settings = settings
}
}
}
// GetUserSettings returns the settings of a specific user
func GetUserSettings(jid string) interface{} {
// Return the settings
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
return u.Settings
}
}
// User isn't registered yet. Do it now!
AddUserByJid(jid)
// Return the settings
for _, u := range users.BotUsers {
if u.Contact.Jid == jid {
return u.Settings
}
}
return nil
}
// GetUserIndex returns a Users index within the Array
func GetUserIndex(message whatsapp.TextMessage) int {
for i, u := range users.BotUsers {
if u.Contact.Jid == MessageToJid(message) {
return i
}
}
return -1
}
// SaveUsersToDisk saves the BotUser-Slice
func SaveUsersToDisk() bool {
if len(users.BotUsers) > 0 {
usersJSON, _ := json.Marshal(users)
usersJSON = encryptData(usersJSON)
ioutil.WriteFile(usersFile, usersJSON, 0600)
return true
}
return false
}
// GetSaveData returns the stored savedata
// -> bool is false if there was no data
func GetSaveData() (BotUserList, bool) {
savedUsers := BotUserList{}
savedData, err := ioutil.ReadFile(usersFile)
if err == nil {
savedData = decryptData(savedData)
err = json.Unmarshal(savedData, &savedUsers)
if err == nil {
return savedUsers, true
}
}
return BotUserList{}, false
}
// UseSaveData loads the given data
func UseSaveData(savedata BotUserList) {
users.BotUsers = savedata.BotUsers
}