-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
utils.js
103 lines (88 loc) · 3.42 KB
/
utils.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
const AsciiTable = require("ascii-table/ascii-table");
const YouTube = require("youtube-sr").default;
const ytdl = require("ytdl-core");
module.exports = {
/**
* @description Sends logs to console and adds the date/time
* @param content The content to log
*/
isFloat: function(n) {
return ((typeof n==='number')&&(n%1!==0));
},
log: function(content) {
date_ob = new Date();
date = date_ob.getDate().toString();
month = date_ob.getMonth().toString();
year = date_ob.getFullYear().toString();
if(date.length === 1){date = "0" + date;};
if(month.length === 1){month = "0" + month;};
dmy = date + "/" + month + "/" + year;
/* Gets hours, minutes and seconds */
hms = date_ob.toLocaleTimeString();
console.log(`[ ${dmy} | ${hms} ] ${content}`);
},
/**
* @description Checks if the provided string is an url
* @param {String} url
*/
isURL: function (url) {
if(!url) return false;
var pattern = new RegExp('^(https?:\\/\\/)?'+
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+
'((\\d{1,3}\\.){3}\\d{1,3}))|' +
'localhost' +
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+
'(\\?[;&a-z\\d%_.~+=-]*)?'+
'(\\#[-a-z\\d_]*)?$', 'i');
return pattern.test(url);
},
/**
* @description Create an ascii-table shown in the console on startup with the loaded events & commands
* @param {Object} loaded
*/
showTable: function(loaded){
var table = new AsciiTable('Loading content...');
table.setHeading("Commands","Events");
for(let i=0; i<=Math.max(loaded.commands.length, loaded.events.length)-1; i++){
table.addRow(loaded.commands[i], loaded.events[i]);
};
return table.render();
},
getUrl: async function (words){
stringOfWords = words.join(" ");
lookingOnYtb = new Promise(async (resolve) => {
YouTube.search(stringOfWords, { limit: 1 })
.then(result => {
resolve("https://www.youtube.com/watch?v=" + result[0].id);
});
});
let link = await lookingOnYtb;
return link;
},
play: function(song) {
const utils = require("./utils");
const serverQueue = queue.get("queue");
if(!song){
utils.log("No songs left in queue")
serverQueue.voiceChannel.leave();
return queue.delete("queue");
}
utils.log(`Started playing the music : ${song.title}`)
const dispatcher = serverQueue.connection.play(ytdl(song.url, {
filter: 'audioonly',
quality: 'highestaudio',
highWaterMark: 1 << 25
}));
dispatcher.on('finish', () => {
if(serverQueue.songs[0]) utils.log(`Finished playing the music : ${serverQueue.songs[0].title}`);
else utils.log(`Finished playing all musics, no more musics in the queue`);
if(serverQueue.loop === false || serverQueue.skipped === true) serverQueue.songs.shift();
if(serverQueue.skipped === true) serverQueue.skipped = false;
utils.play(serverQueue.songs[0]);
});
dispatcher.on('error', error => {
console.log(error)
});
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
}
}