Skip to content

Commit

Permalink
Test message components
Browse files Browse the repository at this point in the history
  • Loading branch information
kn-lim committed Oct 30, 2023
1 parent 857e9c0 commit 1d70772
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cmd/task/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func handler(interaction discordgo.Interaction) error {
}
}

if message == "" {
return nil
}

return discord.SendDeferredMessage(interaction.AppID, interaction.Token, message)
}

Expand Down
14 changes: 7 additions & 7 deletions internal/discord/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,15 @@ var (
Name: "partyfinder",
Description: "Party Finder command",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionString,
Name: "description",
Description: "Description of the event",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionInteger,
Name: "groupsize",
Name: "size",
Description: "Group Size",
Required: false,
},
Expand All @@ -100,12 +106,6 @@ var (
Description: "Event's start date",
Required: false,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "description",
Description: "Description of the event",
Required: true,
},
},
},
Handler: partyfinder,
Expand Down
34 changes: 32 additions & 2 deletions internal/discord/partyfinder.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package discord

import (
"fmt"
"log"

"github.com/bwmarrin/discordgo"
Expand All @@ -10,5 +9,36 @@ import (
func partyfinder(i *discordgo.Interaction, opts ...Option) (string, error) {
log.Println("/partyfinder")

return fmt.Sprintf("/partyfinder %v %v %v %v", i.ApplicationCommandData().Options[0].Value, i.ApplicationCommandData().Options[1].Value, i.ApplicationCommandData().Options[2].Value, i.ApplicationCommandData().Options[3].Value), nil
// Create actions row
actionsRow := &discordgo.ActionsRow{
Components: []discordgo.MessageComponent{
// Create buttons
discordgo.Button{
Label: "Tank",
Style: discordgo.PrimaryButton,
CustomID: "tank",
},
discordgo.Button{
Label: "Healer",
Style: discordgo.SuccessButton,
CustomID: "healer",
},
discordgo.Button{
Label: "DPS",
Style: discordgo.DangerButton,
CustomID: "dps",
},
discordgo.Button{
Label: "🗑️",
Style: discordgo.SecondaryButton,
CustomID: "delete",
},
},
}

if err := SendDeferredMessage(i.AppID, i.Token, "/partyfinder", WithActionsRow(actionsRow)); err != nil {
return "", err
}

return "", nil
}
35 changes: 32 additions & 3 deletions internal/discord/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,31 @@ const (
ErrMissingRole = "You don't have the required role to use this command!"
)

type message struct {
Content string `json:"content"`
Components components `json:"components"`
}

type components struct {
Type int `json:"type"`
Components *discordgo.ActionsRow `json:"components"`
}

type options struct {
actionsRow *discordgo.ActionsRow

// For tests
client *http.Client
url string
}
type Option func(*options)

func WithActionsRow(actionsRow *discordgo.ActionsRow) Option {
return func(o *options) {
o.actionsRow = actionsRow
}
}

func WithClient(client *http.Client) Option {
return func(o *options) {
o.client = client
Expand Down Expand Up @@ -56,16 +75,26 @@ func SendDeferredMessage(appID string, token string, content string, opts ...Opt

// Defaults
config := &options{
// For tests
client: &http.Client{},
url: DiscordBaseURL,
}
for _, opt := range opts {
opt(config)
}

payload, err := json.Marshal(map[string]string{
"content": content,
})
message := message{
Content: content,
Components: components{},
}
if config.actionsRow != nil {
message.Components = components{
Type: int(discordgo.ActionsRowComponent),
Components: config.actionsRow,
}
}

payload, err := json.Marshal(message)
if err != nil {
return fmt.Errorf("couldn't marshal JSON: %v", err)
}
Expand Down

0 comments on commit 1d70772

Please sign in to comment.