Discord Components & Role Selection (#5)

This commit is contained in:
2023-04-01 01:23:37 -04:00
committed by GitHub
parent 5e6a433b92
commit e1038a15cd
25 changed files with 696 additions and 31 deletions

View File

@ -0,0 +1,45 @@
package discord
import (
"github.com/bwmarrin/discordgo"
"github.com/yeslayla/birdbot/common"
)
type Button struct {
Label string
ID string
discord *Discord
}
// NewButton creates a new button component
func (discord *Discord) NewButton(id string, label string) *Button {
return &Button{
discord: discord,
ID: id,
Label: label,
}
}
// OnClick registers an event when the button is clicked
func (button *Button) OnClick(action func(user common.User)) {
button.discord.session.AddHandler(func(s *discordgo.Session, r *discordgo.InteractionCreate) {
if r.MessageComponentData().CustomID == button.ID {
action(NewUser(r.Member.User))
s.InteractionRespond(r.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
})
}
})
}
func (button *Button) toMessageComponent() discordgo.MessageComponent {
return discordgo.Button{
Label: button.Label,
CustomID: button.ID,
Style: discordgo.PrimaryButton,
}
}