This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
appcmd_manager.go
68 lines (57 loc) · 1.82 KB
/
appcmd_manager.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package appcmdmanager
import (
"github.com/bwmarrin/discordgo"
"log"
"sync"
)
type AppCmdInterface interface {
ApplicationCommandStruct() *discordgo.ApplicationCommand
ApplicationCommandHandler(s *discordgo.Session, i *discordgo.InteractionCreate)
MessageComponentHandler() map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
ModalSubmitHandler() map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate)
}
func RegisterApplicationCommands(cmds []AppCmdInterface, s *discordgo.Session, guildID string) (appCmds []*discordgo.ApplicationCommand) {
var (
mutex sync.Mutex
wg sync.WaitGroup
)
for _, command := range cmds {
wg.Add(1)
go func(cmd AppCmdInterface) {
appCmd, err := s.ApplicationCommandCreate(s.State.User.ID, guildID, cmd.ApplicationCommandStruct())
if err != nil {
log.Panicf("Cannot create '%v' command: %v", cmd.ApplicationCommandStruct().Name, err)
}
mutex.Lock()
appCmds = append(appCmds, appCmd)
mutex.Unlock()
wg.Done()
}(command)
}
wg.Wait()
return appCmds
}
func RegisterHandler(apps []AppCmdInterface) func(s *discordgo.Session, i *discordgo.InteractionCreate) {
return func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
for _, app := range apps {
if app.ApplicationCommandStruct().Name == i.ApplicationCommandData().Name {
app.ApplicationCommandHandler(s, i)
}
}
case discordgo.InteractionMessageComponent:
for _, app := range apps {
if handler, ok := app.MessageComponentHandler()[i.MessageComponentData().CustomID]; ok {
handler(s, i)
}
}
case discordgo.InteractionModalSubmit:
for _, app := range apps {
if handler, ok := app.ModalSubmitHandler()[i.ModalSubmitData().CustomID]; ok {
handler(s, i)
}
}
}
}
}