-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
416 lines (361 loc) · 13.2 KB
/
index.js
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"use strict";
import discord from "discord.js";
import * as fs from "fs";
import {
make_simple_embed,
is_same_vc_as,
leave_voice_channel,
post_stats,
is_voted,
create_yt_data_from_playdl_data, make_playing_embed, get_control_button_row
} from "./utils/utils.js";
import { any_audio_playing, stop_audio, pause_audio, play_audio } from "./utils/audio.js";
import topgg from "@top-gg/sdk";
import * as voice from "@discordjs/voice";
const token = process.env.DISCORD_TOKEN;
export const client = new discord.Client({
intents: [
discord.GatewayIntentBits.Guilds,
discord.GatewayIntentBits.GuildVoiceStates,
],
});
const webhook_client = new discord.WebhookClient({ url: process.env.WEBHOOK_URL });
const prefix = "!";
const is_maintenance = process.env.MAINTENANCE === "true" || false;
client.streams = new discord.Collection();
client.commands = new discord.Collection();
client.topgg_api = new topgg.Api(process.env.TOPGG_TOKEN);
client.login(token).catch((e) => {
console.error("The bot token was incorrect.\n" + e);
});
client.once(discord.Events.ClientReady, async () => {
console.log("Loading commands...");
const command_files = fs.readdirSync("./commands").filter((file) => file.endsWith(".js"));
for await (const file of command_files) {
const { data, execute } = await import(`./commands/${file}`);
client.commands.set(data.name, execute);
//console.log("Loaded command: " + data.name);
}
console.log("Loaded " + client.commands.size + " commands!\n");
console.log("Bot is ready!\n");
});
client.on(discord.Events.VoiceStateUpdate, (oldState, newState) => {
try {
// Check if the bot was kicked from a voice channel
if (oldState.member.user.id === client.user.id && oldState.channel && !newState.channel) {
// Check if the bot is in a voice channel
if (client.streams.has(oldState.guild.id)) {
// Stop the audio
stop_audio(oldState.guild.id);
client.streams.delete(oldState.guild.id);
const conn = voice.getVoiceConnection(oldState.guild.id);
if (conn?.state.status !== voice.VoiceConnectionStatus.Destroyed) {
conn?.destroy();
}
//console.log("Stopped audio in guild with ID " + oldState.guild.id + " because I was kicked from the voice channel.");
return;
}
}
// Leave the voice channel if the bot is the only one in it
if (oldState.channel) {
if (oldState.channel.members.size === 1 && oldState.channel.members.first().user.id === client.user.id) {
const guild_stream = client.streams.get(oldState.guild.id);
if (guild_stream !== undefined) {
clearTimeout(guild_stream.leave_timeout_id);
guild_stream.leave_timeout_id = setTimeout(async () => {
if (!oldState?.channel?.id || !oldState?.guild?.id) {
return;
}
const channel = await client.channels.fetch(oldState.channel.id);
if (channel && channel.members.size === 1) {
leave_voice_channel(oldState.guild.id);
}
}, 30000);
}
}
}
} catch (e) {
//console.log("An error occurred!");
console.log(e);
}
});
client.on(discord.Events.MessageCreate, async (message) => {
try {
if (message.author.bot) return;
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/);
switch (args.shift().toLowerCase()) {
case "servers":
if (message.author.id === "548120702373593090") {
await message.reply({
embeds: [make_simple_embed("Servers: (" + client.guilds.cache.size + ")\n - " + [...client.guilds.cache].join("\n - "))],
allowedMentions: { repliedUser: false },
});
}
break;
}
}
} catch (e) {
console.log(e);
}
});
client.on(discord.Events.GuildCreate, async (guild) => {
await webhook_client.send({
username: 'broki\'s music bot',
embeds: [
make_simple_embed("I was added to a new server: **" + guild.name + "**! (total servers: " + client.guilds.cache.size + ")")
],
});
post_stats();
});
client.on(discord.Events.GuildDelete, async (guild) => {
await webhook_client.send({
username: 'broki\'s music bot',
embeds: [
make_simple_embed("I was removed from a server: **" + guild.name + "**! (total servers: " + client.guilds.cache.size + ")")
],
});
post_stats();
});
client.on(discord.Events.InteractionCreate, async (interaction) => {
try {
// Check if the interaction is valid
if (interaction.replied || interaction.deferred) {
console.log("Invalid interaction! (replied: " + interaction.replied + ", deferred: " + interaction.deferred + ", channel: " + interaction.channel + ")");
return;
}
if (interaction.isChatInputCommand()) {
await handleChatInputCommand(interaction);
} else if (interaction.isButton()) {
await handleButton(interaction);
}
} catch (e) {
console.log(e);
}
});
async function handleChatInputCommand(interaction) {
if (!interaction.channel) {
await interaction.reply({
embeds: [make_simple_embed("You must be in a server to use this command!")],
ephemeral: true,
});
return;
}
if (is_maintenance && interaction.user.id !== "548120702373593090") {
await interaction.reply({
embeds: [make_simple_embed("This bot is currently in maintenance mode. Please try again later.")],
});
return;
}
const execute = client.commands.get(interaction.commandName);
if (!execute) {
await interaction.reply({
embeds: [make_simple_embed("There was an error while executing this command!")],
});
return;
}
// check if the bot has permission to send message to the channel
if (!interaction.guild.members.me?.permissionsIn(interaction.channel).has(discord.PermissionsBitField.Flags.SendMessages)) {
await interaction.reply({
embeds: [make_simple_embed("I don't have permission to send message to this channel!")],
ephemeral: true,
});
return;
}
await interaction.reply({ content: "..." });
try {
await execute(interaction);
} catch (error) {
console.error(error);
try {
await interaction.channel.send({
embeds: [make_simple_embed("There was an error while executing this command!")],
});
} catch (ignored) {
}
}
}
async function handleButton(interaction) {
switch (interaction.customId) {
case "pause":
if (!(await is_same_vc_as(interaction.user.id, interaction.guildId))) {
await interaction.reply({
embeds: [make_simple_embed("You are not in the same voice channel!")],
ephemeral: true
});
return;
}
if (!any_audio_playing(interaction.guildId)) {
await interaction.reply({
embeds: [make_simple_embed("No audio is currently playing")],
ephemeral: true,
});
return;
}
if (pause_audio(interaction.guildId) === 0) {
await interaction.reply({
embeds: [
make_simple_embed("The currently playing audio has been successfully **resumed**").setFooter({
text: "by " + interaction.user.username + "#" + interaction.user.discriminator,
iconURL: interaction.user.displayAvatarURL({ size: 16 }),
}),
],
});
} else {
await interaction.reply({
embeds: [
make_simple_embed("The currently playing audio has been successfully **paused**").setFooter({
text: "by " + interaction.user.username + "#" + interaction.user.discriminator,
iconURL: interaction.user.displayAvatarURL({ size: 16 }),
}),
]
});
}
break;
case "stop":
if (!(await is_same_vc_as(interaction.user.id, interaction.guildId))) {
await interaction.reply({
embeds: [make_simple_embed("You are not in the same voice channel!")],
ephemeral: true
});
return;
}
if (!any_audio_playing(interaction.guildId)) {
await interaction.reply({
embeds: [make_simple_embed("No audio is currently playing")],
ephemeral: true,
});
return;
}
stop_audio(interaction.guildId);
await interaction.reply({
embeds: [
make_simple_embed("YouTube audio successfully stopped!").setFooter({
text: "by " + interaction.user.username + "#" + interaction.user.discriminator,
iconURL: interaction.user.displayAvatarURL({ size: 16 }),
}),
]
});
break;
case "loop":
if (!(await is_same_vc_as(interaction.user.id, interaction.guildId))) {
await interaction.reply({
embeds: [make_simple_embed("You are not in the same voice channel!")],
ephemeral: true
});
return;
}
if (!any_audio_playing(interaction.guildId)) {
await interaction.reply({
embeds: [make_simple_embed("No audio is currently playing")],
ephemeral: true,
});
return;
}
const guild_stream = client.streams.get(interaction.guildId);
guild_stream.loop = !guild_stream.loop;
await interaction.reply({
embeds: [
make_simple_embed(
guild_stream.loop ? "Loop successfully **enabled** for current audio" : "Loop successfully **disabled** for current audio"
).setFooter({
text: "by " + interaction.user.username + "#" + interaction.user.discriminator,
iconURL: interaction.user.displayAvatarURL({ size: 16 }),
}),
]
});
break;
default:
if (interaction.customId.startsWith("replay:")) {
const replay_url = interaction.customId.replaceAll("replay:", "");
const guild = client.guilds.cache.get(interaction.guildId);
const bot = guild.members.cache.get(client.user.id);
const user = guild.members.cache.get(interaction.member.id);
if (!user.voice.channel) {
await interaction.reply({
embeds: [make_simple_embed("You are not in a voice channel!")],
ephemeral: true,
});
return;
}
if (bot.voice.channel) {
if (!(await is_same_vc_as(interaction.member.id, interaction.guildId))) {
await interaction.reply({
embeds: [make_simple_embed("You are not in the same voice channel!")],
ephemeral: true
});
return;
}
}
await interaction.reply({
content: "..."
});
let message = null;
const guild_stream = client.streams.get(interaction.guildId);
if (guild_stream?.queue?.length >= 5) {
const timeoutId = setTimeout(async () => {
message = await interaction.channel.send({
embeds: [make_simple_embed("<a:loading:1032708714605592596> Loading...")],
});
}, 1500);
if (!(await is_voted(interaction.member.id))) {
clearTimeout(timeoutId);
const contents = {
embeds: [
make_simple_embed(
"Queue is full (max 5)! However, you can unlock more queue slots by [voting for the bot](https://top.gg/bot/961240507894353970/vote)! (You can vote every 12 hours)"
),
],
};
if (message) {
await message.edit(contents);
} else {
await interaction.channel.send(contents);
}
return;
}
clearTimeout(timeoutId);
if (guild_stream?.queue?.length >= 10) {
const contents = {
embeds: [make_simple_embed("Queue is full (max 10)!")],
};
if (message) {
await message.edit(contents);
} else {
await interaction.channel.send(contents);
}
return;
}
}
message = await interaction.channel.send({
embeds: [await make_simple_embed(`<a:loading:1032708714605592596> Replaying audio...`)],
allowedMentions: { repliedUser: false },
});
const stream_data = await play_audio(replay_url, interaction.guildId, interaction.member.voice.channelId);
if (stream_data === null) {
await message.edit({
embeds: [make_simple_embed("No results found!")],
});
return;
}
const yt_data = create_yt_data_from_playdl_data(stream_data)
if (guild_stream?.queue?.length >= 1) {
await message.edit({
embeds: [
await make_playing_embed(interaction.guildId, interaction.member, yt_data)
.setTitle(`Added to queue (#${guild_stream?.queue?.length})`)
.setColor(0x44DDBF),
],
allowedMentions: { repliedUser: false },
});
} else {
await message.edit({
embeds: [await make_playing_embed(interaction.guildId, interaction.member, yt_data)],
components: [get_control_button_row(yt_data.url)],
allowedMentions: { repliedUser: false },
});
}
}
}
}
//process.on('warning', e => console.warn(e.stack));