-
Notifications
You must be signed in to change notification settings - Fork 14
/
client-registry.go
184 lines (144 loc) · 5.03 KB
/
client-registry.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package tempest
import (
"errors"
"net/http"
)
func (client *Client) RegisterCommand(command Command) error {
if _, exists := client.commands[command.Name]; exists {
return errors.New("client already has registered \"" + command.Name + "\" slash command (name already in use)")
}
if command.Type == 0 {
command.Type = CHAT_INPUT_COMMAND_TYPE
}
if command.ApplicationID == 0 {
command.ApplicationID = client.ApplicationID
}
tree := make(map[string]Command)
tree[ROOT_PLACEHOLDER] = command
client.commands[command.Name] = tree
return nil
}
func (client *Client) RegisterSubCommand(subCommand Command, rootCommandName string) error {
if _, available := client.commands[rootCommandName]; !available {
return errors.New("missing \"" + rootCommandName + "\" slash command in registry (root command needs to be registered in client before adding subcommands)")
}
if _, available := client.commands[rootCommandName][subCommand.Name]; available {
return errors.New("client already has registered \"" + rootCommandName + "@" + subCommand.Name + "\" slash subcommand")
}
client.commands[rootCommandName][subCommand.Name] = subCommand
return nil
}
// Bind function to all components with matching custom ids. App will automatically run bound function whenever receiving component interaction with matching custom id.
func (client *Client) RegisterComponent(customIDs []string, fn func(ComponentInteraction)) error {
for _, ID := range customIDs {
_, exists := client.components[ID]
if exists {
return errors.New("client already has registered \"" + ID + "\" component (custom id already in use)")
}
}
for _, ID := range customIDs {
client.components[ID] = fn
}
return nil
}
// Bind function to modal with matching custom id. App will automatically run bound function whenever receiving modal interaction with matching custom id.
func (client *Client) RegisterModal(customID string, fn func(ModalInteraction)) error {
_, exists := client.modals[customID]
if exists {
return errors.New("client already has registered \"" + customID + "\" modal (custom id already in use)")
}
client.modals[customID] = fn
return nil
}
// Sync currently cached slash commands to discord API. By default it'll try to make (bulk) global update (limit 100 updates per day), provide array with guild id snowflakes to update data only for specific guilds.
// You can also add second param -> slice with all command names you want to update (whitelist). There's also third, boolean param that when = true will reverse wishlist to work as blacklist.
func (client *Client) SyncCommands(guildIDs []Snowflake, whitelist []string, switchMode bool) error {
payload := client.parseCommands(whitelist, switchMode)
if len(guildIDs) == 0 {
_, err := client.Rest.Request(http.MethodPut, "/applications/"+client.ApplicationID.String()+"/commands", payload)
return err
}
for _, guildID := range guildIDs {
_, err := client.Rest.Request(http.MethodPut, "/applications/"+client.ApplicationID.String()+"/guilds/"+guildID.String()+"/commands", payload)
if err != nil {
return err
}
}
return nil
}
func (client *Client) seekCommand(itx CommandInteraction) (CommandInteraction, *Command, bool) {
if len(itx.Data.Options) != 0 && itx.Data.Options[0].Type == SUB_OPTION_TYPE {
command, available := client.commands[itx.Data.Name][itx.Data.Options[0].Name]
if available {
if itx.Member != nil {
itx.Member.GuildID = itx.GuildID
}
itx.Data.Name, itx.Data.Options = itx.Data.Options[0].Name, itx.Data.Options[0].Options
itx.Client = client
}
return itx, &command, available
}
if itx.Member != nil {
itx.Member.GuildID = itx.GuildID
}
itx.Client = client
command, available := client.commands[itx.Data.Name][ROOT_PLACEHOLDER]
return itx, &command, available
}
// Parses registered commands into Discord format.
func (client *Client) parseCommands(whitelist []string, reverseMode bool) []Command {
list := make([]Command, len(client.commands))
var itx uint32 = 0
for _, tree := range client.commands {
command := tree[ROOT_PLACEHOLDER]
if len(tree) > 1 {
for key, subCommand := range tree {
if key == ROOT_PLACEHOLDER {
continue
}
command.Options = append(command.Options, CommandOption{
Name: subCommand.Name,
Description: subCommand.Description,
Type: SUB_OPTION_TYPE,
Options: subCommand.Options,
})
}
}
list[itx] = command
itx++
}
wls := len(whitelist)
if wls == 0 {
return list
}
itx = 0
// Work as blacklist
if reverseMode {
filteredList := make([]Command, len(list)-wls)
for itx, command := range list {
blocked := false
for _, cmdName := range whitelist {
if command.Name == cmdName {
blocked = true
break
}
}
if blocked {
continue
}
filteredList[itx] = command
}
return filteredList
}
// Work as whitelist
filteredList := make([]Command, wls)
for _, command := range list {
for _, cmdName := range whitelist {
if command.Name == cmdName {
filteredList[itx] = command
itx++
}
}
}
return filteredList
}