-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscord.go
More file actions
85 lines (74 loc) · 2.46 KB
/
Copy pathdiscord.go
File metadata and controls
85 lines (74 loc) · 2.46 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"strings"
"github.com/bwmarrin/discordgo"
"github.com/charmbracelet/log"
)
var s *discordgo.Session
func setupDiscord(cfg config, removeCommands bool) {
dg, err := discordgo.New(cfg.DiscordToken)
if err != nil {
log.Fatal("Failed to create Discord session", "err", err)
}
s = dg
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Info("Authenticated with Discord", "user", s.State.User.Username)
if removeCommands {
_, err := s.ApplicationCommandBulkOverwrite(s.State.User.ID,
cfg.GuildId, []*discordgo.ApplicationCommand{})
if err != nil {
log.Error("Failed to remove commands", "err", err)
}
}
_, err := s.ApplicationCommandCreate(s.State.User.ID, cfg.GuildId, &discordgo.ApplicationCommand{
Name: loc("cmd.packs.title"),
Description: loc("cmd.packs.description"),
DMPermission: pointer(false),
})
if err != nil {
log.Error("Failed to create slash command", "err", err)
}
})
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.Type {
case discordgo.InteractionApplicationCommand:
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i, cfg)
return
}
log.Warn("Failed to handle interaction", "ref", i.ApplicationCommandData().Name)
case discordgo.InteractionMessageComponent:
if h, ok := commandHandlers[strings.Split(
i.MessageComponentData().CustomID, "/")[0]+"[comp]"]; ok {
h(s, i, cfg)
return
}
log.Warn("Failed to handle interaction", "ref", i.MessageComponentData().CustomID)
case discordgo.InteractionModalSubmit:
if h, ok := commandHandlers[strings.Split(
i.ModalSubmitData().CustomID, "/")[0]+"[modal]"]; ok {
h(s, i, cfg)
return
}
log.Warn("Failed to handle interaction", "ref", i.ModalSubmitData().CustomID)
default:
log.Error("Unhandled interaction type", "type", i.Type)
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Flags: discordgo.MessageFlagsEphemeral,
Content: loc("generic.failed"),
},
})
})
err = s.Open()
if err != nil {
log.Fatal("Failed to open Discord session", "err", err)
}
}
var commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate, cfg config){
loc("cmd.packs.title"): dMainCommand,
"serverSelect[comp]": dServerSelect,
"switchTo[comp]": dSwitchTo,
}