-
Notifications
You must be signed in to change notification settings - Fork 0
/
modals.go
51 lines (39 loc) · 1.5 KB
/
modals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package minidis
import (
"errors"
"github.com/bwmarrin/discordgo"
)
type ModalInteractionProps struct {
ID string
Execute func(s *SlashContext, c *ModalSubmitContext) error
}
// AddModalSubmitHandler adds a new modal submit interaction handler for a specific custom id.
func (m *Minidis) AddModalSubmitHandler(mi *ModalInteractionProps) {
m.modalSubmitHandlers[mi.ID] = mi
}
// AddCustomModalSubmitHandler is a fallback handler for AddModalSubmitHandler.
// This is useful if you have custom mod ids that are changing or unique.
// This is called if the component's id has no handler set.
func (m *Minidis) AddCustomModalSubmitHandler(handler func(s *SlashContext, c *ModalSubmitContext) error) {
m.customModalSubmitHandler = handler
}
type ModalSubmitContext struct {
Data discordgo.ModalSubmitInteractionData
}
func (m *Minidis) NewModalContext(data discordgo.ModalSubmitInteractionData) *ModalSubmitContext {
return &ModalSubmitContext{
Data: data,
}
}
func (m *Minidis) executeModalSubmit(session *discordgo.Session, event *discordgo.Interaction) error {
data := event.ModalSubmitData()
slashContext := m.NewSlashContext(session, event, false)
modalContext := m.NewModalContext(data)
if handler, ok := m.modalSubmitHandlers[data.CustomID]; ok {
return handler.Execute(slashContext, modalContext)
}
if m.customModalSubmitHandler != nil {
return m.customModalSubmitHandler(slashContext, modalContext)
}
return errors.New("no modal submit handler defined for custom id: " + data.CustomID)
}