-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
133 lines (113 loc) · 3.68 KB
/
main.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"context"
"log"
"os"
"sync"
"github.com/Karitham/corde"
)
var command = corde.NewSlashCommand(
"cmd",
"edit and view existing slash commands",
corde.NewSubcommand("list", "list existing slash commands"),
)
func main() {
token := os.Getenv("DISCORD_BOT_TOKEN")
if token == "" {
log.Fatalln("DISCORD_BOT_TOKEN not set")
}
appID := corde.SnowflakeFromString(os.Getenv("DISCORD_APP_ID"))
if appID == 0 {
log.Fatalln("DISCORD_APP_ID not set")
}
pk := os.Getenv("DISCORD_PUBLIC_KEY")
if pk == "" {
log.Fatalln("DISCORD_PUBLIC_KEY not set")
}
g := corde.GuildOpt(corde.SnowflakeFromString(os.Getenv("DISCORD_GUILD_ID")))
mu := &sync.Mutex{}
selectedID := 0
m := corde.NewMux(pk, appID, token)
m.Route("cmd", func(m *corde.Mux) {
m.Route("list", func(m *corde.Mux) {
m.SlashCommand("", list(m, g))
m.ButtonComponent("next", btnNext(m, g, mu, &selectedID))
m.ButtonComponent("remove", btnRemove(m, g, mu, &selectedID))
})
})
if err := m.RegisterCommand(command, g); err != nil {
log.Fatalln("error registering command: ", err)
}
log.Println("serving on :8070")
if err := m.ListenAndServe(":8070"); err != nil {
log.Fatalln(err)
}
}
var nextBtn = corde.Component{
Type: corde.COMPONENT_BUTTON,
CustomID: "cmd/list/next",
Style: corde.BUTTON_SECONDARY,
Label: "Next",
Emoji: &corde.Emoji{Name: "➡️"},
}
var delBtn = corde.Component{
Type: corde.COMPONENT_BUTTON,
CustomID: "cmd/list/remove",
Style: corde.BUTTON_DANGER,
Label: "Delete",
Emoji: &corde.Emoji{Name: "🗑️"},
}
func list(m *corde.Mux, g func(*corde.CommandsOpt)) func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.SlashCommandInteractionData]) {
return func(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.SlashCommandInteractionData]) {
w.Respond(corde.NewResp().
ActionRow(nextBtn).
Ephemeral().
Content("Click on the buttons to move between existing commands and or delete them."),
)
}
}
func btnNext(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.ButtonInteractionData]) {
return func(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.ButtonInteractionData]) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
if err != nil {
w.Update(corde.NewResp().Contentf("Error getting commands: %s", err.Error()).Ephemeral())
return
}
if len(commands) == 0 {
w.Update(corde.NewResp().Content("No commands found.").Ephemeral())
return
}
*selectedID = (*selectedID + 1) % len(commands)
w.Update(corde.NewResp().
Contentf("%s - %s", commands[*selectedID].Name, commands[*selectedID].Description).
ActionRow(nextBtn, delBtn).
Ephemeral(),
)
}
}
func btnRemove(m *corde.Mux, g func(*corde.CommandsOpt), mu *sync.Mutex, selectedID *int) func(context.Context, corde.ResponseWriter, *corde.Interaction[corde.ButtonInteractionData]) {
return func(ctx context.Context, w corde.ResponseWriter, _ *corde.Interaction[corde.ButtonInteractionData]) {
mu.Lock()
defer mu.Unlock()
commands, err := m.GetCommands(g)
if err != nil {
w.Update(corde.NewResp().Contentf("Error getting commands: %s", err.Error()).Ephemeral())
return
}
c := commands[*selectedID]
m.DeleteCommand(c.ID, g)
commands, _ = m.GetCommands(g)
if len(commands) == 0 {
w.Update(corde.NewResp().Content("No commands found.").Ephemeral())
return
}
*selectedID = (*selectedID + 1) % len(commands)
w.Update(corde.NewResp().
Contentf("%s - %s", commands[*selectedID].Name, commands[*selectedID].Description).
ActionRow(nextBtn, delBtn).
Ephemeral(),
)
}
}