Skip to content

Commit

Permalink
added modal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Kesuaheli committed Nov 18, 2024
1 parent a381177 commit d519f10
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
7 changes: 7 additions & 0 deletions event/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package event
import (
"cake4everybot/event/command"
"cake4everybot/event/component"
"cake4everybot/event/modal"
"strings"

"github.com/bwmarrin/discordgo"
Expand All @@ -42,5 +43,11 @@ func handleInteractionCreate(s *discordgo.Session, i *discordgo.InteractionCreat
} else {
log.Printf("got component interaction from unknown module '%s' (full id '%s')", strings.Split(data.CustomID, ".")[0], data.CustomID)
}

case discordgo.InteractionModalSubmit:
data := i.ModalSubmitData()
if m, ok := modal.ModalMap[strings.Split(data.CustomID, ".")[0]]; ok {
m.HandleModal(s, i)
}
}
}
2 changes: 2 additions & 0 deletions event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package event
import (
"cake4everybot/event/command"
"cake4everybot/event/component"
"cake4everybot/event/modal"
"cake4everybot/event/twitch"
logger "log"

Expand All @@ -33,6 +34,7 @@ func PostRegister(dc *discordgo.Session, t *twitchgo.Twitch, guildID string) err
return err
}
component.Register()
modal.Register()

twitch.Register(t)

Expand Down
42 changes: 42 additions & 0 deletions event/modal/componentBase.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package modal

import (
"cake4everybot/modules/secretsanta"
"log"

"github.com/bwmarrin/discordgo"
)

// Modal is an interface wrapper for all message components.
type Modal interface {
// Function of a component.
// All things that should happen after submitting a modal.
HandleModal(*discordgo.Session, *discordgo.InteractionCreate)

// Custom ID of the modal to identify the module
ID() string
}

// ModalMap holds all active modals. It maps them from a unique string identifier to the
// corresponding [Modal].
var ModalMap = make(map[string]Modal)

// Register registers modals
func Register() {
// This is the list of modals to use. Add a modal via
// simply appending the struct (which must implement the
// [Modal] interface) to the list, e.g.:
//
// ModalList = append(ModalList, mymodule.MyComponent{})
var ModalList []Modal

ModalList = append(ModalList, secretsanta.Component{})

if len(ModalList) == 0 {
return
}
for _, c := range ModalList {
ModalMap[c.ID()] = c
}
log.Printf("Added %d modal handler(s)!", len(ModalMap))
}
27 changes: 26 additions & 1 deletion modules/secretsanta/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
// The Component of the secret santa package.
type Component struct {
secretSantaBase
data discordgo.MessageComponentInteractionData
data discordgo.MessageComponentInteractionData
modal discordgo.ModalSubmitInteractionData
}

// Handle handles the functionality of a component.
Expand All @@ -35,12 +36,36 @@ func (c Component) Handle(s *discordgo.Session, i *discordgo.InteractionCreate)
return
case "invite":
c.handleInvite(ids)
return
default:
log.Printf("Unknown component interaction ID: %s", c.data.CustomID)
}

}

// HandleModal handles the functionality of a modal.
func (c Component) HandleModal(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}
}
//lint:ignore SA4005 assignment to c.modal is intentional
c.modal = i.ModalSubmitData()

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

switch util.ShiftL(ids) {
default:
log.Printf("Unknown modal submit ID: %s", c.modal.CustomID)
}
}

// ID returns the custom ID of the modal to identify the module
func (c Component) ID() string {
return "secretsanta"
Expand Down
2 changes: 1 addition & 1 deletion modules/secretsanta/handleComponentInvite.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (c Component) handleInviteSetAddress(ids []string) {
return
}

c.ReplyModal("secretsanta.invite.set_address_modal."+c.Interaction.GuildID, lang.GetDefault(tp+"msg.invite.modal.set_address.title"), discordgo.ActionsRow{Components: []discordgo.MessageComponent{
c.ReplyModal("secretsanta.set_address."+c.Interaction.GuildID, lang.GetDefault(tp+"msg.invite.modal.set_address.title"), discordgo.ActionsRow{Components: []discordgo.MessageComponent{
discordgo.TextInput{
CustomID: "address",
Label: lang.GetDefault(tp + "msg.invite.modal.set_address.label"),
Expand Down

0 comments on commit d519f10

Please sign in to comment.