-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.go
267 lines (217 loc) · 6.46 KB
/
commands.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package commands
import (
"fmt"
"net/http"
"github.com/janitorjeff/jeff-bot/commands/audio"
"github.com/janitorjeff/jeff-bot/commands/category"
"github.com/janitorjeff/jeff-bot/commands/connect"
"github.com/janitorjeff/jeff-bot/commands/custom-command"
"github.com/janitorjeff/jeff-bot/commands/god"
"github.com/janitorjeff/jeff-bot/commands/help"
"github.com/janitorjeff/jeff-bot/commands/id"
"github.com/janitorjeff/jeff-bot/commands/mask"
"github.com/janitorjeff/jeff-bot/commands/nick"
"github.com/janitorjeff/jeff-bot/commands/paintball"
"github.com/janitorjeff/jeff-bot/commands/prefix"
"github.com/janitorjeff/jeff-bot/commands/rps"
"github.com/janitorjeff/jeff-bot/commands/search"
"github.com/janitorjeff/jeff-bot/commands/time"
"github.com/janitorjeff/jeff-bot/commands/title"
"github.com/janitorjeff/jeff-bot/commands/tts"
"github.com/janitorjeff/jeff-bot/commands/urban-dictionary"
"github.com/janitorjeff/jeff-bot/commands/wikipedia"
"github.com/janitorjeff/jeff-bot/commands/youtube"
"github.com/janitorjeff/jeff-bot/core"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
)
var Commands = core.CommandsStatic{
audio.Advanced,
category.Normal,
category.Advanced,
connect.Normal,
custom_command.Advanced,
god.Advanced,
god.Normal,
help.Normal,
help.Advanced,
help.Admin,
id.Normal,
mask.Admin,
nick.Normal,
nick.Advanced,
nick.Admin,
paintball.Normal,
prefix.Normal,
prefix.Advanced,
prefix.Admin,
rps.Normal,
search.Advanced,
time.Advanced,
time.NormalTime,
time.NormalTimezone,
title.Normal,
title.Advanced,
tts.Advanced,
tts.NormalTTS,
tts.NormalVoice,
tts.NormalSubOnly,
urban_dictionary.Normal,
urban_dictionary.Advanced,
wikipedia.Normal,
youtube.Normal,
youtube.Advanced,
}
// checkNameCollisions checks if there are any name is used more than once in
// the given list of commands
func checkNameCollisions(cmds core.CommandsStatic) {
// will act like a set
names := map[string]struct{}{}
for _, cmd := range cmds {
for _, n := range cmd.Names() {
if _, ok := names[n]; ok {
panic(fmt.Sprintf("name %s already exists in %v", n, names))
}
names[n] = struct{}{}
}
}
}
// checkDifferentParentType will check if all the parent's children are of the
// same command type as the parent.
func checkDifferentParentType(parent core.CommandStatic, children core.CommandsStatic) {
for _, child := range children {
if parent.Type() != child.Type() {
panic(fmt.Sprintf("child %v is of different type from parent %v", child.Names(), parent.Names()))
}
}
}
// checkDifferentParentType will check if all the parent's children are of the
// same command type as the parent.
func checkDifferentParentCategory(parent core.CommandStatic, children core.CommandsStatic) {
for _, child := range children {
if parent.Category() != child.Category() {
panic(fmt.Sprintf("child %v is of different category from parent %v", child.Names(), parent.Names()))
}
}
}
// checkWrongParentChild will check if a parent's children have set their parent
// correctly.
func checkWrongParentInChild(parent core.CommandStatic, children core.CommandsStatic) {
for _, child := range children {
if child.Parent() != parent {
panic(fmt.Sprintf("incorrect parent-child relationship, expected parent %v for child %v but got %v", parent.Names(), child.Names(), child.Parent().Names()))
}
}
}
// recurse will recursively go through all the children of the passed command
// and perform the following checks on them:
//
// - Name collisions: will check if a name is used more than once among the
// children
// - Wrong type: Will check if a child's command type is different from its
// parent's
// - Wrong parent: Will check if a child has set its parent incorrectly
//
// If any of the above checks fail the program will immediately panic.
func recurse(cmd core.CommandStatic) {
if cmd.Children() == nil {
return
}
checkNameCollisions(cmd.Children())
checkDifferentParentType(cmd, cmd.Children())
checkDifferentParentCategory(cmd, cmd.Children())
checkWrongParentInChild(cmd, cmd.Children())
for _, child := range cmd.Children() {
recurse(child)
}
}
func init() {
for _, cmd := range Commands {
recurse(cmd)
}
}
// This must be run after all of the global variables have been set (including
// ones that frontend init functions might set) since the `Init` functions might
// depend on them.
func Init() {
for _, cmd := range Commands {
if err := cmd.Init(); err != nil {
log.Fatal().Err(err).Msgf("failed to init command %v", core.Format(cmd, "!"))
}
}
}
type CommandJSON struct {
Names []string `json:"names"`
Description string `json:"description"`
Example string `json:"example"`
Parent int `json:"parent"`
Children []int `json:"children"`
Category string `json:"category"`
}
type Resp struct {
Prefix string `json:"prefix"`
Categories []string `json:"categories"`
Commands []CommandJSON `json:"commands"`
}
func ToJSON(t core.CommandType) Resp {
resp := Resp{
Prefix: "!",
Categories: []string{},
Commands: []CommandJSON{},
}
commandsIndex := map[core.CommandStatic]int{}
categories := map[core.CommandCategory]struct{}{}
Commands.Recurse(func(cmd core.CommandStatic) {
if cmd.Type() != t {
return
}
cmdJSON := CommandJSON{
Names: cmd.Names(),
Description: cmd.Description(),
Example: "Example.",
Category: string(cmd.Category()),
}
if cmd.Parent() == nil {
cmdJSON.Parent = -1
} else {
cmdJSON.Parent = commandsIndex[cmd.Parent()]
}
if cmd.Children() == nil {
cmdJSON.Children = []int{}
}
commandsIndex[cmd] = len(resp.Commands)
resp.Commands = append(resp.Commands, cmdJSON)
categories[cmd.Category()] = struct{}{}
})
Commands.Recurse(func(cmd core.CommandStatic) {
if cmd.Type() != t {
return
}
if cmd.Children() == nil {
return
}
index := commandsIndex[cmd]
for _, child := range cmd.Children() {
resp.Commands[index].Children = append(resp.Commands[index].Children, commandsIndex[child])
}
})
for cat, _ := range categories {
resp.Categories = append(resp.Categories, string(cat))
}
return resp
}
func init() {
core.Gin.GET("/api/v1/commands", func(c *gin.Context) {
var t core.CommandType
switch c.DefaultQuery("type", "normal") {
case "normal":
t = core.Normal
case "advanced":
t = core.Advanced
default:
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"error": "invalid type"})
return
}
c.JSON(http.StatusOK, ToJSON(t))
})
}