-
Notifications
You must be signed in to change notification settings - Fork 7
/
context.go
338 lines (301 loc) · 9.39 KB
/
context.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
package ken
import (
"github.com/bwmarrin/discordgo"
)
// Context defines the implementation of an interaction
// command context passed to the command handler.
type Context interface {
Respond(r *discordgo.InteractionResponse) (err error)
RespondEmbed(emb *discordgo.MessageEmbed) (err error)
RespondError(content, title string) (err error)
FollowUp(wait bool, data *discordgo.WebhookParams) (fum *FollowUpMessage)
FollowUpEmbed(emb *discordgo.MessageEmbed) (fum *FollowUpMessage)
FollowUpError(content, title string) (fum *FollowUpMessage)
Defer() (err error)
Get(key string) (v interface{})
Channel() (*discordgo.Channel, error)
Guild() (*discordgo.Guild, error)
User() (u *discordgo.User)
Options() CommandOptions
SlashCommand() (cmd SlashCommand, ok bool)
UserCommand() (cmd UserCommand, ok bool)
MessageCommand() (cmd MessageCommand, ok bool)
HandleSubCommands(handler ...SubCommandHandler) (err error)
GetKen() *Ken
GetSession() *discordgo.Session
GetEvent() *discordgo.InteractionCreate
GetCommand() Command
GetEphemeral() bool
SetEphemeral(v bool)
}
// Ctx holds the invokation context of
// a command.
//
// The Ctx must not be stored or used
// after command execution.
type Ctx struct {
ObjectMap
responded bool
// Ken keeps a reference to the main Ken instance.
Ken *Ken
// Session holds the discordgo session instance.
Session *discordgo.Session
// Event provides the InteractionCreate event
// instance.
Event *discordgo.InteractionCreate
// Command provides the called command instance.
Command Command
// Ephemeral can be set to true which will
// send all subsequent command responses
// only to the user which invoked the command.
Ephemeral bool
}
var _ Context = (*Ctx)(nil)
func newCtx() *Ctx {
return &Ctx{
ObjectMap: make(simpleObjectMap),
}
}
func (c *Ctx) messageFlags(p uint64) (f uint64) {
f = p
if c.Ephemeral {
f |= uint64(discordgo.MessageFlagsEphemeral)
}
return
}
// Respond to an interaction event with the given
// interaction response payload.
//
// When an interaction has already been responded to,
// the response will be edited instead on execution.
func (c *Ctx) Respond(r *discordgo.InteractionResponse) (err error) {
if r.Data == nil {
r.Data = new(discordgo.InteractionResponseData)
}
r.Data.Flags = uint64(c.messageFlags(r.Data.Flags))
if c.responded {
if r == nil || r.Data == nil {
return
}
_, err = c.Session.InteractionResponseEdit(c.Event.Interaction, &discordgo.WebhookEdit{
Content: r.Data.Content,
Embeds: r.Data.Embeds,
Components: r.Data.Components,
Files: r.Data.Files,
AllowedMentions: r.Data.AllowedMentions,
})
} else {
err = c.Session.InteractionRespond(c.Event.Interaction, r)
c.responded = err == nil
if err != nil {
_ = err
}
}
return
}
// RespondEmbed is shorthand for Respond with an
// embed payload as passed.
func (c *Ctx) RespondEmbed(emb *discordgo.MessageEmbed) (err error) {
if emb.Color <= 0 {
emb.Color = c.Ken.opt.EmbedColors.Default
}
return c.Respond(&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
emb,
},
},
})
}
// RespondError is shorthand for RespondEmbed with an
// error embed as message with the passed content and
// title.
func (c *Ctx) RespondError(content, title string) (err error) {
return c.RespondEmbed(&discordgo.MessageEmbed{
Description: content,
Title: title,
Color: c.Ken.opt.EmbedColors.Error,
})
}
// FollowUp creates a follow up message to the
// interaction event and returns a FollowUpMessage
// object containing the created message as well as
// an error instance, if an error occurred.
//
// This way it allows to be chained in one call with
// subsequent FollowUpMessage method calls.
func (c *Ctx) FollowUp(wait bool, data *discordgo.WebhookParams) (fum *FollowUpMessage) {
data.Flags = uint64(c.messageFlags(data.Flags))
fum = &FollowUpMessage{
s: c.Session,
i: c.Event.Interaction,
}
fum.Message, fum.Error = c.Session.FollowupMessageCreate(c.Event.Interaction, wait, data)
return
}
// FollowUpEmbed is shorthand for FollowUp with an
// embed payload as passed.
func (c *Ctx) FollowUpEmbed(emb *discordgo.MessageEmbed) (fum *FollowUpMessage) {
if emb.Color <= 0 {
emb.Color = c.Ken.opt.EmbedColors.Default
}
return c.FollowUp(true, &discordgo.WebhookParams{
Embeds: []*discordgo.MessageEmbed{
emb,
},
})
}
// FollowUpError is shorthand for FollowUpEmbed with an
// error embed as message with the passed content and
// title.
func (c *Ctx) FollowUpError(content, title string) (fum *FollowUpMessage) {
return c.FollowUpEmbed(&discordgo.MessageEmbed{
Description: content,
Title: title,
Color: c.Ken.opt.EmbedColors.Error,
})
}
// Defer is shorthand for Respond with an InteractionResponse
// of the type InteractionResponseDeferredChannelMessageWithSource.
//
// It should be used when the interaction response can not be
// instantly returned.
func (c *Ctx) Defer() (err error) {
err = c.Respond(&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseDeferredChannelMessageWithSource,
})
return
}
// Get either returns an instance from the internal object map -
// if existent. Otherwise, the object is looked up in the specified
// dependency provider, if available. When no object was found in
// either of both maps, nil is returned.
func (c *Ctx) Get(key string) (v interface{}) {
if v = c.ObjectMap.Get(key); v == nil && c.Ken.opt.DependencyProvider != nil {
v = c.Ken.opt.DependencyProvider.Get(key)
}
return
}
// Channel tries to fetch the channel object from the contained
// channel ID using the specified state manager.
func (c *Ctx) Channel() (*discordgo.Channel, error) {
return c.Ken.opt.State.Channel(c.Session, c.Event.ChannelID)
}
// Channel tries to fetch the guild object from the contained
// guild ID using the specified state manager.
func (c *Ctx) Guild() (*discordgo.Guild, error) {
return c.Ken.opt.State.Guild(c.Session, c.Event.GuildID)
}
// User returns the User object of the executor either from
// the events User object or from the events Member object.
func (c *Ctx) User() (u *discordgo.User) {
u = c.Event.User
if u == nil && c.Event.Member != nil {
u = c.Event.Member.User
}
return
}
// Options returns the application command data options
// with additional functionality methods.
func (c *Ctx) Options() CommandOptions {
return c.Event.ApplicationCommandData().Options
}
// SlashCommand returns the contexts Command as a
// SlashCommand interface.
func (c *Ctx) SlashCommand() (cmd SlashCommand, ok bool) {
cmd, ok = c.Command.(SlashCommand)
return
}
// UserCommand returns the contexts Command as a
// UserCommand interface.
func (c *Ctx) UserCommand() (cmd UserCommand, ok bool) {
cmd, ok = c.Command.(UserCommand)
return
}
// MessageCommand returns the contexts Command as a
// MessageCommand interface.
func (c *Ctx) MessageCommand() (cmd MessageCommand, ok bool) {
cmd, ok = c.Command.(MessageCommand)
return
}
// SubCommandHandler is the handler function used
// to handle sub command calls.
type SubCommandHandler struct {
Name string
Run func(ctx *SubCommandCtx) error
}
// SubCommandCtx wraps the current command Ctx and
// with the called sub command name and scopes the
// command options to the options of the called
// sub command.
//
// The SubCommandCtx must not be stored or used
// after command execution.
type SubCommandCtx struct {
*Ctx
SubCommandName string
}
var _ Context = (*SubCommandCtx)(nil)
// Options returns the options array of the called
// sub command.
func (c *SubCommandCtx) Options() CommandOptions {
return c.Ctx.Options().GetByName(c.SubCommandName).Options
}
// HandleSubCommands takes a list of sub command handles.
// When the command is executed, the options are scanned
// for the sib command calls by their names. If one of
// the registered sub commands has been called, the specified
// handler function is executed.
//
// If the call occured, the passed handler function is
// getting passed the scoped sub command Ctx.
//
// The SubCommandCtx passed must not be stored or used
// after command execution.
func (c *Ctx) HandleSubCommands(handler ...SubCommandHandler) (err error) {
for _, h := range handler {
opt := c.Options().Get(0)
if opt.Type != discordgo.ApplicationCommandOptionSubCommand || opt.Name != h.Name {
continue
}
ctx := c.Ken.subCtxPool.Get().(*SubCommandCtx)
ctx.Ctx = c
ctx.SubCommandName = h.Name
err = h.Run(ctx)
c.Ken.subCtxPool.Put(ctx)
break
}
return
}
// GetKen returns the root instance of Ken.
func (c *Ctx) GetKen() *Ken {
return c.Ken
}
// GetSession returns the current Discordgo session instance.
func (c *Ctx) GetSession() *discordgo.Session {
return c.Session
}
// GetEvent returns the InteractionCreate event instance which
// invoked the interaction command.
func (c *Ctx) GetEvent() *discordgo.InteractionCreate {
return c.Event
}
// GetCommand returns the command instance called.
func (c *Ctx) GetCommand() Command {
return c.Command
}
// GetEphemeral returns the current emphemeral state
// of the command invokation.
func (c *Ctx) GetEphemeral() bool {
return c.Ephemeral
}
// SetEphemeral sets the emphemeral state of the command
// invokation.
//
// Ephemeral can be set to true which will
// send all subsequent command responses
// only to the user which invoked the command.
func (c *Ctx) SetEphemeral(v bool) {
c.Ephemeral = v
}