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
/
Copy pathcommand.go
81 lines (67 loc) · 2.27 KB
/
command.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
package handler
import (
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
)
type (
CommandHandler func(event *events.ApplicationCommandInteractionCreate) error
AutocompleteHandler func(event *events.AutocompleteInteractionCreate) error
)
type Command struct {
Create discord.ApplicationCommandCreate
Check Check[*events.ApplicationCommandInteractionCreate]
AutocompleteCheck Check[*events.AutocompleteInteractionCreate]
CommandHandlers map[string]CommandHandler
AutocompleteHandlers map[string]AutocompleteHandler
}
func (h *Handler) handleCommand(event *events.ApplicationCommandInteractionCreate) {
name := event.Data.CommandName()
cmd, ok := h.Commands[name]
if !ok || cmd.CommandHandlers == nil {
h.Logger.Errorf("No command or handler found for \"%s\"", name)
}
if cmd.Check != nil && !cmd.Check(event) {
return
}
var path string
if d, ok := event.Data.(discord.SlashCommandInteractionData); ok {
path = buildCommandPath(d.SubCommandName, d.SubCommandGroupName)
}
handler, ok := cmd.CommandHandlers[path]
if !ok {
h.Logger.Warnf("No handler for command \"%s\" with path \"%s\" found", name, path)
return
}
if err := handler(event); err != nil {
h.Logger.Errorf("Failed to handle command \"%s\" with path \"%s\": %s", name, path, err)
}
}
func (h *Handler) handleAutocomplete(event *events.AutocompleteInteractionCreate) {
name := event.Data.CommandName
cmd, ok := h.Commands[name]
if !ok || cmd.AutocompleteHandlers == nil {
h.Logger.Errorf("No command or handler found for \"%s\"", name)
}
if cmd.AutocompleteCheck != nil && !cmd.AutocompleteCheck(event) {
return
}
path := buildCommandPath(event.Data.SubCommandName, event.Data.SubCommandGroupName)
handler, ok := cmd.AutocompleteHandlers[path]
if !ok {
h.Logger.Warnf("No autocomplete handler for command \"%s\" with path \"%s\" found", name, path)
return
}
if err := handler(event); err != nil {
h.Logger.Errorf("Failed to handle autocomplete for command \"%s\" with path \"%s\": %s", name, path, err)
}
}
func buildCommandPath(subcommand *string, subcommandGroup *string) string {
var path string
if subcommand != nil {
path = *subcommand
}
if subcommandGroup != nil {
path = *subcommandGroup + "/" + path
}
return path
}