-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
435 lines (426 loc) · 13.9 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
package main
import (
"strings"
"github.com/bwmarrin/discordgo"
)
type BotCommands struct {
Global []*discordgo.ApplicationCommand
Home []*discordgo.ApplicationCommand
}
// Defaults for command settings.
var (
defaultIntegrationTypes = []discordgo.IntegrationType{discordgo.GuildInstallIntegrationType, discordgo.UserInstallIntegrationType}
defaultContexts = []discordgo.ContextType{discordgo.GuildContextType, discordgo.BotDMContextType, discordgo.PrivateChannelContextType}
)
// CommandsGlobalChat are the globally-enabled Slash commands supported by the
// bot. Commands must be removed from this list before removing their handler
// functions.
var CommandsGlobalChat = []*discordgo.ApplicationCommand{
{
Name: "roll",
Description: "Roll a dice expression",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Options: MergeApplicationCommandOptions(rollOptionsDefault, rollOptionsDetailed, rollOptionsSecret, rollOptionsPrivate),
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "tirar",
},
DescriptionLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Tirar un expressión de dados",
},
},
{
Name: "help",
Description: "Show help for using Dice Golem.",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "ayuda",
},
DescriptionLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Mostrar ayuda para el uso de Dice Golem.",
},
},
{
Name: "info",
Description: "Show bot information for Dice Golem.",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "información",
},
DescriptionLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Mostrar información sobre Dice Golem.",
},
},
{
Name: "secret",
Description: "Make an ephemeral roll that only you will see",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Options: MergeApplicationCommandOptions(rollOptionsDefault, rollOptionsDetailed),
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "secreto",
},
DescriptionLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Tirar un expressión de dados que solo tu verás",
},
},
{
Name: "private",
Description: "Make a roll to have DMed to you",
IntegrationTypes: defaultIntegrationTypes,
Contexts: []discordgo.ContextType{discordgo.GuildContextType, discordgo.PrivateChannelContextType},
Options: MergeApplicationCommandOptions(rollOptionsDefault, rollOptionsDetailed),
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "privado",
},
DescriptionLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Tirar un expressión de dados en un mensaje directo",
},
},
{
Name: "clear",
Description: "Data removal commands",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "recent",
Description: "Clear your recent roll history.",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "expressions",
Description: "Clear your saved roll expressions.",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
DescriptionLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Comandos de eliminación de detos",
},
},
{
Name: "preferences",
Description: "Configure your preferences",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Type: discordgo.ApplicationCommandType(discordgo.ApplicationCommandOptionSubCommand),
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "preferencias",
},
Options: []*discordgo.ApplicationCommandOption{
{
Name: "recent",
Description: "Suggestions based on your recent rolls",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "enabled",
Description: "Enable suggestions based on your recent rolls",
Required: true,
},
},
},
{
Name: "output",
Description: "Roll output preferences",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "detailed",
Description: "Prefer detailed roll output by default",
Required: true,
},
},
},
},
},
{
Name: "buttons",
Description: "Mobile-friendly dice button pads",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "dnd5e",
Description: "Common D&D 5e system dice rolls",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "fate",
Description: "Common Fate (and Fudge) system rolls",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "botones",
},
},
{
Name: "expressions",
Description: "Commands for managing saved expressions",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "save",
Description: "Save an expression with an optional name and label",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: MergeApplicationCommandOptions(rollOptionsDefault, rollOptionsName),
},
{
Name: "unsave",
Description: "Remove a saved expression",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: MergeApplicationCommandOptions([]*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "expression",
Description: "Saved expression to remove",
Autocomplete: true,
Required: true,
},
}),
},
{
Name: "edit",
Description: "Edit your saved expressions (experimental)",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "export",
Description: "Export your saved expressions to a CSV.",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "clear",
Description: "Clear your saved roll exressions.",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
},
{
Name: "ping",
Description: "Check response times.",
IntegrationTypes: defaultIntegrationTypes,
DefaultMemberPermissions: Ptr(int64(discordgo.PermissionManageServer)),
},
{
Name: "invite",
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
Description: "Request an invite for the bot.",
},
{
Name: "Roll Message",
Type: discordgo.MessageApplicationCommand,
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Tirar mensaje",
},
},
{
Name: "Save Expression",
Type: discordgo.MessageApplicationCommand,
IntegrationTypes: defaultIntegrationTypes,
Contexts: defaultContexts,
NameLocalizations: &map[discordgo.Locale]string{
discordgo.SpanishES: "Guardar tira",
},
},
}
// Commands to enable in the bot's home server(s).
var CommandsHomeChat = []*discordgo.ApplicationCommand{
{
Name: "health",
Description: "Show bot health information.",
DefaultMemberPermissions: Ptr(int64(discordgo.PermissionAdministrator)),
},
{
Name: "stats",
Description: "Show bot statistics.",
DefaultMemberPermissions: Ptr(int64(discordgo.PermissionAdministrator)),
},
{
Name: "debug",
Description: "The debug interaction handler",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "channel",
Description: "Selected channel",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
Required: true,
},
},
},
// {
// Name: "settings",
// Description: "Server settings commands",
// DMPermission: Ptr(false),
// DefaultMemberPermissions: Ptr(int64(discordgo.PermissionManageServer)),
// Options: []*discordgo.ApplicationCommandOption{
// {
// Name: "forward",
// Description: "Configure roll forwarding for the current channel",
// Type: discordgo.ApplicationCommandOptionSubCommand,
// Options: []*discordgo.ApplicationCommandOption{
// {
// Name: "channel",
// Description: "Destination channel",
// Type: discordgo.ApplicationCommandOptionChannel,
// ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
// Required: true,
// },
// },
// },
// },
// },
{
Name: "golemancy",
Description: "Bot control commands",
DefaultMemberPermissions: Ptr(int64(discordgo.PermissionAdministrator)),
DMPermission: Ptr(false),
Options: []*discordgo.ApplicationCommandOption{
{
Name: "restart",
Description: "Close and reopen bot connections to Discord",
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "session",
Description: "Close and reopen a shard's session",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "id",
Description: "ID of the target shard",
Type: discordgo.ApplicationCommandOptionInteger,
Required: true,
},
},
},
},
},
},
},
}
// Option sets for commands.
var (
// options common to multiple rolling commands. First item MUST be the roll
// input string.
rollOptionsDefault = []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "expression",
Description: "Dice expression to roll, like '2d6+1'",
Required: true,
Autocomplete: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "label",
Description: "Roll label, like 'fire damage'",
Autocomplete: true,
},
}
rollOptionsDetailed = []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "detailed",
Description: "Include detailed results of the roll",
},
}
rollOptionsSecret = []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "secret",
Description: "Roll as an ephemeral roll",
},
}
rollOptionsPrivate = []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionBoolean,
Name: "private",
Description: "Have the result DMed to you",
},
}
rollOptionsName = []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "name",
Description: "Name for the expression, like 'Fireball'",
Autocomplete: true,
},
}
// helper to fetch a command option value given a name rather than an index
getOptionByName = func(opts []*discordgo.ApplicationCommandInteractionDataOption, name string) *discordgo.ApplicationCommandInteractionDataOption {
for _, opt := range opts {
if opt.Name == name {
return opt
}
for _, subOpt := range opt.Options {
if subOpt.Name == name {
return subOpt
}
}
}
return nil
}
// getOptionByName, but panics if the option is not found
mustGetOptionByName = func(opts []*discordgo.ApplicationCommandInteractionDataOption, name string) *discordgo.ApplicationCommandInteractionDataOption {
opt := getOptionByName(opts, name)
if opt == nil {
panic("nil option")
}
return opt
}
getModalTextInputComponents = func(modal discordgo.ModalSubmitInteractionData) map[string]interface{} {
data := make(map[string]interface{})
for _, irow := range modal.Components {
row := irow.(*discordgo.ActionsRow)
for _, ifield := range row.Components {
field, ok := ifield.(*discordgo.TextInput)
if ok {
data[field.CustomID] = field.Value
}
}
}
return data
}
)
// get focused option and path in the command tree
func getFocusedOption(data discordgo.ApplicationCommandInteractionData) (option *discordgo.ApplicationCommandInteractionDataOption, path string) {
var param strings.Builder
param.WriteString(data.Name)
for _, opt := range data.Options {
if opt.Focused {
param.WriteString(":" + opt.Name)
return opt, param.String()
}
for _, subOpt := range opt.Options {
if subOpt.Focused {
param.WriteString(" " + opt.Name)
param.WriteString(":" + subOpt.Name)
return subOpt, param.String()
}
}
}
return nil, param.String()
}
func getApplicationCommandPaths(data discordgo.ApplicationCommandInteractionData) []string {
path := []string{data.Name}
for _, opt := range data.Options {
if opt.Type == discordgo.ApplicationCommandOptionSubCommandGroup || opt.Type == discordgo.ApplicationCommandOptionSubCommand {
path = append(path, opt.Name)
if opt.Type == discordgo.ApplicationCommandOptionSubCommandGroup {
path = append(path, opt.Options[0].Name)
}
}
}
return path
}