Skip to content

Commit

Permalink
feat(command): added pagination for list command response
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Apr 25, 2024
1 parent b090c2d commit 7a9b43f
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 41 deletions.
35 changes: 24 additions & 11 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
type slashCommandHandler struct {
ctx context.Context
commands map[string]command.DiscordSlashCommandHandler
options map[string]command.DiscordEventHandler
options []command.DiscordEventHandler
}

func (sc slashCommandHandler) handleSlashCommand(s *discordgo.Session, i *discordgo.InteractionCreate) {
Expand All @@ -47,12 +47,16 @@ func (sc slashCommandHandler) handleSlashCommand(s *discordgo.Session, i *discor
userID := i.Member.User.ID
// Handle options assigned to slash commands
if i.Type == discordgo.InteractionMessageComponent {
if option, ok := sc.options[i.Data.(discordgo.MessageComponentInteractionData).CustomID]; ok {
logger.InfoContext(ctx, fmt.Sprintf("user '%s' select '%s' option", userID, i.Data.(discordgo.MessageComponentInteractionData).CustomID))
customID := i.Data.(discordgo.MessageComponentInteractionData).CustomID

handleEventResponse(ctx, s, i, option)
for _, option := range sc.options {
if matcher, ok := option.(command.DiscordOptionMatcher); ok && matcher.MatchCustomID(customID) {
logger.InfoContext(ctx, fmt.Sprintf("user '%s' select '%s' option", userID, customID))

return
handleEventResponse(ctx, s, i, option)

return
}
}
}

Expand Down Expand Up @@ -155,15 +159,24 @@ func createCommands(
}
}

func createOptions(services []joke.GetService) map[string]command.DiscordEventHandler {
func createOptions(
services []joke.GetService,
commands map[string]command.DiscordSlashCommandHandler,
) []command.DiscordEventHandler {
apologiesOption := command.ApologiesOption{}
nextJokeOption := command.NextJokeOption{Services: services}
sameJokeCategoryOption := command.SameJokeCategoryOption{Services: services}

return map[string]command.DiscordEventHandler{
command.ApologiesButtonName: apologiesOption,
command.NextJokeButtonName: nextJokeOption,
command.SameJokeCategoryButtonName: sameJokeCategoryOption,
listCommand := commands[command.ListCommandName].(*command.ListCommand)
nextListOption := command.NextListCommandOption{Cmd: listCommand}
previousListOption := command.PreviousListCommandOption{Cmd: listCommand}

return []command.DiscordEventHandler{
apologiesOption,
nextJokeOption,
sameJokeCategoryOption,
nextListOption,
previousListOption,
}
}

Expand Down Expand Up @@ -201,7 +214,7 @@ func NewDiscordBot(ctx context.Context) (*DiscordBot, error) {
}

// General handler for slash commands
handler := slashCommandHandler{ctx, commands, createOptions(getServices)}
handler := slashCommandHandler{ctx, commands, createOptions(getServices, commands)}

bot.AddHandler(handler.handleSlashCommand)

Expand Down
12 changes: 12 additions & 0 deletions bot/internal/command/joke.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ func createJokeFromOptions(data discordgo.ApplicationCommandInteractionData) (j

type ApologiesOption struct{}

func (a ApologiesOption) MatchCustomID(customID string) bool {
return customID == ApologiesButtonName
}

func (a ApologiesOption) Execute(_ context.Context, _ *discordgo.Session, _ *discordgo.InteractionCreate) (DiscordMessageReceiver, error) {
return SimpleMessageResponse{Msg: "Przepraszam"}, nil
}
Expand All @@ -362,6 +366,10 @@ type NextJokeOption struct {
Services []joke.GetService
}

func (n NextJokeOption) MatchCustomID(customID string) bool {
return customID == NextJokeButtonName
}

func (n NextJokeOption) Execute(ctx context.Context, _ *discordgo.Session, i *discordgo.InteractionCreate) (DiscordMessageReceiver, error) {
service, err := selectGetService(ctx, n.Services)
if err != nil {
Expand Down Expand Up @@ -389,6 +397,10 @@ type SameJokeCategoryOption struct {
Services []joke.GetService
}

func (s SameJokeCategoryOption) MatchCustomID(customID string) bool {
return customID == SameJokeCategoryButtonName
}

func (s SameJokeCategoryOption) Execute(ctx context.Context, _ *discordgo.Session, i *discordgo.InteractionCreate) (DiscordMessageReceiver, error) {
embedFields := i.Message.Embeds[0].Fields
category := joke.Category(embedFields[len(embedFields)-1].Value)
Expand Down
Loading

0 comments on commit 7a9b43f

Please sign in to comment.