This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
generated from NamVr/DiscordBot-Template
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot.js
286 lines (230 loc) · 9.07 KB
/
bot.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
/**
* @file Main File of the bot, responsible for registering events, commands, interactions etc.
* @author Naman Vrati
* @version 3.0.0
*/
// Declare constants which will be used throughout the bot.
const fs = require("fs");
const { Client, Collection, Intents } = require("discord.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const { token, client_id, test_guild_id } = require("./config.json");
const DisTube = require('distube')
const SoundCloudPlugin = require('@distube/soundcloud')
const SpotifyPlugin = require('@distube/spotify')
/**
* From v13, specifying the intents is compulsory.
* @type {Object}
* @description Main Application Client */
const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES],
});
/**********************************************************************/
// Below we will be making an event handler!
/**
* @description All event files of the event handler.
* @type {String[]}
*/
const eventFiles = fs
.readdirSync("./events")
.filter((file) => file.endsWith(".js"));
// Loop through all files and execute the event when it is actually emmited.
for (const file of eventFiles) {
const event = require(`./events/${file}`);
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client));
} else {
client.on(
event.name,
async (...args) => await event.execute(...args, client)
);
}
}
/**********************************************************************/
// Define Collection of Commands, Slash Commands and cooldowns
client.commands = new Collection();
client.slashCommands = new Collection();
client.buttonCommands = new Collection();
client.selectCommands = new Collection();
client.contextCommands = new Collection();
client.cooldowns = new Collection();
client.triggers = new Collection();
client.distube = new DisTube.default(client, {
searchSongs: 1,
searchCooldown: 30,
leaveOnEmpty: true,
emptyCooldown: 0,
leaveOnFinish: true,
leaveOnStop: true,
plugins: [new SoundCloudPlugin.default(), new SpotifyPlugin.default()],
ytdlOptions: {
quality: "highestaudio",
filter: "audioonly",
quality: "highestaudio",
audioBitrate: 240000,
averageBitrate: 240000,
audioQuality: 'AUDIO_QUALITY_MEDIUM',
}
})
/**********************************************************************/
// Registration of Message-Based Commands
/**
* @type {String[]}
* @description All command categories aka folders.
*/
const commandFolders = fs.readdirSync("./commands");
// Loop through all files and store commands in commands collection.
for (const folder of commandFolders) {
if (folder==".DS_Store") {continue;}
const commandFiles = fs
.readdirSync(`./commands/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(`./commands/${folder}/${file}`);
client.commands.set(command.name, command);
}
}
/**********************************************************************/
// Registration of Slash-Command Interactions.
/**
* @type {String[]}
* @description All slash commands.
*/
const slashCommands = fs.readdirSync("./interactions/slash");
// Loop through all files and store slash-commands in slashCommands collection.
for (const module of slashCommands) {
const commandFiles = fs
.readdirSync(`./interactions/slash/${module}`)
.filter((file) => file.endsWith(".js"));
for (const commandFile of commandFiles) {
const command = require(`./interactions/slash/${module}/${commandFile}`);
client.slashCommands.set(command.data.name, command);
}
}
/**********************************************************************/
// Registration of Context-Menu Interactions
/**
* @type {String[]}
* @description All Context Menu commands.
*/
const contextMenus = fs.readdirSync("./interactions/context-menus");
// Loop through all files and store slash-commands in slashCommands collection.
for (const folder of contextMenus) {
if (folder==".DS_Store") {continue;}
const files = fs
.readdirSync(`./interactions/context-menus/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of files) {
if (folder==".DS_Store") {continue;}
const menu = require(`./interactions/context-menus/${folder}/${file}`);
const keyName = `${folder.toUpperCase()} ${menu.data.name}`;
client.contextCommands.set(keyName, menu);
}
}
/**********************************************************************/
// Registration of Button-Command Interactions.
/**
* @type {String[]}
* @description All button commands.
*/
const buttonCommands = fs.readdirSync("./interactions/buttons");
// Loop through all files and store button-commands in buttonCommands collection.
for (const module of buttonCommands) {
const commandFiles = fs
.readdirSync(`./interactions/buttons/${module}`)
.filter((file) => file.endsWith(".js"));
for (const commandFile of commandFiles) {
const command = require(`./interactions/buttons/${module}/${commandFile}`);
client.buttonCommands.set(command.id, command);
}
}
/**********************************************************************/
// Registration of select-menus Interactions
/**
* @type {String[]}
* @description All Select Menu commands.
*/
const selectMenus = fs.readdirSync("./interactions/select-menus");
// Loop through all files and store select-menus in slashCommands collection.
for (const module of selectMenus) {
const commandFiles = fs
.readdirSync(`./interactions/select-menus/${module}`)
.filter((file) => file.endsWith(".js"));
for (const commandFile of commandFiles) {
const command = require(`./interactions/select-menus/${module}/${commandFile}`);
client.selectCommands.set(command.id, command);
}
}
/**********************************************************************/
// Registration of Slash-Commands in Discord API
const rest = new REST({ version: "9" }).setToken(token);
const commandJsonData = [
...Array.from(client.slashCommands.values()).map((c) => c.data.toJSON()),
...Array.from(client.contextCommands.values()).map((c) => c.data),
];
(async () => {
try {
console.log("Started refreshing application (/) commands.");
await rest.put(
/**
* Here we are sending to discord our slash commands to be registered.
There are 2 types of commands, guild commands and global commands.
Guild commands are for specific guilds and global ones are for all.
In development, you should use guild commands as guild commands update
instantly, whereas global commands take upto 1 hour to be published. To
deploy commands globally, replace the line below with:
Routes.applicationCommands(client_id)
*/
Routes.applicationGuildCommands(client_id, test_guild_id),
{ body: commandJsonData }
);
console.log("Successfully reloaded application (/) commands.");
} catch (error) {
console.error(error);
}
})();
/**********************************************************************/
// Registration of Message Based Chat Triggers
/**
* @type {String[]}
* @description All trigger categories aka folders.
*/
const triggerFolders = fs.readdirSync("./triggers");
// Loop through all files and store commands in commands collection.
for (const folder of triggerFolders) {
const triggerFiles = fs
.readdirSync(`./triggers/${folder}`)
.filter((file) => file.endsWith(".js"));
for (const file of triggerFiles) {
const trigger = require(`./triggers/${folder}/${file}`);
client.triggers.set(trigger.name, trigger);
}
}
// Distube initialization.
const status = queue => `Volume: \`${queue.volume}%\` | Filter: \`${queue.filters.join(", ") || "Off"}\` | Loop: \`${queue.repeatMode ? queue.repeatMode === 2 ? "All Queue" : "This Song" : "Off"}\` | Autoplay: \`${queue.autoplay ? "On" : "Off"}\``
client.distube
.on("playSong", (queue, song) => queue.textChannel.send(
`Playing \`${song.name}\` - \`${song.formattedDuration}\`\nRequested by: ${song.user}\n${status(queue)}`
))
.on("addSong", (queue, song) => queue.textChannel.send(
`Added ${song.name} - \`${song.formattedDuration}\` to the queue by ${song.user}`
))
.on("addList", (queue, playlist) => queue.textChannel.send(
`Added \`${playlist.name}\` playlist (${playlist.songs.length} songs) to queue\n${status(queue)}`
))
// DisTubeOptions.searchSongs = true
.on("searchResult", (message, result) => {
let i = 0
message.channel.send(`**Choose an option from below**\n${result.map(song => `**${++i}**. ${song.name} - \`${song.formattedDuration}\``).join("\n")}\n*Enter anything else or wait 60 seconds to cancel*`)
})
// DisTubeOptions.searchSongs = true
.on("searchCancel", message => message.channel.send(`Searching canceled`))
.on("error", (channel, e) => {
channel.send(`An error encountered: ${e}`)
console.error(e)
})
.on("empty", channel => channel.send("Voice channel is empty! Leaving the channel..."))
.on("searchNoResult", message => message.channel.send(`No result found!`))
.on("finish", queue => queue.textChannel.send("Finished!"))
// Login into your client application with bot's token.
client.login(token);