-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.go
133 lines (122 loc) · 3.99 KB
/
handlers.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"fmt"
"os"
"slices"
"strings"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
"github.com/sirupsen/logrus"
"github.com/vaporvee/acecore/shared"
)
var commands []shared.Command
func ready(e *events.Ready) {
logrus.Info("Starting up...")
removeOldCommandFromAllGuilds(e.Client())
var existingCommandNames []string
existingCommands, err := e.Client().Rest().GetGlobalCommands(e.Client().ApplicationID(), false)
if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err)
} else {
for _, existingCommand := range existingCommands {
existingCommandNames = append(existingCommandNames, existingCommand.Name())
}
}
globalCommands := []discord.ApplicationCommandCreate{}
for _, command := range commands {
if !slices.Contains(existingCommandNames, command.Definition.CommandName()) || slices.Contains(os.Args, "--update-all") || slices.Contains(os.Args, "--clean") {
globalCommands = append(globalCommands, command.Definition)
logrus.Infof("Appending command \"%s\"", command.Definition.CommandName())
}
}
if len(globalCommands) > 0 {
logrus.Infof("Attempting to add global commands %s", fmt.Sprint(globalCommands))
_, err = e.Client().Rest().SetGlobalCommands(e.Client().ApplicationID(), globalCommands)
if err != nil {
logrus.Errorf("error creating global commands '%s'", err)
} else {
logrus.Infof("Added global commands sucessfully!")
}
}
logrus.Info("Successfully started the Bot!")
}
func applicationCommandInteractionCreate(e *events.ApplicationCommandInteractionCreate) {
for _, command := range commands {
if command.Interact != nil && e.Data.CommandName() == command.Definition.CommandName() {
command.Interact(e)
}
}
}
func autocompleteInteractionCreate(e *events.AutocompleteInteractionCreate) {
for _, command := range commands {
if command.Autocomplete != nil && e.Data.CommandName == command.Definition.CommandName() {
command.Autocomplete(e)
}
}
}
func componentInteractionCreate(e *events.ComponentInteractionCreate) {
for _, command := range commands {
if command.ComponentInteract != nil {
if slices.Contains(command.ComponentIDs, e.Data.CustomID()) || slices.ContainsFunc(command.DynamicComponentIDs(), func(id string) bool {
var customID string
if strings.ContainsAny(e.Data.CustomID(), ";") {
customID = strings.TrimSuffix(e.Data.CustomID(), ";"+strings.Split(e.Data.CustomID(), ";")[1])
} else {
customID = e.Data.CustomID()
}
return id == customID
}) {
command.ComponentInteract(e)
}
}
}
}
func modalSubmitInteractionCreate(e *events.ModalSubmitInteractionCreate) {
for _, command := range commands {
if command.ModalSubmit != nil {
var hasID bool = false
var modalIDs []string
if command.ModalIDs != nil {
modalIDs = command.ModalIDs
}
if command.DynamicModalIDs != nil {
modalIDs = append(command.ModalIDs, command.DynamicModalIDs()...)
}
for _, modalID := range modalIDs {
if strings.HasPrefix(e.Data.CustomID, modalID) {
hasID = true
break
}
}
if hasID {
command.ModalSubmit(e)
return // I have no idea why it crashes without that return
}
}
}
}
func removeOldCommandFromAllGuilds(c bot.Client) {
app, err := c.Rest().GetCurrentApplication()
if err != nil {
logrus.Error(err)
}
globalCommands, err := c.Rest().GetGlobalCommands(app.Bot.ID, false)
if err != nil {
logrus.Errorf("error fetching existing global commands: %v", err)
return
}
var commandNames []string
for _, command := range commands {
commandNames = append(commandNames, command.Definition.CommandName())
}
for _, existingCommand := range globalCommands {
if !slices.Contains(commandNames, existingCommand.Name()) {
logrus.Infof("Deleting command '%s'", existingCommand.Name())
err := c.Rest().DeleteGlobalCommand(c.ApplicationID(), existingCommand.ID())
if err != nil {
logrus.Errorf("error deleting command %s: %v", existingCommand.Name(), err)
}
}
}
}