This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
85 lines (73 loc) · 2.1 KB
/
handler.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
package handler
import (
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/disgoorg/log"
"github.com/disgoorg/snowflake/v2"
)
var _ bot.EventListener = (*Handler)(nil)
func New(logger log.Logger) *Handler {
return &Handler{
Logger: logger,
Commands: map[string]Command{},
Components: map[string]Component{},
Modals: map[string]Modal{},
}
}
type Handler struct {
Logger log.Logger
Commands map[string]Command
Components map[string]Component
Modals map[string]Modal
}
func (h *Handler) AddCommands(commands ...Command) {
for _, command := range commands {
h.Commands[command.Create.CommandName()] = command
}
}
func (h *Handler) AddComponents(components ...Component) {
for _, component := range components {
h.Components[component.Name] = component
}
}
func (h *Handler) AddModals(modals ...Modal) {
for _, modal := range modals {
h.Modals[modal.Name] = modal
}
}
func (h *Handler) SyncCommands(client bot.Client, guildIDs ...snowflake.ID) {
commands := make([]discord.ApplicationCommandCreate, len(h.Commands))
var i int
for _, command := range h.Commands {
commands[i] = command.Create
i++
}
if len(guildIDs) == 0 {
if _, err := client.Rest().SetGlobalCommands(client.ApplicationID(), commands); err != nil {
h.Logger.Error("Failed to sync global commands: ", err)
return
}
h.Logger.Infof("Synced %d global commands", len(commands))
return
}
for _, guildID := range guildIDs {
if _, err := client.Rest().SetGuildCommands(client.ApplicationID(), guildID, commands); err != nil {
h.Logger.Errorf("Failed to sync commands for guild %d: %s", guildID, err)
continue
}
h.Logger.Infof("Synced %d commands for guild %s", len(commands), guildID)
}
}
func (h *Handler) OnEvent(event bot.Event) {
switch e := event.(type) {
case *events.ApplicationCommandInteractionCreate:
h.handleCommand(e)
case *events.AutocompleteInteractionCreate:
h.handleAutocomplete(e)
case *events.ComponentInteractionCreate:
h.handleComponent(e)
case *events.ModalSubmitInteractionCreate:
h.handleModal(e)
}
}