-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
295 lines (267 loc) · 8.15 KB
/
client.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"context"
"fmt"
"os"
"path/filepath"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/gateway"
"github.com/disgoorg/disgo/rest"
"github.com/disgoorg/disgolink/v3/disgolink"
)
type Client struct {
Bot bot.Client
Rest rest.Rest
Lavalink disgolink.Client
}
func NewClient() error {
assets, err := os.ReadDir("./assets")
if err != nil {
return err
}
choices := make([]discord.ApplicationCommandOptionChoiceString, 0, 25)
for _, asset := range assets {
name := asset.Name()
name = name[:len(name)-len(filepath.Ext(name))]
ASSETS_PATHS[name] = fmt.Sprintf("./assets/%s", asset.Name())
ASSETS = append(ASSETS, name)
choices = append(choices, discord.ApplicationCommandOptionChoiceString{
Name: name,
Value: name,
})
}
for i, command := range COMMANDS {
if command.CommandName() == "joel" {
COMMANDS[i].(discord.SlashCommandCreate).Options[0] = discord.ApplicationCommandOptionString{
Name: "joel",
Description: "JOEL",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Posts a random or specific JOEL",
discord.LocalePortugueseBR: "Posta um JOEL aleatório ou um específico",
},
Choices: choices,
Required: false,
}
break
}
}
bot, err := disgo.New(CONFIG.Token,
bot.WithGatewayConfigOpts(
gateway.WithIntents(
gateway.IntentGuilds,
gateway.IntentGuildMessages,
gateway.IntentDirectMessages,
gateway.IntentMessageContent,
gateway.IntentGuildVoiceStates,
),
gateway.WithPresenceOpts(
gateway.WithPlayingActivity("/help"),
gateway.WithOnlineStatus(discord.OnlineStatusDND),
),
),
bot.WithEventListenerFunc(onReady),
bot.WithEventListenerFunc(onMessageCreate),
bot.WithEventListenerFunc(onVoiceStateUpdate),
bot.WithEventListenerFunc(onVoiceServerUpdate),
bot.WithEventListenerFunc(commandListener),
)
if err != nil {
return err
}
fmt.Println("Connecting to Discord")
err = bot.OpenGateway(context.TODO())
if err != nil {
return err
}
// Wait for ready
<-READY
APPLICATION_ID := bot.ApplicationID()
BOT_ID := bot.ID()
MENTION = fmt.Sprintf("<@%s>", APPLICATION_ID)
if *REGISTER {
cmds, err := bot.Rest().SetGlobalCommands(APPLICATION_ID, COMMANDS)
if err != nil {
fmt.Printf("Error setting global commands: %v\n", err)
panic(err)
}
fmt.Printf("Registered %d global commands\n", len(cmds))
}
MENTION = fmt.Sprintf("<@%s>", BOT_ID)
fmt.Println("Connecting to Lavalink")
lavalink := disgolink.New(BOT_ID, disgolink.WithListenerFunc(onTrackEnd))
_, err = lavalink.AddNode(context.TODO(), disgolink.NodeConfig{
Name: "joel",
Address: "0.0.0.0:2333",
})
if err != nil {
return err
}
CLIENT = Client{
Bot: bot,
Lavalink: lavalink,
Rest: bot.Rest(),
}
return nil
}
var (
COMMANDS = []discord.ApplicationCommandCreate{
discord.SlashCommandCreate{
Name: "help",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "help",
discord.LocalePortugueseBR: "ajuda",
},
Description: "Displays the help message",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Displays the help message",
discord.LocalePortugueseBR: "Exibe a mensagem de ajuda",
},
},
discord.SlashCommandCreate{
Name: "joel",
Description: "JOEL",
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "",
Description: "",
DescriptionLocalizations: map[discord.Locale]string{},
Choices: []discord.ApplicationCommandOptionChoiceString{},
Required: false,
},
},
},
discord.SlashCommandCreate{
Name: "ttj",
Description: "Latency test",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Latency test",
discord.LocalePortugueseBR: "Teste de latência",
},
},
discord.SlashCommandCreate{
Name: "play",
Description: "Plays a track",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Plays a track",
discord.LocalePortugueseBR: "Toca uma música",
},
Options: []discord.ApplicationCommandOption{
discord.ApplicationCommandOptionString{
Name: "query",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "query",
discord.LocalePortugueseBR: "pesquisa",
},
Description: "Can be an URL or a search query",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Can be an URL or a search query",
discord.LocalePortugueseBR: "Pode ser uma URL ou uma pesquisa",
},
Required: true,
},
},
},
discord.SlashCommandCreate{
Name: "stop",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "stop",
discord.LocalePortugueseBR: "parar",
},
Description: "Stops the current track",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Stops the current track",
discord.LocalePortugueseBR: "Para a música atual",
},
},
discord.SlashCommandCreate{
Name: "pause",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "pause",
discord.LocalePortugueseBR: "pausar",
},
Description: "Pauses the current track",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Pauses the current track",
discord.LocalePortugueseBR: "Pausa a música atual",
},
},
discord.SlashCommandCreate{
Name: "resume",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "resume",
discord.LocalePortugueseBR: "resumir",
},
Description: "Resumes the current track",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Resumes the current track",
discord.LocalePortugueseBR: "Resume a música atual",
},
},
discord.SlashCommandCreate{
Name: "skip",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "skip",
discord.LocalePortugueseBR: "pular",
},
Description: "Skips the current track",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Skips the current track",
discord.LocalePortugueseBR: "Pula a música atual",
},
},
discord.SlashCommandCreate{
Name: "join",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "join",
discord.LocalePortugueseBR: "entrar",
},
Description: "Joins the voice channel",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Joins the voice channel",
discord.LocalePortugueseBR: "Entra no canal de voz",
},
},
discord.SlashCommandCreate{
Name: "leave",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "leave",
discord.LocalePortugueseBR: "sair",
},
Description: "Leaves the voice channel",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Leaves the voice channel",
discord.LocalePortugueseBR: "Sai do canal de voz",
},
},
discord.SlashCommandCreate{
Name: "queue",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "queue",
discord.LocalePortugueseBR: "fila",
},
Description: "Displays the queue",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Displays the music queue",
discord.LocalePortugueseBR: "Exibe a fila de músicas",
},
},
discord.SlashCommandCreate{
Name: "playing",
NameLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "playing",
discord.LocalePortugueseBR: "tocando",
},
Description: "Displays the currently playing track",
DescriptionLocalizations: map[discord.Locale]string{
discord.LocaleEnglishUS: "Displays the currently playing track",
discord.LocalePortugueseBR: "Exibe a música atual",
},
},
discord.SlashCommandCreate{
Name: "reset",
Description: "Resets your chat history with the bot",
},
}
)