-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (145 loc) · 3.94 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/kelseyhightower/envconfig"
"os"
"os/signal"
"syscall"
)
type Config struct {
BotToken string `split_words:"true" required:"true"`
GuildID string `split_words:"true" required:"true"`
}
type discordHandler struct {
config Config
}
func main() {
// Config
var config Config
err := envconfig.Process("bot", &config)
if err != nil {
fmt.Printf("Error with configuration: %s\n", err.Error())
os.Exit(1)
}
// Create discord session
discord, err := discordgo.New("Bot " + config.BotToken)
if err != nil {
fmt.Printf("Error creating Discord session: %s\n", err.Error())
os.Exit(1)
}
// Register callbacks
dh := &discordHandler{}
discord.AddHandler(dh.ready)
discord.AddHandler(dh.command)
// Open the websocket
err = discord.Open()
if err != nil {
fmt.Printf("Error opening Discord connection: %s\n", err.Error())
os.Exit(1)
}
// Register command
command := &discordgo.ApplicationCommand{
Name: "test",
Description: "A test command with subcommand-groups and subcommands",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "test-a",
Description: "Test-a sub-command group",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "test-a-a",
Description: "Test-a-a sub-command",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "test-a-b",
Description: "Test-a-b sub-command",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
},
{
Name: "test-b",
Description: "Test-b sub-command group",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "test-b-a",
Description: "Test-b-a sub-command",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
{
Name: "test-b-b",
Description: "Test-b-b sub-command",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
Type: discordgo.ApplicationCommandOptionSubCommandGroup,
},
{
Name: "test-c",
Description: "Test-c sub-command",
Type: discordgo.ApplicationCommandOptionSubCommand,
},
},
}
cmd, err := discord.ApplicationCommandCreate(discord.State.User.ID, config.GuildID, command)
if err != nil {
fmt.Printf("Error adding command: %s\n", err.Error())
}
// Block until we get ctrl-c
fmt.Println("Bot running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Clean up
fmt.Println("Bot exiting")
err = discord.ApplicationCommandDelete(discord.State.User.ID, config.GuildID, cmd.ID)
if err != nil {
fmt.Printf("Error removing command: %s\n", err.Error())
}
discord.Close()
}
func (dh *discordHandler) ready(s *discordgo.Session, m *discordgo.Ready) {
s.UpdateListeningStatus("/test")
}
func (dh *discordHandler) command(s *discordgo.Session, i *discordgo.InteractionCreate) {
switch i.ApplicationCommandData().Name {
case "test":
options := i.ApplicationCommandData().Options
response := ""
switch options[0].Name {
case "test-c":
response = "Test C Command"
case "test-a":
options := options[0].Options
switch options[0].Name {
case "test-a-a":
response = "Test A A Command"
case "test-a-b":
response = "Test A B Command"
default:
response = "Error!"
}
case "test-b":
options := options[0].Options
switch options[0].Name {
case "test-b-a":
response = "Test B A Command"
case "test-b-b":
response = "Test B B Command"
default:
response = "Error!"
}
default:
response = "Error!"
}
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: response,
},
})
}
}