Skip to content

Commit ab625dc

Browse files
authored
add context.Context support (#10)
* add context.Context support * bump postcord deps * fixes for tests
1 parent 041a4ad commit ab625dc

20 files changed

+1378
-1111
lines changed

command.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package router
22

33
import (
44
"container/list"
5+
"context"
56
"errors"
7+
"strconv"
8+
69
"github.com/Postcord/objects"
710
"github.com/Postcord/rest"
8-
"strconv"
911
)
1012

1113
// Command is used to define a Discord (sub-)command. DO NOT MAKE YOURSELF! USE CommandGroup.NewCommandBuilder OR CommandRouter.NewCommandBuilder!
@@ -126,7 +128,7 @@ func (c *Command) mapOptions(autocomplete bool, data *objects.ApplicationCommand
126128
}
127129

128130
// Execute the command.
129-
func (c *Command) execute(opts commandExecutionOptions, middlewareList *list.List) (resp *objects.InteractionResponse) {
131+
func (c *Command) execute(reqCtx context.Context, opts commandExecutionOptions, middlewareList *list.List) (resp *objects.InteractionResponse) {
130132
// Process the options.
131133
var mappedOptions map[string]interface{}
132134
if opts.data.TargetID != 0 {
@@ -169,6 +171,7 @@ func (c *Command) execute(opts commandExecutionOptions, middlewareList *list.Lis
169171
globalAllowedMentions: opts.allowedMentions,
170172
errorHandler: opts.exceptionHandler,
171173
Interaction: opts.interaction,
174+
Context: reqCtx,
172175
Command: c,
173176
Options: mappedOptions,
174177
RESTClient: opts.restClient,

command__test.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package router
55

66
import (
77
"container/list"
8+
"context"
89
"errors"
910
"testing"
1011

@@ -136,7 +137,7 @@ func TestCommand_mapOptions(t *testing.T) {
136137
},
137138
{
138139
name: "channel option",
139-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
140+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
140141
cmdOptions: []*objects.ApplicationCommandOption{
141142
{
142143
OptionType: objects.TypeChannel,
@@ -153,13 +154,13 @@ func TestCommand_mapOptions(t *testing.T) {
153154
expects: map[string]interface{}{
154155
"opt1": &ResolvableChannel{
155156
id: "123",
156-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
157+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
157158
},
158159
},
159160
},
160161
{
161162
name: "role option",
162-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
163+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
163164
cmdOptions: []*objects.ApplicationCommandOption{
164165
{
165166
OptionType: objects.TypeRole,
@@ -176,13 +177,13 @@ func TestCommand_mapOptions(t *testing.T) {
176177
expects: map[string]interface{}{
177178
"opt1": &ResolvableRole{
178179
id: "123",
179-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
180+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
180181
},
181182
},
182183
},
183184
{
184185
name: "user option",
185-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
186+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
186187
cmdOptions: []*objects.ApplicationCommandOption{
187188
{
188189
OptionType: objects.TypeUser,
@@ -199,7 +200,7 @@ func TestCommand_mapOptions(t *testing.T) {
199200
expects: map[string]interface{}{
200201
"opt1": &ResolvableUser{
201202
id: "123",
202-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
203+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
203204
},
204205
},
205206
},
@@ -262,7 +263,7 @@ func TestCommand_mapOptions(t *testing.T) {
262263
},
263264
{
264265
name: "mentionable option",
265-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
266+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
266267
cmdOptions: []*objects.ApplicationCommandOption{
267268
{
268269
OptionType: objects.TypeMentionable,
@@ -279,7 +280,7 @@ func TestCommand_mapOptions(t *testing.T) {
279280
expects: map[string]interface{}{
280281
"opt1": &ResolvableMentionable{
281282
id: "123",
282-
data: &objects.ApplicationCommandInteractionData{ID: 6921},
283+
data: &objects.ApplicationCommandInteractionData{DiscordBaseObject: objects.DiscordBaseObject{ID: 6921}},
283284
},
284285
},
285286
},
@@ -333,12 +334,12 @@ var messageTargetData = &objects.ApplicationCommandInteractionData{
333334
Resolved: objects.ApplicationCommandInteractionDataResolved{
334335
Users: map[objects.Snowflake]objects.User{
335336
1: {
336-
ID: 1234,
337+
DiscordBaseObject: objects.DiscordBaseObject{ID: 6921},
337338
},
338339
},
339340
Messages: map[objects.Snowflake]objects.Message{
340341
1: {
341-
ID: 5678,
342+
DiscordBaseObject: objects.DiscordBaseObject{ID: 6921},
342343
},
343344
},
344345
},
@@ -349,12 +350,12 @@ var userTargetData = &objects.ApplicationCommandInteractionData{
349350
Resolved: objects.ApplicationCommandInteractionDataResolved{
350351
Users: map[objects.Snowflake]objects.User{
351352
2: {
352-
ID: 1234,
353+
DiscordBaseObject: objects.DiscordBaseObject{ID: 6921},
353354
},
354355
},
355356
Messages: map[objects.Snowflake]objects.Message{
356357
1: {
357-
ID: 5678,
358+
DiscordBaseObject: objects.DiscordBaseObject{ID: 6921},
358359
},
359360
},
360361
},
@@ -515,7 +516,7 @@ func TestCommand_execute(t *testing.T) {
515516
}
516517

517518
// Execute the command.
518-
resp := c.execute(commandExecutionOptions{
519+
resp := c.execute(context.Background(), commandExecutionOptions{
519520
restClient: dummyRestClient,
520521
exceptionHandler: errHandler,
521522
interaction: dummyInteraction,

command_router.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package router
22

33
import (
44
"container/list"
5+
"context"
56
"encoding/json"
67
"errors"
78
"fmt"
@@ -29,6 +30,9 @@ type CommandRouterCtx struct {
2930
// Defines the interaction which started this.
3031
*objects.Interaction
3132

33+
// Context is a context.Context passed from the HTTP handler.
34+
Context context.Context
35+
3236
// Command defines the command that was invoked.
3337
Command *Command `json:"command"`
3438

@@ -204,7 +208,7 @@ var NoAutoCompleteFunc = errors.New("discord sent auto-complete for argument wit
204208

205209
// Used to define the autocomplete handler.
206210
func (c *CommandRouter) autocompleteHandler(loader loaderPassthrough) interactions.HandlerFunc {
207-
return func(interaction *objects.Interaction) *objects.InteractionResponse {
211+
return func(reqCtx context.Context, interaction *objects.Interaction) *objects.InteractionResponse {
208212
// Parse the data JSON.
209213
var rootData objects.ApplicationCommandInteractionData
210214
if err := json.Unmarshal(interaction.Data, &rootData); err != nil {
@@ -429,7 +433,7 @@ func (c *CommandRouter) commandHandler(loader loaderPassthrough) interactions.Ha
429433
}
430434

431435
// Process the response.
432-
return func(interaction *objects.Interaction) *objects.InteractionResponse {
436+
return func(reqCtx context.Context, interaction *objects.Interaction) *objects.InteractionResponse {
433437
// Handle middleware.
434438
middlewareList := list.New()
435439
if c.middleware != nil {
@@ -488,7 +492,7 @@ func (c *CommandRouter) commandHandler(loader loaderPassthrough) interactions.Ha
488492
switch x := cmdOrCat.(type) {
489493
case *Command:
490494
// In this case, we should go ahead and execute.
491-
resp := x.execute(commandExecutionOptions{
495+
resp := x.execute(reqCtx, commandExecutionOptions{
492496
restClient: r,
493497
exceptionHandler: errHandler,
494498
allowedMentions: allowedMentions,

0 commit comments

Comments
 (0)