From 66471f1408b42dc8eec5c11a1fcd34941757a5d6 Mon Sep 17 00:00:00 2001 From: Aleksander Zaruczewski Date: Sun, 12 Feb 2023 17:11:35 +0200 Subject: [PATCH] refactor(cmdroute)!: change data to resp --- 0-examples/commands-hybrid/main.go | 42 ++++++++++++++++++----------- 0-examples/commands-minimal/main.go | 9 +++++-- 0-examples/commands/main.go | 42 ++++++++++++++++++----------- api/cmdroute/fntypes.go | 6 ++--- api/cmdroute/router.go | 9 +++---- api/cmdroute/router_test.go | 36 ++++++++++++++++--------- 6 files changed, 91 insertions(+), 53 deletions(-) diff --git a/0-examples/commands-hybrid/main.go b/0-examples/commands-hybrid/main.go index ca905f3f..333e1227 100644 --- a/0-examples/commands-hybrid/main.go +++ b/0-examples/commands-hybrid/main.go @@ -109,13 +109,16 @@ func newHandler(s *state.State) *handler { return h } -func (h *handler) cmdPing(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData { - return &api.InteractionResponseData{ - Content: option.NewNullableString("Pong!"), +func (h *handler) cmdPing(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("Pong!"), + }, } } -func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { +func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponse { var options struct { Arg string `discord:"argument"` } @@ -124,23 +127,32 @@ func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.I return errorResponse(err) } - return &api.InteractionResponseData{ - Content: option.NewNullableString(options.Arg), - AllowedMentions: &api.AllowedMentions{}, // don't mention anyone + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString(options.Arg), + AllowedMentions: &api.AllowedMentions{}, // don't mention anyone + }, } } -func (h *handler) cmdThonk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { +func (h *handler) cmdThonk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponse { time.Sleep(time.Duration(3+rand.Intn(5)) * time.Second) - return &api.InteractionResponseData{ - Content: option.NewNullableString("https://tenor.com/view/thonk-thinking-sun-thonk-sun-thinking-sun-gif-14999983"), + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("https://tenor.com/view/thonk-thinking-sun-thonk-sun-thinking-sun-gif-14999983"), + }, } } -func errorResponse(err error) *api.InteractionResponseData { - return &api.InteractionResponseData{ - Content: option.NewNullableString("**Error:** " + err.Error()), - Flags: discord.EphemeralMessage, - AllowedMentions: &api.AllowedMentions{ /* none */ }, +func errorResponse(err error) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("**Error:** " + err.Error()), + Flags: discord.EphemeralMessage, + AllowedMentions: &api.AllowedMentions{ /* none */ }, + }, } } diff --git a/0-examples/commands-minimal/main.go b/0-examples/commands-minimal/main.go index a8f88cd5..efd53ded 100644 --- a/0-examples/commands-minimal/main.go +++ b/0-examples/commands-minimal/main.go @@ -16,8 +16,13 @@ var commands = []api.CreateCommandData{{Name: "ping", Description: "Ping!"}} func main() { r := cmdroute.NewRouter() - r.AddFunc("ping", func(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { - return &api.InteractionResponseData{Content: option.NewNullableString("Pong!")} + r.AddFunc("ping", func(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("Pong!"), + }, + } }) s := state.New("Bot " + os.Getenv("BOT_TOKEN")) diff --git a/0-examples/commands/main.go b/0-examples/commands/main.go index b0f6c96b..37d66632 100644 --- a/0-examples/commands/main.go +++ b/0-examples/commands/main.go @@ -88,13 +88,16 @@ func newHandler(s *state.State) *handler { return h } -func (h *handler) cmdPing(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponseData { - return &api.InteractionResponseData{ - Content: option.NewNullableString("Pong!"), +func (h *handler) cmdPing(ctx context.Context, cmd cmdroute.CommandData) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("Pong!"), + }, } } -func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { +func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponse { var options struct { Arg string `discord:"argument"` } @@ -103,23 +106,32 @@ func (h *handler) cmdEcho(ctx context.Context, data cmdroute.CommandData) *api.I return errorResponse(err) } - return &api.InteractionResponseData{ - Content: option.NewNullableString(options.Arg), - AllowedMentions: &api.AllowedMentions{}, // don't mention anyone + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString(options.Arg), + AllowedMentions: &api.AllowedMentions{}, // don't mention anyone + }, } } -func (h *handler) cmdThonk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponseData { +func (h *handler) cmdThonk(ctx context.Context, data cmdroute.CommandData) *api.InteractionResponse { time.Sleep(time.Duration(3+rand.Intn(5)) * time.Second) - return &api.InteractionResponseData{ - Content: option.NewNullableString("https://tenor.com/view/thonk-thinking-sun-thonk-sun-thinking-sun-gif-14999983"), + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("https://tenor.com/view/thonk-thinking-sun-thonk-sun-thinking-sun-gif-14999983"), + }, } } -func errorResponse(err error) *api.InteractionResponseData { - return &api.InteractionResponseData{ - Content: option.NewNullableString("**Error:** " + err.Error()), - Flags: discord.EphemeralMessage, - AllowedMentions: &api.AllowedMentions{ /* none */ }, +func errorResponse(err error) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("**Error:** " + err.Error()), + Flags: discord.EphemeralMessage, + AllowedMentions: &api.AllowedMentions{ /* none */ }, + }, } } diff --git a/api/cmdroute/fntypes.go b/api/cmdroute/fntypes.go index e48cd43c..87eedc98 100644 --- a/api/cmdroute/fntypes.go +++ b/api/cmdroute/fntypes.go @@ -50,16 +50,16 @@ type CommandHandler interface { // handler does not return a response within the deadline, the response will // be automatically deferred in a goroutine, and the returned response will // be sent to the user through the API instead. - HandleCommand(ctx context.Context, data CommandData) *api.InteractionResponseData + HandleCommand(ctx context.Context, data CommandData) *api.InteractionResponse } // CommandHandlerFunc is a function that implements CommandHandler. -type CommandHandlerFunc func(ctx context.Context, data CommandData) *api.InteractionResponseData +type CommandHandlerFunc func(ctx context.Context, data CommandData) *api.InteractionResponse var _ CommandHandler = CommandHandlerFunc(nil) // HandleCommand implements CommandHandler. -func (f CommandHandlerFunc) HandleCommand(ctx context.Context, data CommandData) *api.InteractionResponseData { +func (f CommandHandlerFunc) HandleCommand(ctx context.Context, data CommandData) *api.InteractionResponse { return f(ctx, data) } diff --git a/api/cmdroute/router.go b/api/cmdroute/router.go index 6e569718..dcf370d9 100644 --- a/api/cmdroute/router.go +++ b/api/cmdroute/router.go @@ -135,18 +135,15 @@ func (r *Router) HandleCommand(ev *discord.InteractionEvent, data *discord.Comma func (r *Router) handleCommand(ev *discord.InteractionEvent, found handlerData) *api.InteractionResponse { return r.handleInteraction(ev, func(ctx context.Context, ev *discord.InteractionEvent) *api.InteractionResponse { - data := found.handler.HandleCommand(ctx, CommandData{ + resp := found.handler.HandleCommand(ctx, CommandData{ CommandInteractionOption: found.data, Event: ev, }) - if data == nil { + if resp == nil { return nil } - return &api.InteractionResponse{ - Type: api.MessageInteractionWithSource, - Data: data, - } + return resp }, ) } diff --git a/api/cmdroute/router_test.go b/api/cmdroute/router_test.go index f8646bea..ae38b5c6 100644 --- a/api/cmdroute/router_test.go +++ b/api/cmdroute/router_test.go @@ -45,7 +45,7 @@ func TestRouter(t *testing.T) { t.Run("unknown", func(t *testing.T) { r := NewRouter() - r.AddFunc("test", func(ctx context.Context, data CommandData) *api.InteractionResponseData { + r.AddFunc("test", func(ctx context.Context, data CommandData) *api.InteractionResponse { t.Fatal("unexpected call") return nil }) @@ -61,8 +61,11 @@ func TestRouter(t *testing.T) { } r := NewRouter() - r.AddFunc("ping", func(_ context.Context, _ CommandData) *api.InteractionResponseData { - return data + r.AddFunc("ping", func(_ context.Context, _ CommandData) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: data, + } }) resp := r.HandleInteraction(newInteractionEvent(discord.CommandInteraction{ ID: 4, @@ -83,8 +86,11 @@ func TestRouter(t *testing.T) { } r := NewRouter() - r.AddFunc("ping", func(_ context.Context, _ CommandData) *api.InteractionResponseData { - return nil + r.AddFunc("ping", func(_ context.Context, _ CommandData) *api.InteractionResponse { + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: nil, + } }) r.AddAutocompleterFunc("ping", func(_ context.Context, comp AutocompleteData) api.AutocompleteChoices { var data struct { @@ -239,18 +245,24 @@ func TestRouter(t *testing.T) { Error: func(err error) { t.Error(err) }, Done: func(*discord.Message) { wg.Done() }, })) - r.AddFunc("ping", func(ctx context.Context, data CommandData) *api.InteractionResponseData { + r.AddFunc("ping", func(ctx context.Context, data CommandData) *api.InteractionResponse { assertDeferred(t, ctx, false) - return &api.InteractionResponseData{ - Content: option.NewNullableString("pong"), + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("pong"), + }, } }) - r.AddFunc("ping-defer", func(ctx context.Context, data CommandData) *api.InteractionResponseData { + r.AddFunc("ping-defer", func(ctx context.Context, data CommandData) *api.InteractionResponse { assertDeferred(t, ctx, false) time.Sleep(200 * time.Millisecond) assertDeferred(t, ctx, true) - return &api.InteractionResponseData{ - Content: option.NewNullableString("pong-defer"), + return &api.InteractionResponse{ + Type: api.MessageInteractionWithSource, + Data: &api.InteractionResponseData{ + Content: option.NewNullableString("pong-defer"), + }, } }) @@ -312,7 +324,7 @@ var mockOptions = []discord.CommandInteractionOption{ } func assertHandler(t *testing.T, opts discord.CommandInteractionOptions) CommandHandler { - return CommandHandlerFunc(func(ctx context.Context, data CommandData) *api.InteractionResponseData { + return CommandHandlerFunc(func(ctx context.Context, data CommandData) *api.InteractionResponse { if len(data.Options) != len(opts) { t.Fatalf("expected %d options, got %d", len(opts), len(data.Options)) }