-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
702 lines (581 loc) · 23.1 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
v1.3
Written by Albion Fung
Refs:
1. https://gabrieltanner.org/blog/dicord-music-bot
2. https://www.online-tech-tips.com/fun-stuff/how-to-make-your-own-discord-music-bot/
3. https://dev.to/galnir/how-to-write-a-music-command-using-the-discord-js-library-462f
4. https://stackoverflow.com
5. https://github.com (issues)
Reqs: Make sure to have a file config.json in the project's root folder. It must contain:
- prefix for the bot
- developerPrefix for the testing bot (can be the same as above prefix if not using a test bot)
- token for the bot
- developerToken for the testing bot (can be the same as above token if not using a test bot)
- API Key for Youtube API v3
You may freely distribute and use my code as long as your code is:
- open source
- credit me
- credit above refs when using my code
- follow the licenses of the modules used, unless you have your own replacement and do
not use the modules I use
- do not directly charge customers for using an application that uses this code base *
- have this same clause for the distribution and integration of your code
*: The code may still be used in a commercial operation as long as it is offered free.
For example, you may make use of this code in an operation that makes a profit off of
advertisements, as long as you do not charge users to pay for this code's functionality
- including the functionality of this code "free" to certain tiers of purchase but not
others is considered charging the user directly.
*/
// developer options
const args = process.argv.slice(2);
const isDev = args.indexOf('-dev') === -1 ? false : true;
const isDebug = args.indexOf('-debug') === -1 ? false : true;
const ytdl = require('ytdl-core');
const Discord = require('discord.js');
let {
prefix,
developerPrefix,
token,
developerToken,
apiKey,
BPQ_PATH
} = require('./config.json');
// my server uses ! and > for two rhythm bots as well. Add exclusions as necessary.
const otPrefixes = ['!', '>', isDev ? prefix : developerPrefix];
prefix = isDev ? developerPrefix : prefix;
const client = new Discord.Client();
isDev ? client.login(developerToken) : client.login(token);
const fetch = require("node-fetch");
const url = "https://www.googleapis.com/youtube/v3/search?part=id&type=video&key=" + apiKey + "&q=";
const vurl = "https://www.youtube.com/watch?v=";
// send html request to Youtube API
// Ik it's written in a sketchy way LOL so
// TODO: refactor this function, it's ugly
function sendReq(query, message, serverQueue, func = undefined, argvs = undefined) {
console.log("fetching results");
fetch(url + query)
.then(data=>{ return data.json();})
.then(res=>{ if(isDebug) console.log(res);
if(func) {
if(argvs) // if we have arguments we want to pass to callback
func(res, message, serverQueue, argvs);
else // specific callback specified
func(res, message, serverQueue)
} else { //default to enqueue
enqueue(res, message, serverQueue);
}
});
};
// for caching
const fs = require('fs');
const queue = new Map();
const BalPriorityQueue = require('./balanced-priority-queue.js');
let bpq;
// for saving and loading cache
// gdi windows why are you like this
if(process.platform === "win32") {
const rl = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
// when we recieve the terminating signal, END IT ALL. peacefully.
rl.on("SIGINT", function() {
botState = states.EXITING;
process.emit("SIGINT");
});
}
// redundant botstate change in case
// when sigint recieved, save the cache
process.on("SIGINT", function() {
botState = states.EXITING;
saveCache(BPQ_PATH);
process.exit();
});
function saveCache(BPQ_PATH) {
// stringify the cache
const cache = JSON.stringify({
pq: bpq.getCacheForSave()
});
fs.writeFileSync(BPQ_PATH, cache);
process.exit();
};
function loadCache(BPQ_PATH) {
let data = undefined;
if(fs.existsSync(BPQ_PATH))
data = JSON.parse(fs.readFileSync(BPQ_PATH));
// you can pass null / undef FYI
bpq = new BalPriorityQueue(data, isDebug);
};
const states = {
DC: 0,
CONNECTED: 1,
PLAYING: 2,
PAUSED: 3,
EXITING: 4,
};
let dispatcher = undefined;
let currentSong = undefined;
let recentRequestPerUser = {};
// this is where we link commands from input to the functions
let botFuncs = {};
botFuncs[`${prefix}h`] = botFuncs[`${prefix}help`] = help;
botFuncs[`${prefix}play`] = execute;
botFuncs[`${prefix}p`] = togglePlay;
botFuncs[`${prefix}stop`] = botFuncs[`${prefix}pause`] = pause;
botFuncs[`${prefix}skip`] = botFuncs[`${prefix}s`] = skip;
botFuncs[`${prefix}resume`] = botFuncs[`${prefix}r`] = resume;
botFuncs[`${prefix}oops`] = botFuncs[`${prefix}o`] = wrongResult;
botFuncs[`${prefix}UwUops`] = wongWesults;
botFuncs[`${prefix}queue`] = botFuncs[`${prefix}q`] = listQueue;
botFuncs[`${prefix}remove`] = botFuncs[`${prefix}rm`] = removeFromQueue;
botFuncs[`${prefix}clear`] = botFuncs[`${prefix}clr`] = botFuncs[`${prefix}l`] = clearQueue;
botFuncs[`${prefix}playing`] = botFuncs[`${prefix}np`] = nowPlaying;
// non music related commands
let notMusic = ["help", "h"];
// start at dc'd
let botState = states.DC;
loadCache(BPQ_PATH);
client.once('ready', () => {
console.log('Status: Ready');
console.log(`Prefix: ${prefix}`);
isDev ? console.log('DEVELOPER VEDA') : 1;
isDebug ? console.log('DEBUG MODE ON') : 1;
});
client.once('reconnecting', () => { console.log('Status: Reconnecting...'); });
client.once('disconnect', () => { console.log('Status: Disconnected'); botState = states.DC; });
client.on('message', async message => {
let cmd = message.content.split(" ")[0];
console.log('\n\n\nLog: Message: ' + message.content);
console.log("Log: First argument: " + cmd + " ," + cmd.length);
// ignore messages from other bots
if(message.author.bot) return;
// ignore messages meant for other bots
if(otPrefixes.indexOf(cmd[0]) !== -1) {
return;
}
// messages not meant for other bots or myself will be deleted
if(cmd[0] !== `${prefix}`) {
console.log('Log: Message deleted: ', message.content);
return message.delete();
}
// stop taking commands after exit signal recieved as it may fuck up the cache
if(botState === states.EXITING) {
return message.channel.send('Veda is currently shutting down and cannot take any commands.');
}
// check if the commands are valid
if(Object.keys(botFuncs).indexOf(cmd) === -1) {
console.log('Error: Unrecognized command: ', cmd);
return message.channel.send('Unrecognized command: ' + cmd);
}
// if help is requested, don't need to be in a voice channel
// TODO: fix it so it doesn't check for just 2 commands
if(!notMusic.includes(cmd)) {
return botFuncs[cmd](message, undefined);
}
// can't send music bot commands without being in a voice channel
if(!message.member.voice.channel) {
if(isDebug) console.log(message.member.voice.channel);
return message.channel.send('You must be in a voice channel to send a music related command to Veda.');
}
console.log("Log: command: " + cmd);
const serverQueue = queue.get(message.guild.id);
// call the relevant function for the command
botFuncs[cmd](message, serverQueue);
});
// this is run when they queue a song to play
// sorry for the confusing name
async function execute(message, serverQueue) {
console.log('Status: Play command received; not toggle.');
const args = message.content.split(" ");
const permissions = message.member.voice.channel.permissionsFor(message.client.user);
if (!permissions.has("CONNECT") || !permissions.has("SPEAK")) {
console.log('Error: No permission to connect or speak.');
return message.channel.send(
"I need the permissions to join and speak in your voice channel!"
);
}
let res;
// check if it's just a url
if(validURL(args[1])) {
// url, no need to fetch
if(isDebug) {
console.log('DEBUG: Given a URL:', args[1]);
}
if(!args[1].includes('youtube')) {
if(isDebug) console.log('Non-youtube link');
return message.channel.send('Only non-shortened Youtube links are accepted');
}
// pass to enqueue
// need vID?
let url = args[1];
let vidSub = url.split('v=')[1];
let ampPos = vidSub.indexOf('&');
let vID = ampPos !== -1 ? vidSub.substring(0, amPos) : vidSub;
let song = {
id: { videoId: vID }
};
res = bpq.getWithVID(vID);
if(res) {
res.isCached = true;
res = {items: [res] };
}
else
res = {items: [song]};
enqueue(res, message, serverQueue);
return;
}
// get cached if possible
// remove the command part of the string as it will lower score unecessarily
res = await bpq.get(message.content.substring(message.content.indexOf(' ') + 1));
if(res) {
if(isDebug) {
console.log('DEBUG: cache match found.');
message.channel.send('DEBUG: cache match found');
}
res.isCached = true;
enqueue({items: [res]}, message, serverQueue);
return;
}
// not cached, have to fetch from youtube.
if(isDebug) console.log('DEBUG: cannot find cache match.');
sendReq(args.slice(1, args.length + 1).join('+'), message, serverQueue);
};
// actual enqueuing the song
async function enqueue(response, message, serverQueue) {
if(isDebug) console.log('DEBUG Status: Enqueue.');
// this is from the youtube API call
let results = response.items;
if(isDebug) console.log('DEBUG: Results in enqueue ', results);
if(!results || results.length === 0) {
return message.channel.send('No matching query found.');
}
// get and build song properties
let song;
if(results[0].isCached) { // cached: we can just build the song obj
song = results[0];
song.requester = message.author.username;
song.wrongCount = 0;
song.message = message.content;
} else { // not cached: just get it from ytdl
const vID = results[0].id.videoId;
song = await getSong(message, vID, 0);
}
if(isDebug) {
console.log('DEBUG: song obj in enqueue:', song);
}
// add to / update cache
bpq.addSong(song);
// keep track of each user's last requested song for the oops command
recentRequestPerUser[song.requester] = {
results: results,
wrongCount: 0,
song: song
};
if(isDebug) console.log('DEBUG Log: Server queue check');
if(!serverQueue) { // create a server queue if we don't have one
if(isDebug) console.log('DEBUG Log: Creating server queue');
const queueContruct = {
textChannel: message.channel,
voiceChannel: message.member.voice.channel,
connection: null,
songs: [],
volume: 5,
playing: true
};
queue.set(message.guild.id, queueContruct);
queueContruct.songs.push(song);
try {
var connection = await message.member.voice.channel.join();
botState = states.CONNECTED;
queueContruct.connection = connection;
play(message.guild, queueContruct.songs[0]);
} catch (err) {
console.log(err);
queue.delete(message.guild.id);
return message.channel.send(err);
}
} else {
if(isDebug) console.log('DEBUG Log: Adding to server queue');
serverQueue.songs.push(song);
if(isDebug) console.log('DEBUG: serverQueue songs ', serverQueue.songs);
return message.channel.send(`${song.title} has been added to the queue.`);
}
};
// actually playing the song
function play(guild, song) {
console.log('Status: Playing audio');
// last song they requested, no longer need to keep track
if(song && recentRequestPerUser[song.requester].results[song.wrongCount].id.videoId
=== song.vID) {
delete recentRequestPerUser[song.requester];
}
currentSong = song;
const serverQueue = queue.get(guild.id);
// TODO: timeout to leave channel
if(!song) {
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
dispatcher = undefined;
return;
}
if(isDebug) console.log('DEBUG: song url ', song.url);
dispatcher = serverQueue.connection
.play(ytdl(song.url, { filter: 'audioonly', quality: 'highestaudio', highWaterMark: 1 << 25 }))
.on("finish", () => { // when song is finished
serverQueue.songs.shift();
currentSong = undefined;
play(guild, serverQueue.songs[0]);
})
.on("error", error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`Now playing: ${song.title}`);
botState = states.PLAYING;
};
function skip(message, serverQueue) {
if (!serverQueue)
return message.channel.send('The queue is already clear.');
serverQueue.connection.dispatcher.end();
dispatcher = undefined;
};
function togglePlay(message, serverQueue) {
if(message.content.split(" ").length > 1) {
execute(message, serverQueue);
} else if(botState === states.PAUSED) {
resume(message, serverQueue);
} else if(botState === states.PLAYING) {
pause(message, serverQueue);
}
};
function pause(message, serverQueue) {
if(botState !== states.PLAYING) {
return serverQueue.textChannel.send('Cannot pause when no music is being played.');
}
if(dispatcher) {
dispatcher.pause();
botState = states.PAUSED;
console.log('Status: Paused playback.');
return serverQueue.textChannel.send(`Paused.`);
}
console.log('Error: Dispatcher not found.');
return serverQueue.textChannel.send('Error: Unable to complete action. Please report this to the administrator: Dispatcher not found.');
};
function resume(message, serverQueue) {
if(botState !== states.PAUSED) {
return serverQueue.textChannel.send('Cannot resume playback when no music has been paused.');
}
if(dispatcher) {
botState = states.PLAYING;
dispatcher.resume();
console.log('Status: Resumed playing.');
return serverQueue.textChannel.send(`Resumed playing: ${currentSong.title}`);
}
console.log('Error: Dispatcher not found.');
return serverQueue.textChannel.send('Error: Unable to complete action. Please report this to the administrator: Dispatcher not found.');
}
function listQueue(message, serverQueue) {
let ret = 'Current queue:\n';
for(let i in serverQueue.songs) {
ret += `${i}. ${serverQueue.songs[i].title}\n`;
}
ret += `
use \`${prefix}remove NUMBER\` to remove NUMBERth item from queue.
`;
return message.channel.send(ret);
};
function removeFromQueue(message, serverQueue) {
let num = message.content.split(" ")[1];
if(isNaN(num) || parseInt(num) >= serverQueue.songs.length) {
return message.channel.send('Invalid selection ' + num);
}
num = parseInt(num);
let title = serverQueue.songs[num].title;
let numth = num === 1 ? 'st' : (num === 2 ? 'nd' : (num === 3 ? 'rd' : 'th'));
serverQueue.songs.splice(num, 1);
return message.channel.send(`Removed ${num}${numth} entry: ${title} in the queue.`);
};
function clearQueue(message, serverQueue) {
serverQueue.songs = [];
return message.channel.send(`${message.author.toString()} cleared the queue.`);
};
// function for oops command
async function wrongResult(message, serverQueue) {
// user can only fix their own last queued song that hasn't been played yet
let songInfo = recentRequestPerUser[message.author.toString()];
if(!songInfo) {
return message.channel.send('No song queued.');
}
// wrongCount lets us know which result to take next.
let wrongCount = 0;
let vID = undefined;
let song = songInfo.song;
if(!song.isCached) { // not cached, change as normal
// loops back if the last song still isn't what they want; just in case
// if they'd rather have the first one.
wrongCount = songInfo.wrongCount + 1 === songInfo.results.length ?
0 : songInfo.wrongCount + 1;
songInfo.wrongCount = wrongCount;
vID = songInfo.results[wrongCount].id.videoId;
getAndSwapSong(undefined, message, serverQueue, {
song: song,
vID: vID,
wrongCount: wrongCount
});
} else { // cached, fetch new results befroe swapping
let args = song.message.split(" ");
args = args.slice(1, args.length + 1).join('+');
sendReq(args, message, serverQueue, getAndSwapSong, {song: song, wrongCount: wrongCount});
}
};
async function nowPlaying(message, serverQueue) {
if(!currentSong) {
return message.channel.send(`Not playing anything.`);
}
if(!currentSong.duration) { // older cache won't have duration
// we need to fetch that for it
if(isDebug) console.log('DEBUG NP: updating song duration, not in cache.');
let info = await ytdl.getBasicInfo(vurl + currentSong.vID);
bpq.update(currentSong.vID, 'duration', info.length_seconds);
currentSong.duration = info.length_seconds;
}
let durStr = getTimeString(currentSong.duration);
let played = '';
if(dispatcher) {
played = getTimeString(dispatcher.streamTime, true);
played += "/";
}
return message.channel.send({ // this sends an embedded msg
embed: {
color: 3447003,
title: currentSong.title,
url: vurl + currentSong.vID,
description: `${played}${durStr}\nRequested by ${currentSong.requester}`
}
});
};
function getTimeString(seconds, isMili=false) {
if(isMili) seconds = Math.round(seconds / 1000);
let min = Math.floor(seconds / 60);
let sec = seconds - (min * 60);
let hour = '';
if(min >= 60) {
hour = Math.floor(min / 60);
min = min - hour * 60;
}
if(min < 10) min = '0' + min;
if(sec < 10) sec = '0' + sec;
if(hour && hour < 10) hour = '0' + hour;
if(hour) hour += ':'
return `${hour}${min}:${sec}`;
};
async function getAndSwapSong(res, message, serverQueue, argvs) {
const song = argvs.song;
const wrongCount = argvs.wrongCount;
let vID = argvs.vID;
if(res) { // only exists if we had to refetch
const results = res.items;
if(!results || results.length === 0) {
return message.channel.send('Unable to retrieve alternative query result.');
}
vID = results[0].id.videoId;
}
if(isDebug) {
console.log('DEBUG W: song info ', song);
console.log('DEBUG W: wrongCount ', wrongCount);
console.log('DEBUG W: song id ', vID);
}
const newSong = await getSong(message, vID, wrongCount);
if(res) {
recentRequestPerUser[song.requester] = {
results: res.items,
wrongCount: wrongCount,
song: newSong
};
}
replaceSong(song, newSong, serverQueue);
if(isDebug) {
console.log(`DEBUG W: old id ${song.vID} and new id ${newSong.vID}`);
}
return message.channel.send(`${song.title} replaced by ${newSong.title}`);
};
async function wongWesults(message, serverQueue) {
message.channel.send('"You weeb" - Declan 2020');
wrongResult(message, serverQueue);
};
function replaceSong(song, newSong, serverQueue) {
// get index of the song we need to replace
const index = serverQueue.songs.map(function(e) { return song.vID; })
.indexOf(song.vID);
if(isDebug) {
console.log('DEBUG replace: replacing song');
console.log('DEBUG replace: to replace index: ', index)
console.log('DEBUG replace: old song: ', serverQueue[index]);
}
// replace it
serverQueue[index] = newSong;
if(isDebug) {
console.log('DEBUG replace: new song', serverQueue[index]);
}
};
async function getSong(message, vID, resultID) {
console.log('Log: Getting video from vID ' + vID);
const songInfo = await ytdl.getInfo(vurl + vID);
if(isDebug) console.log('DEBUG: get song song info', songInfo.length_seconds);
const song = {
title: songInfo.title,
url: songInfo.video_url,
vID: vID,
requester: message.author.toString(),
wrongCount: resultID,
duration: songInfo.length_seconds
};
return song;
};
// courtesy of
// https://stackoverflow.com/questions/5717093/check-if-a-javascript-string-is-a-url
function validURL(str) {
var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
'(\\#[-a-z\\d_]*)?$','i'); // fragment locator
return !!pattern.test(str);
};
// help
function help (message, serverQueue) {
const helpMsg =
`Hello! I am Veda. These are my current functionalities:
- Play music.
My command prefix is \`${prefix}\`.
Documentation notes:
- [...] denotes optional arguments.
- Capitalization denotes required arguments.
- \`...\` denotes a command.
These are the available commands:
- play URL: go to Youtube URL and play that video. **PREFERRED.**
- play NAME: search Youtube for NAME and play the top result.
- resume: resumes playback.
- skip: skip current song.
- queue: display songs in the queue.
- remove NUM: remove NUMth song on the queue.
- clear: clears the entire queue - careful, you'll get called out.
- playing: show what is being played, its progress and its link.
- oops: search for the next best result for **your** last query if songbot queues the wrong song. Only works before it's played.
- help: shows this help message.
Shorthands (If you get confused, use above full commands):
- p URL: same as \`play URL\`. **PREFERRED.**
- p NAME: same as \`play NAME\`.
- p: toggles music playback.
- r: same as \`resume\`.
- s: same as \`skip\`.
- q: same as \`queue\`.
- rm: same as \`remove\`.
- clr: same as \`clear\`.
- l: same as \`clear\`.
- np: same as \`playing\`.
- o: same as \`oops\`.
- h: same as \`help\`.
`;
return message.channel.send(helpMsg);
};