Skip to content

Commit

Permalink
feat(Random): added component handler for subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Dec 28, 2024
1 parent 980a2f6 commit e43844b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions event/component/componentBase.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package component
import (
"cake4everybot/logger"
"cake4everybot/modules/adventcalendar"
"cake4everybot/modules/random"
"cake4everybot/modules/secretsanta"

"github.com/bwmarrin/discordgo"
Expand Down Expand Up @@ -34,6 +35,7 @@ func Register() {
var componentList []Component

componentList = append(componentList, adventcalendar.Component{})
componentList = append(componentList, random.Component{})
componentList = append(componentList, secretsanta.Component{})

if len(componentList) == 0 {
Expand Down
51 changes: 51 additions & 0 deletions modules/random/component.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package random

import (
"cake4everybot/util"
"strings"

"github.com/bwmarrin/discordgo"
)

// The Component of the random package.
type Component struct {
randomBase
data discordgo.MessageComponentInteractionData
}

// Handle handles the functionality of a component.
func (c Component) Handle(s *discordgo.Session, i *discordgo.InteractionCreate) {
c.InteractionUtil = util.InteractionUtil{Session: s, Interaction: i}
c.member = i.Member
c.user = i.User
if i.Member != nil {
c.user = i.Member.User
} else if i.User != nil {
c.member = &discordgo.Member{User: i.User}
}
c.data = i.MessageComponentData()

ids := strings.Split(c.data.CustomID, ".")
// pop the first level identifier
util.ShiftL(ids)

switch util.ShiftL(ids) {
case "coin":
c.subcommandCoin().handleComponent(ids)
return
case "dice":
c.subcommandDice().handleComponent(ids)
return
case "teams":
c.subcommandTeams().handleComponent(ids)
return
default:
log.Printf("Unknown component interaction ID: %s", c.data.CustomID)
}

}

// ID returns the custom ID of the modal to identify the module
func (c Component) ID() string {
return "random"
}
5 changes: 4 additions & 1 deletion modules/random/handleSubcommandCoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ func (cmd subcommandCoin) handle() {
}

reflipButton := util.CreateButtonComponent(
"coin.reflip",
"random.coin.reflip",
"",
discordgo.PrimaryButton,
util.GetConfigComponentEmoji("random.coin.reflip"))
components := []discordgo.MessageComponent{discordgo.ActionsRow{Components: []discordgo.MessageComponent{reflipButton}}}

cmd.ReplyComponents(components, util.GetConfigEmoji("random.coin."+side).MessageFormat())
}

func (cmd subcommandCoin) handleComponent(ids []string) {
}
3 changes: 3 additions & 0 deletions modules/random/handleSubcommandDice.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,6 @@ func (cmd subcommandDice) handle() {

cmd.ReplyComponentsSimpleEmbedf(components, 0xFF7D00, lang.GetDefault(tp+"msg.dice.roll"), diceResult)
}

func (cmd subcommandDice) handleComponent(ids []string) {
}
3 changes: 3 additions & 0 deletions modules/random/handleSubcommandTeams.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,6 @@ func (cmd subcommandTeams) appCmd() *discordgo.ApplicationCommandOption {

func (cmd subcommandTeams) handle() {
}

func (cmd subcommandTeams) handleComponent(ids []string) {
}

0 comments on commit e43844b

Please sign in to comment.