-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
310 lines (289 loc) · 11.5 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
const fs = require('fs');
const os = require('os');
let cpuStat = require("cpu-stat");
const {
REST
} = require('@discordjs/rest');
const {
Routes
} = require('discord-api-types/v9');
const {
Client,
Collection,
EmbedBuilder,
GatewayIntentBits,
ActivityType,
version
} = require('discord.js');
const configFile = (process.argv[2]) ? process.argv[2] : './config.json';
const {
token,
clientId,
guildIds
} = require(configFile);
const config = require(configFile);
const MusicPlayer = require("./discord-player.js");
const client = new Client({
shards: "auto",
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.MessageContent
],
disableMentions: 'everyone',
});
client.commands = new Map();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.data.name, command);
}
var DATA = {
queue: [],
loop: false,
loopSong: false,
volume: 1,
current: null, // {id: "", title: ""}
guildId: null,
speech: false,
status: "Idle"
};
var players = new Map();
async function buildSlashCommands() {
const commands = [];
client.commands.forEach(command => {
commands.push(command.data.toJSON());
});
const rest = new REST({
version: '10'
}).setToken(token);
try {
await rest.put(Routes.applicationCommands(client.user.id), {
body: commands,
});
console.log('Successfully registered slash commands globally!');
} catch (error) {
console.error('Error while registering slash commands globally:', error);
}
}
// Event: Ready
client.once('ready', async() => {
console.log(`Logged in as ${client.user.tag}`);
client.user.setPresence({
activities: [{
name: `/help`,
type: ActivityType.Listening
}],
});
updateStatus();
// Build and register slash commands globally on bot startup
await buildSlashCommands();
});
// Event: Interaction Create
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const command = client.commands.get(interaction.commandName);
if (!command) return;
try {
await execute(interaction.commandName, interaction);
} catch (error) {
console.error(error);
return interaction.reply({
content: 'There was an error while executing this command!',
ephemeral: true
});
}
});
function updateStatus() {
const customStatus = ["/play - The best high quality music bot.", "/help - The best high quality music bot."];
let currentIndex = 0;
setInterval(() => {
const status = customStatus[currentIndex];
client.user.setPresence({
activities: [{
name: status,
type: ActivityType.Listening
}],
});
currentIndex = (currentIndex + 1) % customStatus.length;
}, 180000); // 3 minutes in milliseconds
}
function createEmbed(description, color) {
const embed = new EmbedBuilder()
.setDescription(description)
.setColor(color)
.setTimestamp();
return embed;
}
async function execute(name, interaction) {
if (!players.has(interaction.guildId)) {
const musicPlayer = new MusicPlayer(client, configFile, DATA);
//musicPlayer.on("log", text => console.log("[" + interaction.guildId + "][Music Player][Log] " + text));
//musicPlayer.on("error", error => console.error("[" + interaction.guildId + "][Music Player][Error] " + error));
//musicPlayer.on("state", state => console.log("[" + interaction.guildId + "][Music Player][State Update] " + state));
players.set(interaction.guildId, musicPlayer);
}
try {
const musicPlayer = players.get(interaction.guildId);
switch (name) {
case "help":
const helpEmbed = new EmbedBuilder()
.setColor('#e9196c')
.setTitle('Available commands\n__For support, join our server:__\nhttps://discord.gg/3YABbWRP7z')
.setDescription(`/help - Show all available commands\n/clear-list - Clear the queue.\n/join - Will connect to your current voice channel.\n/leave - Leave the voice channel.\n/list- List the contents of the queue.\n/loop - Toggle the loop.\n/lyrics - Fetch the lyrics of the songs, that's playing at the moment.\n/np - Get the currently playing song.\n/pause - Pause the playback.\n/play - Will play some example music.\n/remove - Remove a specific item from the queue.\n/resume - Resume the playback.\n/shuffle - Shuffle the queue.\n/skip - Skip to the next song.\n/state - Sends a new message with the current state of the bot.\n/stats - Show stats bot.\n/toggle-speech - Enable/Disable the voice controls.`)
.setThumbnail(client.user.displayAvatarURL());
return interaction.reply({
embeds: [helpEmbed],
ephemeral: false
});
break;
case "join":
await interaction.deferReply();
let s = musicPlayer.join(interaction);
if (!s) {
const embed1 = createEmbed('Please join a voice channel first.', '#e9196c');
interaction.editReply({
embeds: [embed1],
ephemeral: true
});
return;
}
const embed2 = createEmbed('Joined', '#e9196c');
interaction.editReply({
embeds: [embed2]
});
break;
case "leave":
await interaction.deferReply();
var left = musicPlayer.leave(interaction);
if (left) {
const embed3 = createEmbed('Left the voice channel.', '#e9196c');
return interaction.editReply({
embeds: [embed3]
});
}
const embed4 = createEmbed("I'm not connected to a voice channel...", '#e9196c');
return interaction.editReply({
embeds: [embed4],
ephemeral: true
});
break;
case "play":
await musicPlayer.play(interaction);
break;
case "np":
await musicPlayer.nowPlaying(interaction);
break;
case "pause":
musicPlayer.pause();
const embed5 = createEmbed('Paused.', '#e9196c');
interaction.reply({
embeds: [embed5]
});
break;
case "resume":
musicPlayer.resume();
const embed6 = createEmbed('Resumed.', '#e9196c');
interaction.reply({
embeds: [embed6]
});
break;
case "skip":
musicPlayer.skip();
const embed7 = createEmbed('Skipped.', '#e9196c');
interaction.reply({
embeds: [embed7]
});
break;
case "list":
await musicPlayer.list(interaction);
break;
case "loop":
musicPlayer.loop(interaction);
break;
case "remove":
musicPlayer.remove(interaction);
break;
case "statemessage":
musicPlayer.stateDisplay(interaction);
break;
case "shuffle":
musicPlayer.shuffle(interaction);
break;
case "clear-list":
musicPlayer.clear(interaction);
break;
case "stats":
await interaction.deferReply({
ephemeral: false,
});
let uptime = await os.uptime();
let d = Math.floor(uptime / (3600 * 24));
let h = Math.floor(uptime % (3600 * 24) / 3600);
let m = Math.floor(uptime % 3600 / 60);
let sc = Math.floor(uptime % 60);
let dDisplay = d > 0 ? d + (d === 1 ? " day, " : " days, ") : "";
let hDisplay = h > 0 ? h + (h === 1 ? " hour, " : " hours, ") : "";
let mDisplay = m > 0 ? m + (m === 1 ? " minute, " : " minutes, ") : "";
let sDisplay = sc > 0 ? sc + (sc === 1 ? " second" : " seconds") : "";
let ccount = client.channels.cache.size;
let scount = client.guilds.cache.size;
let mcount = 0;
client.guilds.cache.forEach((guild) => {
mcount += guild.memberCount;
});
cpuStat.usagePercent(function(err, percent, seconds) {
if (err) {
return //console.log(err);
}
const embed = new EmbedBuilder()
.setDescription(`__**${client.user.username} Information**__`)
.setThumbnail(client.user.displayAvatarURL())
.addFields([{
name: "**Client**",
value: `\`\`\`Servers: ${scount}\nChannels: ${ccount}\nUsers: ${mcount}\`\`\``,
inline: false
}, {
name: "**CPU**",
value: `\`\`\`Cpu: ${os.cpus().map((i) => `${i.model}`)[0]}\nLoad: ${percent.toFixed(2)}%\nPlatform: ${os.platform()}\`\`\``, inline: false },
{ name: "**DISK**", value: `\`\`\`Disk Used: ${(process.memoryUsage().heapUsed / 1024 / 1024).toFixed(2)} / ${(os.totalmem() / 1024 / 1024).toFixed(2)} MB\`\`\``, inline: false },
{ name: "**Discord**", value: `\`\`\`Discord.js: v${version}\nNode: ${process.version}\nAPI websocket ping: ${Math.round(client.ws.ping)}ms\`\`\``, inline: false },
{ name: "**System**", value: `\`\`\`Uptime: ${dDisplay + hDisplay + mDisplay + sDisplay}\`\`\``, inline: true }
])
.setColor("#e9196c")
.setTimestamp(Date.now());
interaction.editReply({ embeds: [embed] });
});
break;
case "toggle-speech":
musicPlayer.toggleSpeech(interaction);
break;
case "lyrics":
if (config.disableLyrics) {
const embed8 = createEmbed('This function is currently disabled :/.', '#e9196c');
interaction.reply({ embeds: [embed8], ephemeral: true }); return; }
musicPlayer.lyrics(interaction);
break;
default:
break;
}
} catch (e) {
//console.error("Error: ", e);
}
}
client.login(token);
// God, please forgive us, this is just to keep the bot online at all cost
process.on("unhandledRejection", (reason, p) => {
console.log(" [Error_Handling] :: Unhandled Rejection/Catch");
console.log(reason, p);
});
process.on("uncaughtException", (err, origin) => {
console.log(" [Error_Handling] :: Uncaught Exception/Catch");
console.log(err, origin);
});
process.on("uncaughtExceptionMonitor", (err, origin) => {
console.log(" [Error_Handling] :: Uncaught Exception/Catch (MONITOR)");
console.log(err, origin);
});