-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.go
196 lines (181 loc) · 6.12 KB
/
settings.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//settings.go
//Contains methods for settings menu
//starMenu is called first which adds the menu to the global
//handleMsgInit is called to get the inital setting choice and will keep being called unless they enter a valid option
//handleMsgSetting is called when they pick an option
//save is called and then delete is called to remove the menu from the global
//To add a new setting edit setting below and then modify createSetting() in user.go
package main
import (
"context"
"strconv"
"strings"
"time"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
"go.mongodb.org/mongo-driver/bson"
)
//Global to keep track of state of settings menu
var menus = make(map[string]*settingsMenu)
//All available settings to check to see if a message is valid
var settings = [6]string{"graph", "hide", "show", "mention", "disable", "delete"}
//Main menu message embed object
var mainMenu = []discord.Embed{
{Title: "Settings for Game Stats Bot (Contact NerdyRedPanda#7480 with any issues)",
Fields: []discord.EmbedField{
{
Name: ":bar_chart:",
Value: "graph - Changes the graph type",
Inline: false,
},
{
Name: ":no_entry_sign:",
Value: "hide - Allows you to hide games from your graph",
Inline: false,
},
{
Name: ":o:",
Value: "show - Allows you to show hidden games",
Inline: false,
},
{
Name: ":bell:",
Value: "mention - Allows other people to get your stats by mentioning you",
Inline: false,
},
{
Name: ":no_entry:",
Value: "disable - Disables bot collecting game data from your account.",
Inline: false,
},
{
Name: ":wastebasket:",
Value: "delete - Deletes all your data.\nWARNING: This cannot be undone",
Inline: false,
},
}},
}
type settingsMenu struct {
inMenu bool
userID string
settingChange string
options []string
timer *time.Timer
}
//Starts the settings menu flow
func startMenu(m *gateway.MessageCreateEvent) []discord.Embed {
s := &settingsMenu{
timer: time.NewTimer(time.Minute * 2),
inMenu: true,
userID: m.Author.ID.String(),
}
//Called to cleanup when either timer runs out or setting is changed
s.timer = time.AfterFunc(time.Minute*2, func() {
delete(menus, s.userID)
})
menus[m.Author.ID.String()] = s
return mainMenu
}
func stopMenu(s *settingsMenu) {
s.timer.Stop()
delete(menus, s.userID)
}
//Generates the main menu
func genSetting(setting string, userID string) (string, []string) {
settingStr := setting + " Settings (Type \"cancel\" to cancel)\n\n"
var options []string
switch setting {
case "graph":
settingStr += "1. Bar\n2. Pie"
options = []string{"bar", "pie"}
case "hide":
var hiddenGames []stat
ctx, close := context.WithTimeout(context.Background(), time.Second*5)
defer close()
cursor, _ := statsCollection.Find(ctx, bson.M{"id": userID, "ignore": false})
cursor.All(ctx, &hiddenGames)
for i := range hiddenGames {
settingStr += strconv.Itoa(i+1) + ". " + hiddenGames[i].Game
options = append(options, hiddenGames[i].Game)
}
case "show":
var shownGames []stat
ctx, close := context.WithTimeout(context.Background(), time.Second*5)
defer close()
cursor, _ := statsCollection.Find(ctx, bson.M{"id": userID, "ignore": true})
cursor.All(ctx, &shownGames)
for i := range shownGames {
settingStr += strconv.Itoa(i+1) + ". " + shownGames[i].Game
options = append(options, shownGames[i].Game)
}
case "mention":
settingStr += "1. Mentions Enabled\n2. Mentions Disabled"
options = []string{"true", "false"}
case "disable":
settingStr += "1. Enable data collection\n2. Disable data collection"
options = []string{"false", "true"}
case "delete":
settingStr += "1. Delete all data, CANNOT BE UNDONE"
options = []string{"delete"}
}
return settingStr, options
}
//Handles the inital message to change settings
func (s *settingsMenu) handleMsgInit(m *gateway.MessageCreateEvent) string {
var found bool
m.Content = strings.ToLower(m.Content)
for i := 0; i < len(settings); i++ {
if m.Content == settings[i] {
found = true
}
}
if found {
s.settingChange = m.Content
settingStr, sliceOptions := genSetting(m.Content, m.Author.ID.String())
s.options = sliceOptions
return settingStr
}
return "Error: Invalid setting, please try again\n"
}
//Handles the message when the user is changing a setting
func (s *settingsMenu) handleMsgSetting(m *gateway.MessageCreateEvent) string {
option, err := strconv.Atoi(m.Content)
if strings.ToLower(m.Content) == "cancel" {
stopMenu(s)
return "Cancelled"
}
if err != nil {
return "Error: Invalid option, please try again"
}
if option-1 > len(s.options)-1 {
return "Error: Invalid option, please try again"
}
s.save(option - 1)
stopMenu(s)
return "Setting Saved!"
}
//Saves the setting back into the database
func (s *settingsMenu) save(option int) {
ctx, close := context.WithTimeout(context.Background(), time.Second*5)
defer close()
//Switch for changing the setting
switch s.settingChange {
case "graph":
settingCollection.UpdateOne(ctx, bson.M{"id": s.userID}, bson.M{"$set": bson.M{"graphtype": s.options[option]}})
case "hide":
statsCollection.UpdateOne(ctx, bson.M{"id": s.userID, "game": s.options[option]}, bson.M{"$set": bson.M{"ignore": true}})
case "show":
statsCollection.UpdateOne(ctx, bson.M{"id": s.userID, "game": s.options[option]}, bson.M{"$set": bson.M{"ignore": false}})
case "mention":
parsedBool, _ := strconv.ParseBool(s.options[option])
settingCollection.UpdateOne(ctx, bson.M{"id": s.userID}, bson.M{"$set": bson.M{"mentionforstats": parsedBool}})
case "disable":
parsedBool, _ := strconv.ParseBool(s.options[option])
settingCollection.UpdateOne(ctx, bson.M{"id": s.userID}, bson.M{"$set": bson.M{"disable": parsedBool}})
delete(users.users, s.userID)
case "delete":
settingCollection.DeleteOne(ctx, bson.M{"id": s.userID})
statsCollection.DeleteMany(ctx, bson.M{"id": s.userID})
delete(users.users, s.userID)
}
}