-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathmusicbot.js
416 lines (341 loc) · 11.8 KB
/
musicbot.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
////////////////////////////////////////////////////////////////////////////////
// This program is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
////////////////////////////////////////////////////////////////////////////////
const Discord = require("discord.js");
const fs = require("fs");
const ytdl = require("ytdl-core");
const bot = new Discord.Client({autoReconnect: true, max_message_cache: 0});
const dm_text = "Hey there! Use !commands on a public chat room to see the command list.";
const mention_text = "Use !commands to see the command list.";
var aliases_file_path = "aliases.json";
var stopped = false;
var inform_np = true;
var now_playing_data = {};
var queue = [];
var aliases = {};
var voice_connection = null;
var voice_handler = null;
var text_channel = null;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
var commands = [
{
command: "stop",
description: "Stops playlist (will also skip current song!)",
parameters: [],
execute: function(message, params) {
if(stopped) {
message.reply("Playback is already stopped!");
} else {
stopped = true;
if(voice_handler !== null) {
voice_handler.end();
}
message.reply("Stopping!");
}
}
},
{
command: "resume",
description: "Resumes playlist",
parameters: [],
execute: function(message, params) {
if(stopped) {
stopped = false;
if(!is_queue_empty()) {
play_next_song();
}
} else {
message.reply("Playback is already running");
}
}
},
{
command: "request",
description: "Adds the requested video to the playlist queue",
parameters: ["video URL, ID or alias"],
execute: function(message, params) {
add_to_queue(params[1], message);
}
},
{
command: "np",
description: "Displays the current song",
parameters: [],
execute: function(message, params) {
var response = "Now playing: ";
if(is_bot_playing()) {
response += "\"" + now_playing_data["title"] + "\" (requested by " + now_playing_data["user"] + ")";
} else {
response += "nothing!";
}
message.reply(response);
}
},
{
command: "setnp",
description: "Sets whether the bot will announce the current song or not",
parameters: ["on/off"],
execute: function(message, params) {
if(params[1].toLowerCase() == "on") {
var response = "Will announce song names in chat";
inform_np = true;
} else if(params[1].toLowerCase() == "off") {
var response = "Will no longer announce song names in chat";
inform_np = false;
} else {
var response = "Sorry?";
}
message.reply(response);
}
},
{
command: "commands",
description: "Displays this message, duh!",
parameters: [],
execute: function(message, params) {
var response = "Available commands:";
for(var i = 0; i < commands.length; i++) {
var c = commands[i];
response += "\n!" + c.command;
for(var j = 0; j < c.parameters.length; j++) {
response += " <" + c.parameters[j] + ">";
}
response += ": " + c.description;
}
message.reply(response);
}
},
{
command: "skip",
description: "Skips the current song",
parameters: [],
execute: function(message, params) {
if(voice_handler !== null) {
message.reply("Skipping...");
voice_handler.end();
} else {
message.reply("There is nothing being played.");
}
}
},
{
command: "queue",
description: "Displays the queue",
parameters: [],
execute: function(message, params) {
var response = "";
if(is_queue_empty()) {
response = "the queue is empty.";
} else {
for(var i = 0; i < queue.length; i++) {
response += "\"" + queue[i]["title"] + "\" (requested by " + queue[i]["user"] + ")\n";
}
}
message.reply(response);
}
},
{
command: "clearqueue",
description: "Removes all songs from the queue",
parameters: [],
execute: function(message, params) {
queue = [];
message.reply("Queue has been clered!");
}
},
{
command: "aliases",
description: "Displays the stored aliases",
parameters: [],
execute: function(message, params) {
var response = "Current aliases:";
for(var alias in aliases) {
if(aliases.hasOwnProperty(alias)) {
response += "\n" + alias + " -> " + aliases[alias];
}
}
message.reply(response);
}
},
{
command: "setalias",
description: "Sets an alias, overriding the previous one if it already exists",
parameters: ["alias", "video URL or ID"],
execute: function(message, params) {
var alias = params[1].toLowerCase();
var val = params[2];
aliases[alias] = val;
fs.writeFileSync(aliases_file_path, JSON.stringify(aliases));
message.reply("Alias " + alias + " -> " + val + " set successfully.");
}
},
{
command: "deletealias",
description: "Deletes an existing alias",
parameters: ["alias"],
execute: function(message, params) {
var alias = params[1].toLowerCase();
if(!aliases.hasOwnProperty(alias)) {
message.reply("Alias " + alias + " does not exist");
} else {
delete aliases[alias];
fs.writeFileSync(aliases_file_path, JSON.stringify(aliases));
message.reply("Alias \"" + alias + "\" deleted successfully.");
}
}
},
];
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
bot.on("disconnect", event => {
console.log("Disconnected: " + event.reason + " (" + event.code + ")");
});
bot.on("message", message => {
if(message.channel.type === "dm" && message.author.id !== bot.user.id) { //Message received by DM
//Check that the DM was not send by the bot to prevent infinite looping
message.channel.sendMessage(dm_text);
} else if(message.channel.type === "text" && message.channel.name === text_channel.name) { //Message received on desired text channel
if(message.isMentioned(bot.user)) {
message.reply(mention_text);
} else {
var message_text = message.content;
if(message_text[0] == '!') { //Command issued
handle_command(message, message_text.substring(1));
}
}
}
});
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
function add_to_queue(video, message) {
if(aliases.hasOwnProperty(video.toLowerCase())) {
video = aliases[video.toLowerCase()];
}
var video_id = get_video_id(video);
ytdl.getInfo("https://www.youtube.com/watch?v=" + video_id, (error, info) => {
if(error) {
message.reply("The requested video could not be found.");
} else {
queue.push({title: info["title"], id: video_id, user: message.author.username});
message.reply('"' + info["title"] + '" has been added to the queue.');
if(!stopped && !is_bot_playing() && queue.length === 1) {
play_next_song();
}
}
});
}
function play_next_song() {
if(is_queue_empty()) {
text_channel.sendMessage("The queue is empty!");
}
var video_id = queue[0]["id"];
var title = queue[0]["title"];
var user = queue[0]["user"];
now_playing_data["title"] = title;
now_playing_data["user"] = user;
if(inform_np) {
text_channel.sendMessage('Now playing: "' + title + '" (requested by ' + user + ')');
}
var audio_stream = ytdl("https://www.youtube.com/watch?v=" + video_id);
voice_handler = voice_connection.playStream(audio_stream);
voice_handler.once("end", reason => {
voice_handler = null;
if(!stopped && !is_queue_empty()) {
play_next_song();
}
});
queue.splice(0,1);
}
function search_command(command_name) {
for(var i = 0; i < commands.length; i++) {
if(commands[i].command == command_name.toLowerCase()) {
return commands[i];
}
}
return false;
}
function handle_command(message, text) {
var params = text.split(" ");
var command = search_command(params[0]);
if(command) {
if(params.length - 1 < command.parameters.length) {
message.reply("Insufficient parameters!");
} else {
command.execute(message, params);
}
}
}
function is_queue_empty() {
return queue.length === 0;
}
function is_bot_playing() {
return voice_handler !== null;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
function get_video_id(string) {
var searchToken = "?v=";
var i = string.indexOf(searchToken);
if(i == -1) {
searchToken = "&v=";
i = string.indexOf(searchToken);
}
if(i == -1) {
searchToken = "youtu.be/";
i = string.indexOf(searchToken);
}
if(i != -1) {
var substr = string.substring(i + searchToken.length);
var j = substr.indexOf("&");
if(j == -1) {
j = substr.indexOf("?");
}
if(j == -1) {
return substr;
} else {
return substr.substring(0,j);
}
}
return string;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
exports.run = function(server_name, text_channel_name, voice_channel_name, aliases_path, token) {
aliases_file_path = aliases_path;
bot.on("ready", () => {
var server = bot.guilds.find("name", server_name);
var voice_channel = server.channels.find("name", voice_channel_name); //The voice channel the bot will connect to
text_channel = server.channels.find("name", text_channel_name); //The text channel the bot will use to announce stuff
voice_channel.join().then(connection => {voice_connection = connection;}).catch(console.error);
fs.access(aliases_file_path, fs.F_OK, (err) => {
if(err) {
aliases = {};
} else {
try {
aliases = JSON.parse(fs.readFileSync(aliases_file_path));
} catch(err) {
aliases = {};
}
}
});
console.log("Connected!");
});
bot.login(token);
}