-
Notifications
You must be signed in to change notification settings - Fork 0
/
harmonia.go
380 lines (324 loc) · 12.7 KB
/
harmonia.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package harmonia
import (
"errors"
"fmt"
"github.com/bwmarrin/discordgo"
)
// VERSION of Harmonia, follows Semantic Versioning. (http://semver.org/)
const VERSION = "0.7.4"
// A Harmonia represents a connection to the Discord API and contains the slash commands and component handlers used by Harmonia.
type Harmonia struct {
*discordgo.Session
Commands map[string]CommandHandler
ComponentHandlers map[string]CommandFunc
}
// New creates a new Discord session with the provided token and wraps the Harmonia struct around it.
func New(token string) (h *Harmonia, err error) {
s, err := discordgo.New("Bot " + token)
if err != nil {
return nil, err
}
h = &Harmonia{
Session: s,
Commands: make(map[string]CommandHandler),
ComponentHandlers: make(map[string]CommandFunc),
}
return h, err
}
// AddCommand adds a command to Harmonia.
func (h *Harmonia) AddCommand(command CommandHandler) (err error) {
name := command.GetName()
if _, ok := h.Commands[name]; ok {
return fmt.Errorf("command '%v' already exists", name)
}
h.Commands[name] = command
return
}
func (h *Harmonia) interactionMessageFromMessage(m *discordgo.Message, i *discordgo.Interaction) *InteractionMessage {
f := &InteractionMessage{Message: m, Interaction: i}
if m != nil {
guild, _ := h.Guild(m.GuildID)
f.Guild = guild
channel, _ := h.Channel(m.ChannelID)
f.Channel = channel
}
return f
}
// RespondComplex allows you full freedom to respond with whatever you'd like.
func (h *Harmonia) RespondComplex(i *Invocation, resp *discordgo.InteractionResponse) (*InteractionMessage, error) {
err := h.InteractionRespond(i.Interaction, resp)
if err != nil {
return nil, err
}
m, err := h.InteractionResponse(i.Interaction)
return h.interactionMessageFromMessage(m, i.Interaction), err
}
// Respond allows Harmonia to easily respond to an Invocation with a string.
func (h *Harmonia) Respond(i *Invocation, content string) (*InteractionMessage, error) {
return h.RespondComplex(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
},
})
}
// EphemeralRespond does the same as Respond, but sets the flag such that only the invoker can see the message.
func (h *Harmonia) EphemeralRespond(i *Invocation, content string) (*InteractionMessage, error) {
return h.RespondComplex(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
Flags: 1 << 6,
},
})
}
// RespondWithComponents does the same as Respond, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the response.
func (h *Harmonia) RespondWithComponents(i *Invocation, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error) {
comp := ParseComponentMatrix(components)
return h.RespondComplex(i, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: content,
Components: comp,
},
})
}
// DeferResponse sends an acknowledgement to the DiscordAPI, allowing you to send a follow-up message later. See Followup for that.
func (h *Harmonia) DeferResponse(i *Invocation) error {
return h.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
}
// EditResponse edits an already sent response.
func (h *Harmonia) EditResponse(i *Invocation, content string) (*InteractionMessage, error) {
m, err := h.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: &content,
})
return h.interactionMessageFromMessage(m, i.Interaction), err
}
// EditResponseWithComponents does the same as EditResponse, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the response.
func (h *Harmonia) EditResponseWithComponents(i *Invocation, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error) {
comp := ParseComponentMatrix(components)
m, err := h.InteractionResponseEdit(i.Interaction, &discordgo.WebhookEdit{
Content: &content,
Components: &comp,
})
return h.interactionMessageFromMessage(m, i.Interaction), err
}
// DeleteResponse deletes a response.
func (h *Harmonia) DeleteResponse(i *Invocation) error {
return h.InteractionResponseDelete(i.Interaction)
}
// FollowupComplex allows you full freedom to follow-up with whatever you'd like.
func (h *Harmonia) FollowupComplex(i *Invocation, params *discordgo.WebhookParams) (*InteractionMessage, error) {
m, err := h.FollowupMessageCreate(i.Interaction, true, params)
return h.interactionMessageFromMessage(m, i.Interaction), err
}
// Followup sends a follow-up message to the Interaction, this does require you to have used DeferResponse before.
func (h *Harmonia) Followup(i *Invocation, content string) (*InteractionMessage, error) {
return h.FollowupComplex(i, &discordgo.WebhookParams{
Content: content,
})
}
// EphemeralFollowup does the same as Followup, but sets the flag such that only the invoker can see the message.
func (h *Harmonia) EphemeralFollowup(i *Invocation, content string) (*InteractionMessage, error) {
return h.FollowupComplex(i, &discordgo.WebhookParams{
Content: content,
Flags: 1 << 6,
})
}
// FollowupWithComponents does the same as Followup, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the response.
func (h *Harmonia) FollowupWithComponents(i *Invocation, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error) {
comp := ParseComponentMatrix(components)
return h.FollowupComplex(i, &discordgo.WebhookParams{
Content: content,
Components: comp,
})
}
// EditFollowup allows you to edit a follow-up message.
func (h *Harmonia) EditFollowup(f *InteractionMessage, content string) (*InteractionMessage, error) {
m, err := h.FollowupMessageEdit(f.Interaction, f.ID, &discordgo.WebhookEdit{
Content: &content,
})
return h.interactionMessageFromMessage(m, f.Interaction), err
}
// EditFollowupWithComponents does the same as EditFollowup, but also takes in a 2D slice of discordgo.MessageComponents that will be added to the follow-up message.
func (h *Harmonia) EditFollowupWithComponents(f *InteractionMessage, content string, components [][]discordgo.MessageComponent) (*InteractionMessage, error) {
comp := ParseComponentMatrix(components)
m, err := h.FollowupMessageEdit(f.Interaction, f.ID, &discordgo.WebhookEdit{
Content: &content,
Components: &comp,
})
return h.interactionMessageFromMessage(m, f.Interaction), err
}
// DeleteFollowup deletes a follow-up message.
func (h *Harmonia) DeleteFollowup(f *InteractionMessage) error {
return h.FollowupMessageDelete(f.Interaction, f.ID)
}
// AddComponentHandler adds a handler for a component.
// I suggest this is only used for globally used components, and not for components used on a message by message basis. See AddComponentHandlerToInteractionMessage
func (h *Harmonia) AddComponentHandler(customID string, handler CommandFunc) error {
if customID == "" {
return errors.New("empty CustomID")
}
if _, ok := h.ComponentHandlers[customID]; ok {
return fmt.Errorf("CustomID '%v' already exists", customID)
}
h.ComponentHandlers[customID] = handler
return nil
}
// AddComponentHandlerToInteractionMessage adds a handler for a component, but will be handled only on its original Interaction.
// This is done by prepending the InteractionMessage's ID to the customID. Harmonia will do the heavy lifting from there.
func (h *Harmonia) AddComponentHandlerToInteractionMessage(f *InteractionMessage, customID string, handler CommandFunc) error {
if customID == "" {
return errors.New("empty CustomID")
}
followupcustomID := fmt.Sprintf("%v-%v", f.ID, customID)
if _, ok := h.ComponentHandlers[followupcustomID]; ok {
return fmt.Errorf("customID '%v' already exists on Followup '%v'", customID, f.ID)
}
h.ComponentHandlers[followupcustomID] = handler
return nil
}
// RemoveComponentHandler removes a component handler.
func (h *Harmonia) RemoveComponentHandler(customID string) error {
if _, ok := h.ComponentHandlers[customID]; !ok {
return fmt.Errorf("customID '%v' not found", customID)
}
delete(h.ComponentHandlers, customID)
return nil
}
// RemoveComponentHandlerFromInteractionMessage removes a component handler from an InteractionMessage.
func (h *Harmonia) RemoveComponentHandlerFromInteractionMessage(f *InteractionMessage, customID string) error {
followupcustomID := fmt.Sprintf("%v-%v", f.ID, customID)
if _, ok := h.ComponentHandlers[followupcustomID]; !ok {
return fmt.Errorf("customID '%v' not found on Followup '%v'", customID, f.ID)
}
delete(h.ComponentHandlers, followupcustomID)
return nil
}
// Run starts the Harmonia bot up and does the handling for slash commands and components for you.
func (h *Harmonia) Run() error {
h.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if command, ok := h.Commands[i.ApplicationCommandData().Name]; ok {
guild, _ := h.Guild(i.GuildID)
channel, _ := h.Channel(i.ChannelID)
author, _ := AuthorFromInteraction(h, i.Interaction)
options := i.ApplicationCommandData().Options
command.Do(h, &Invocation{
Interaction: i.Interaction,
Guild: guild,
Channel: channel,
Author: author,
options: options,
targetID: i.ApplicationCommandData().TargetID,
})
}
return
case discordgo.InteractionMessageComponent:
if componentHandler, ok := h.ComponentHandlers[i.MessageComponentData().CustomID]; ok {
guild, _ := h.Guild(i.GuildID)
channel, _ := h.Channel(i.ChannelID)
author, _ := AuthorFromInteraction(h, i.Interaction)
values := i.MessageComponentData().Values
componentHandler(h, &Invocation{
Interaction: i.Interaction,
Guild: guild,
Channel: channel,
Author: author,
Values: values,
})
return
}
followupcustomID := fmt.Sprintf("%v-%v", i.Message.ID, i.MessageComponentData().CustomID)
if componentHandler, ok := h.ComponentHandlers[followupcustomID]; ok {
guild, _ := h.Guild(i.GuildID)
channel, _ := h.Channel(i.ChannelID)
author, _ := AuthorFromInteraction(h, i.Interaction)
values := i.MessageComponentData().Values
componentHandler(h, &Invocation{
Interaction: i.Interaction,
Guild: guild,
Channel: channel,
Author: author,
Values: values,
})
return
}
}
})
err := h.Open()
if err != nil {
return err
}
for _, command := range h.Commands {
data := command.getRegistration()
registration, err := h.ApplicationCommandCreate(h.State.User.ID, data.GuildID, data)
if err != nil {
return err
}
command.setRegistration(registration)
}
return nil
}
// RemoveCommand removes a slash command from Harmonia and from the Discord API.
func (h *Harmonia) RemoveCommand(name string) error {
command, ok := h.Commands[name]
if !ok {
return fmt.Errorf("command '%v' was not found", name)
}
registration := command.getRegistration()
if registration.ID == "" {
return fmt.Errorf("command '%v' was not registered", name)
}
err := h.ApplicationCommandDelete(h.State.User.ID, registration.GuildID, registration.ID)
if err != nil {
return err
}
delete(h.Commands, name)
return nil
}
// RemoveAllCommands does removes all registered commands from the Discord API.
func (h *Harmonia) RemoveAllCommands() error {
globals, err := h.ApplicationCommands(h.State.User.ID, "")
if err != nil {
return err
}
for _, global := range globals {
err := h.ApplicationCommandDelete(h.State.User.ID, global.GuildID, global.ID)
if err != nil {
return err
}
}
for _, guild := range h.State.Guilds {
locals, err := h.ApplicationCommands(h.State.User.ID, guild.ID)
if err != nil {
return err
}
for _, local := range locals {
err := h.ApplicationCommandDelete(h.State.User.ID, local.GuildID, local.ID)
if err != nil {
return err
}
}
}
h.Commands = make(map[string]CommandHandler)
return nil
}
// ParseComponentMatrix parses a 2D slice of MessageComponents and returns a 1D slice of MessageComponents with ActionsRows.
func ParseComponentMatrix(components [][]discordgo.MessageComponent) []discordgo.MessageComponent {
comp := make([]discordgo.MessageComponent, len(components))
for i, c := range components {
comp[i] = &discordgo.ActionsRow{Components: c}
}
return comp
}
// InteractionMessage describes a message sent as a follow-up or response to an Interaction.
type InteractionMessage struct {
*discordgo.Message
Interaction *discordgo.Interaction
Channel *discordgo.Channel
Guild *discordgo.Guild
}