Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cmdroute)!: change data to resp #375

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions 0-examples/commands-hybrid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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 */ },
},
}
}
9 changes: 7 additions & 2 deletions 0-examples/commands-minimal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
42 changes: 27 additions & 15 deletions 0-examples/commands/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand All @@ -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 */ },
},
}
}
6 changes: 3 additions & 3 deletions api/cmdroute/fntypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
9 changes: 3 additions & 6 deletions api/cmdroute/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
)
}
Expand Down
36 changes: 24 additions & 12 deletions api/cmdroute/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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"),
},
}
})

Expand Down Expand Up @@ -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))
}
Expand Down