-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommands.go
84 lines (78 loc) · 2.44 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
package sm_report
import (
"github.com/bwmarrin/discordgo"
"github.com/r4g3baby/sm-report/config"
"regexp"
)
type command struct {
command *discordgo.ApplicationCommand
handler func(s *discordgo.Session, i *discordgo.InteractionCreate)
}
var (
commands = map[string]command{}
components = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){}
regexComponents = map[*regexp.Regexp]func(s *discordgo.Session, i *discordgo.InteractionCreate, match []string){}
)
func init() {
handlers = append(handlers, handler{f: func(s *discordgo.Session, _ *discordgo.Ready) {
if cmds, err := s.ApplicationCommands(config.Config.Bot.AppID, config.Config.Bot.GuildID); err == nil {
for _, cmd := range cmds {
if _, ok := commands[cmd.Name]; !ok {
if err := s.ApplicationCommandDelete(config.Config.Bot.AppID, config.Config.Bot.GuildID, cmd.ID); err != nil {
config.Logger.Errorw("failed to delete application command",
"command", cmd.Name,
"error", err,
)
}
}
}
} else {
config.Logger.Errorw("failed to list application commands",
"error", err,
)
}
for name, cmd := range commands {
cmd.command.Name = name
if _, err := s.ApplicationCommandCreate(config.Config.Bot.AppID, config.Config.Bot.GuildID, cmd.command); err != nil {
config.Logger.Errorw("failed to create application command",
"command", cmd.command.Name,
"error", err,
)
}
}
}})
handlers = append(handlers, handler{f: func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand, discordgo.InteractionApplicationCommandAutocomplete:
if command, ok := commands[i.ApplicationCommandData().Name]; ok {
command.handler(s, i)
}
case discordgo.InteractionMessageComponent:
customID := i.MessageComponentData().CustomID
if component, ok := components[customID]; ok {
component(s, i)
} else {
for regex, handler := range regexComponents {
match := regex.FindStringSubmatch(customID)
if len(match) > 0 {
handler(s, i, match)
break
}
}
}
case discordgo.InteractionModalSubmit:
customID := i.ModalSubmitData().CustomID
if component, ok := components[customID]; ok {
component(s, i)
} else {
for regex, handler := range regexComponents {
match := regex.FindStringSubmatch(customID)
if len(match) > 0 {
handler(s, i, match)
break
}
}
}
}
}})
}