-
Notifications
You must be signed in to change notification settings - Fork 0
/
discom.go
528 lines (441 loc) · 13.6 KB
/
discom.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package discom
import (
"bytes"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/bwmarrin/discordgo"
)
var (
ErrTooManyArgs = fmt.Errorf("more args given then options")
ErrInvalidArg = fmt.Errorf("invalid arg")
)
type Response struct {
Content string
}
type InteractionPayload struct {
Message string
AuthorId string
GuildId string
ChannelId string
}
// Interaction any interfaction with the commands
type Interaction interface {
Respond(*discordgo.Session, Response) error
GetPayload() *InteractionPayload
Option(name string) *discordgo.ApplicationCommandInteractionDataOption
}
// CommandHandler A callback function which is triggered when a command is ran
// Error should only return data your fine with the user seeing
type CommandHandler func(*discordgo.Session, Interaction) error
// ErrorHandler called if a command handler returns an error
type ErrorHandler func(*discordgo.Session, Interaction, error)
// Command Represents a Command to the discord bot.
type Command struct {
// Name the name of the command commands should not have spaces
Name string
// Handler The handler function which is called on a message matching the regex
Handler CommandHandler
Description string
Version string
Options []*discordgo.ApplicationCommandOption
}
func (c *Command) asDiscordAppCommand() *discordgo.ApplicationCommand {
return &discordgo.ApplicationCommand{
Name: c.Name,
Description: c.Description,
Version: c.Version,
Options: c.Options,
}
}
func (c *Command) parseArgs(args []string) ([]*discordgo.ApplicationCommandInteractionDataOption, error) {
if len(args) == 0 {
return nil, nil
}
if len(args)%2 != 0 {
return nil, errors.Wrapf(ErrInvalidArg, "argument missing value")
}
argMap := make(map[string]string)
optionsMap := make(map[string]*discordgo.ApplicationCommandOption)
requiredCount := 0
for _, option := range c.Options {
optionsMap[option.Name] = option
if option.Required {
requiredCount++
}
}
for i := 0; i < len(args); i += 2 {
cmd := args[i]
if !strings.HasPrefix(cmd, "-") {
return nil, errors.Wrapf(ErrInvalidArg, "%s must have prefix -", cmd)
}
cmd = strings.TrimPrefix(cmd, "-")
if _, ok := optionsMap[cmd]; !ok {
return nil, errors.Wrapf(ErrInvalidArg, "%s is an unknown argument", cmd)
}
if optionsMap[cmd].Required {
requiredCount--
}
argMap[cmd] = args[i+1]
}
if requiredCount != 0 {
return nil, errors.Wrapf(ErrInvalidArg, "missing required argument")
}
var result []*discordgo.ApplicationCommandInteractionDataOption
for cmd, arg := range argMap {
var value interface{}
var err error
switch optionsMap[cmd].Type {
case discordgo.ApplicationCommandOptionString:
value = arg
case discordgo.ApplicationCommandOptionInteger:
intVal, err := strconv.ParseInt(arg, 10, 64)
if err != nil {
return nil, fmt.Errorf("expected %s to be an int but was given %s", optionsMap[cmd].Name, arg)
}
value = float64(intVal)
case discordgo.ApplicationCommandOptionBoolean:
value, err = strconv.ParseBool(arg)
if err != nil {
return nil, fmt.Errorf("expected %s to be an bool but was given %s", optionsMap[cmd].Name, arg)
}
default:
return nil, fmt.Errorf("not implemnted yet")
}
result = append(result, &discordgo.ApplicationCommandInteractionDataOption{
Name: optionsMap[cmd].Name,
Value: value,
})
}
return result, nil
}
func genOptionsMap(options []*discordgo.ApplicationCommandInteractionDataOption) map[string]*discordgo.ApplicationCommandInteractionDataOption {
result := make(map[string]*discordgo.ApplicationCommandInteractionDataOption)
for _, option := range options {
result[option.Name] = option
}
return result
}
type discordInteraction struct {
sent bool
interaction *discordgo.Interaction
optionsMap map[string]*discordgo.ApplicationCommandInteractionDataOption
}
func (d *discordInteraction) Option(name string) *discordgo.ApplicationCommandInteractionDataOption {
if d.optionsMap == nil {
d.optionsMap = genOptionsMap(d.interaction.ApplicationCommandData().Options)
}
return d.optionsMap[name]
}
func (d *discordInteraction) Options() []*discordgo.ApplicationCommandInteractionDataOption {
return d.interaction.ApplicationCommandData().Options
}
func (d *discordInteraction) GetPayload() *InteractionPayload {
result := &InteractionPayload{
AuthorId: d.interaction.Member.User.ID,
GuildId: d.interaction.GuildID,
ChannelId: d.interaction.ChannelID,
}
if d.interaction.Message != nil {
result.Message = d.interaction.Message.Content
}
return result
}
func (d *discordInteraction) Respond(s *discordgo.Session, res Response) error {
body := res.Content
if len(res.Content) >= 2000 {
left, right := body[0:1999], body[2000:len(body)-1]
defer d.Respond(s, Response{Content: right})
body = left
}
if !d.sent {
err := s.InteractionRespond(d.interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: body,
},
})
d.sent = err == nil
return err
}
return s.InteractionResponseEdit(s.State.User.ID, d.interaction, &discordgo.WebhookEdit{
Content: body,
})
}
type discordMessage struct {
message *discordgo.Message
sentId string
options []*discordgo.ApplicationCommandInteractionDataOption
optionsMap map[string]*discordgo.ApplicationCommandInteractionDataOption
}
func (d *discordMessage) Option(name string) *discordgo.ApplicationCommandInteractionDataOption {
if d.optionsMap == nil {
d.optionsMap = genOptionsMap(d.options)
}
return d.optionsMap[name]
}
func (d *discordMessage) GetPayload() *InteractionPayload {
return &InteractionPayload{
Message: d.message.Content,
AuthorId: d.message.Author.ID,
GuildId: d.message.GuildID,
ChannelId: d.message.ChannelID,
}
}
func (d *discordMessage) Respond(s *discordgo.Session, res Response) error {
body := res.Content
if len(res.Content) >= 2000 {
left, right := body[0:1999], body[2000:len(body)-1]
defer d.Respond(s, Response{Content: right})
body = left
}
if d.sentId == "" {
msg, err := s.ChannelMessageSend(d.message.ChannelID, body)
if err == nil {
d.sentId = msg.ID
}
return err
}
_, err := s.ChannelMessageEdit(d.message.ChannelID, d.sentId, body)
return err
}
// CommandSet Use this to regsiter commands and get the handler to pass to discordgo.
// This should be created with CreateCommandSet.
type CommandSet struct {
Prefix string
ErrorHandler ErrorHandler
commands []Command
handlers map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
}
func (c *Command) valid() error {
if c.Name == "" {
return fmt.Errorf("invalid name is empty")
}
if strings.Contains(c.Name, " ") {
return fmt.Errorf("invalid name conatins space")
}
if strings.ToLower(c.Name) == "help" {
return fmt.Errorf("invalid name cannot be help")
}
if c.Handler == nil {
return fmt.Errorf("invalid handler is nil")
}
requiredCompleted := false
for _, option := range c.Options {
if option.Name == "" {
return fmt.Errorf("all options must have a name")
}
if requiredCompleted {
if option.Required {
return fmt.Errorf("requires must be sequential")
}
} else if !option.Required {
requiredCompleted = true
}
}
return nil
}
// CreateCommandSet Creates a command set
func CreateCommandSet(prefix string, errorHandler ErrorHandler) (*CommandSet, error) {
if strings.Contains(prefix, " ") {
return nil, fmt.Errorf("invlaid prefix contains space")
}
return &CommandSet{
Prefix: prefix,
ErrorHandler: errorHandler,
commands: []Command{},
handlers: make(map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)),
}, nil
}
func cleanPattern(pattern string) string {
return strings.ReplaceAll(pattern, "\\", "")
}
func commandsEqual(a, b *discordgo.ApplicationCommand) bool {
aJson, _ := json.Marshal(a.Options)
bJson, _ := json.Marshal(b.Options)
return a.Name == b.Name && a.Description == b.Description && bytes.Equal(aJson, bJson)
}
func (cs *CommandSet) SyncAppCommands(s *discordgo.Session) error {
commands := make(map[string]Command)
for _, cmd := range cs.commands {
commands[cmd.Name] = cmd
}
existingCmds, _ := s.ApplicationCommands(s.State.User.ID, "")
// delete deleted commandss
for _, v := range existingCmds {
if _, ok := commands[v.Name]; !ok {
err := s.ApplicationCommandDelete(v.ApplicationID, "", v.ID)
if err != nil {
return errors.Wrapf(err, "unable to delete out of date command")
}
}
}
// Edit updated commands
for _, v := range existingCmds {
cmd := commands[v.Name]
if _, ok := commands[v.Name]; ok {
if !commandsEqual(v, cmd.asDiscordAppCommand()) {
_, err := s.ApplicationCommandEdit(v.ApplicationID, "", v.ID, cmd.asDiscordAppCommand())
if err != nil {
return errors.Wrapf(err, "Cannot edit '%v' command: %v", v.Name, err)
}
}
delete(commands, v.Name)
}
}
// Create new commands
for _, cmd := range cs.commands {
if _, err := s.ApplicationCommandCreate(s.State.User.ID, "", cmd.asDiscordAppCommand()); err != nil {
log.Fatalf("Cannot create '%v' command: %v", cmd, err)
}
}
return nil
}
// AddCommand Use this to add a command to a command set
func (cs *CommandSet) AddCommand(com Command) error {
if err := com.valid(); err != nil {
return errors.Wrap(err, "invlaid command")
}
cs.commands = append(cs.commands, com)
cs.handlers[com.Name] = func(s *discordgo.Session, i *discordgo.InteractionCreate) {
com.Handler(s, &discordInteraction{
sent: false,
interaction: i.Interaction,
})
}
return nil
}
// Handler Regsiter this with discordgo.AddHandler will be called every time a new message is sent on a guild.
func (cs *CommandSet) Handler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
msg := m.Content
if len(msg) < len(cs.Prefix) {
return
}
if msg[0:len(cs.Prefix)] != cs.Prefix {
return
}
//Remove prefix from message
msg = msg[len(cs.Prefix):]
if len(msg) < 1 {
s.ChannelMessageSend(
m.ChannelID,
cs.replyMessage(m, "Missing command argument"),
)
return
}
args := strings.Split(msg[1:], " ")
if len(args) < 1 {
s.ChannelMessageSend(
m.ChannelID,
cs.replyMessage(m, "Missing command argument"),
)
return
}
for _, cmd := range cs.commands {
tmpMsg := args[0]
if tmpMsg == cmd.Name {
if len(args) > 1 {
//Remove command from args list
args = args[1:]
} else {
//Clear args
args = make([]string, 0)
}
options, err := cmd.parseArgs(args)
if err != nil {
cs.ErrorHandler(s, &discordMessage{message: m.Message}, err)
return
}
inter := &discordMessage{
message: m.Message,
options: options,
}
if err := cmd.Handler(s, inter); err != nil {
cs.ErrorHandler(s, inter, err)
}
return
}
}
var res string
if strings.ToLower(args[0]) == "help" {
res = cs.getHelpMessage()
} else {
res = fmt.Sprintf("unknown command try \"%s help\"", cs.Prefix)
}
s.ChannelMessageSend(
m.ChannelID,
cs.replyMessage(m, res),
)
}
func (cs *CommandSet) IntreactionHandler(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := cs.handlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
}
func (cs *CommandSet) getHelpMessage() string {
var result strings.Builder
fmt.Fprintf(&result, "here are all the commands I know\n")
for _, com := range cs.commands {
var desc string
if com.Description != "" {
desc = com.Description
} else {
desc = "missing description"
}
result.WriteString("\"")
result.WriteString(cleanPattern(cs.Prefix))
result.WriteString(" ")
result.WriteString(com.Name)
result.WriteString("\"")
result.WriteString(" ")
result.WriteString(desc)
if len(com.Options) > 0 {
result.WriteString(" options\n")
}
for _, option := range com.Options {
result.WriteString("\t")
result.WriteString(option.Name)
result.WriteString(" ")
result.WriteString(option.Description)
result.WriteString(" required ")
result.WriteString(strconv.FormatBool(option.Required))
result.WriteString(" type ")
result.WriteString(ApplicationCommandOptionToString(option.Type))
result.WriteString("\n")
}
result.WriteString("\n\n")
}
return result.String()
}
func (cs *CommandSet) replyMessage(m *discordgo.MessageCreate, response string) string {
return fmt.Sprintf("<@%s> %s", m.Author.ID, response)
}
func ApplicationCommandOptionToString(option discordgo.ApplicationCommandOptionType) string {
switch option {
case discordgo.ApplicationCommandOptionSubCommand:
return "Sub Command"
case discordgo.ApplicationCommandOptionSubCommandGroup:
return "Command Grouup"
case discordgo.ApplicationCommandOptionString:
return "String"
case discordgo.ApplicationCommandOptionInteger:
return "Integer"
case discordgo.ApplicationCommandOptionBoolean:
return "Boolean"
case discordgo.ApplicationCommandOptionUser:
return "User"
case discordgo.ApplicationCommandOptionChannel:
return "Channel"
case discordgo.ApplicationCommandOptionRole:
return "Role"
case discordgo.ApplicationCommandOptionMentionable:
return "Mentionable"
}
return ""
}