-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands_handler.go
74 lines (71 loc) · 1.99 KB
/
commands_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
package gokord
import (
"errors"
"github.com/anhgelus/gokord/utils"
"github.com/bwmarrin/discordgo"
)
var (
ErrSubsAreNil = errors.New("subs are nil in general handler")
)
// generalHandler used for subcommand
func (b *Bot) generalHandler(client *discordgo.Session, i *discordgo.InteractionCreate) {
resp := utils.ResponseBuilder{
I: i,
C: client,
}
data := i.ApplicationCommandData()
if len(data.Options) == 0 {
utils.SendWarn("len(data.Options) == 0", "name", data.Name)
err := resp.IsEphemeral().Message("No subcommand identified (may be a bug)").Send()
if err != nil {
utils.SendAlert("commands_handler.go - No subcommand identified reply", err.Error())
return
}
return
}
subInfo := data.Options[0]
if subInfo == nil {
utils.SendWarn("subInfo == nil", "name", data.Name)
err := resp.IsEphemeral().Message("No subcommand identified").Send()
if err != nil {
utils.SendAlert("commands_handler.go - No subcommand identified reply", err.Error())
return
}
return
}
var cmd *Cmd
for _, c := range b.Commands {
if c.Name == data.Name {
cmd = c.ToCmd()
}
}
if cmd == nil {
utils.SendWarn("cmd == nil", "name", data.Name)
err := resp.IsEphemeral().Message("Command not found").Send()
if err != nil {
utils.SendAlert("commands_handler.go - Command not found reply", err.Error())
return
}
return
}
if cmd.Subs == nil {
utils.SendAlert("commands_handler.go - Checking subs", ErrSubsAreNil.Error())
err := resp.IsEphemeral().Message("Internal error, please report it").Send()
if err != nil {
utils.SendAlert("commands_handler.go - Command not found reply", err.Error())
return
}
}
for _, s := range cmd.Subs {
if subInfo.Name == s.Name {
s.Handler(client, i)
return
}
}
utils.SendWarn("Subcommand not found", "name", data.Name, "subInfo name", subInfo.Name)
err := resp.IsEphemeral().Message("Subcommand not found").Send()
if err != nil {
utils.SendAlert("commands_handler.go - Subcommand not found reply", err.Error())
return
}
}