/
slashcommands.go
67 lines (57 loc) · 2.23 KB
/
slashcommands.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
package godiscordbot
import (
"github.com/rs/zerolog/log"
"github.com/bwmarrin/discordgo"
)
// Map slash command handlers for their alias
func (d *DiscordBot) MapSlashCommands(commands map[*discordgo.ApplicationCommand]func(d *DiscordBot, i *discordgo.InteractionCreate)) {
for command, handler := range commands {
d.slashCommandHandlers[command] = handler
}
}
// Process messages to detect /command usage, and invoke the mapped command's handler when applicable
func (d *DiscordBot) slashCommandProcessor(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
// This would be more efficient as a map instead of an array, but is simpler this way for how few commands there are to process
for cmd, handler := range d.slashCommandHandlers {
if cmd.Name == i.ApplicationCommandData().Name {
handler(d, i)
return
}
}
}
}
// Register slash commands with the discord server, unregistering any existing commands
// Note: this only needs to be done if the ApplicationCommand definition changes, and not the handler
func (d *DiscordBot) registerSlashCommands() {
d.unregisterSlashCommands()
log.Info().Msg("Registering Slash Commands")
for command := range d.slashCommandHandlers {
cmd, err := d.s.ApplicationCommandCreate(d.s.State.User.ID, d.GuildID, command)
if err != nil {
log.Error().Msgf("Cannot create '%v' command: %v", command.Name, err)
}
log.Info().Msgf("Registered Slash Command: %s", command.Name)
d.registeredSlashCommands = append(d.registeredSlashCommands, cmd)
}
}
// Unregister slash commands with the discord server
func (d *DiscordBot) unregisterSlashCommands() {
log.Info().Msg("Unregistering Slash Commands")
if len(d.registeredSlashCommands) == 0 {
var err error
d.registeredSlashCommands, err = d.s.ApplicationCommands(d.s.State.User.ID, d.GuildID)
if err != nil {
log.Error().Msgf("Could not fetch registered commands: %v", err)
return
}
}
for _, v := range d.registeredSlashCommands {
err := d.s.ApplicationCommandDelete(d.s.State.User.ID, d.GuildID, v.ID)
if err != nil {
log.Error().Msgf("Cannot delete '%v' command: %v", v.Name, err)
}
log.Info().Msgf("Unregistered Slash Command: %s: %s", v.Name, v.ID)
}
}