-
Notifications
You must be signed in to change notification settings - Fork 61
/
music.js
118 lines (113 loc) · 3.71 KB
/
music.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
const ytdl = require('ytdl-core');
const request = require('request');
const fs = require('fs');
const getYouTuberID = require('get-youtube-id');
const Circular = require('circular-json');
const config = require('./config.json');
const yt_api_key = config.youtube_api_key;
module.exports = {
isYoutube(str) {
return str.toLowerCase().indexOf('youtube.com') > -1;
},
searchVideo(query, callback) {
request('https://www.googleapis.com/youtube/v3/search?part=id&type=viedo&q=' + encodeURIComponent(query) + '&key=' + yt_api_key, (error, response, body) => {
if (error) {
throw error;
}
let json = JSON.parse(body);
if (!json.items[0]) {
callback('3_-a9nVZYjk');
}
else {
console.log(json.items[0].id);
callback(json.items[0].id.videoId);
}
});
},
getID(str, cb) {
if (this.isYoutube(str)) {
cb(getYouTuberID(str));
}
else {
this.searchVideo(str, (id) => {
cb(id);
});
}
},
addToQueue(strID, GuildQueue) {
if (this.isYoutube(strID)) {
GuildQueue.queue.push(getYouTuberID(strID));
}
else {
GuildQueue.queue.push(strID);
}
},
playMusic(id, message, GuildQueue) {
GuildQueue.voiceChannel = message.member.voiceChannel;
console.log('in here');
if (!GuildQueue.voiceChannel) {
return message.reply(' please join a voice channel first!');
}
GuildQueue.voiceChannel.join().then(connection => {
const stream = ytdl('https://www.youtube.com/watch?v=' + id, { filter: 'audioonly' });
GuildQueue.dispatcher = connection.playStream(stream);
GuildQueue.skipReq = 0;
GuildQueue.skippers = [];
GuildQueue.dispatcher.on('end', () => {
GuildQueue.skipReq = 0;
GuildQueue.skippers = [];
GuildQueue.queue.shift();
GuildQueue.queueNames.shift();
if (GuildQueue.queue.length === 0) {
GuildQueue.queue = [];
GuildQueue.queueNames = [];
GuildQueue.isPlaying = false;
}
else {
this.playMusic(GuildQueue.queue[0], message, GuildQueue);
}
});
console.log('music ready!');
});
},
skipSong(message, GuildQueue) {
GuildQueue.dispatcher.end();
if (GuildQueue.queue.length > 1) {
this.playMusic(GuildQueue.queue[0], message, GuildQueue);
}
else {
GuildQueue.skipReq++;
GuildQueue.skippers = [];
}
},
pauseSong(message, GuildQueue) {
if (GuildQueue.isPlaying) {
GuildQueue.dispatcher.pause();
GuildQueue.isPlaying = false;
}
else {
message.channel.reply(', someone has already paused the queue.');
}
},
resumeSong(message, GuildQueue) {
console.log(GuildQueue.isPlaying);
if (!GuildQueue.isPlaying) {
console.log('in here');
GuildQueue.dispatcher.resume();
GuildQueue.isPlaying = true;
}
else {
message.channel.reply(', the queue is already playing.');
}
},
setVolume(volume, GuildQueue) {
GuildQueue.dispatcher.setBitrate(volume);
},
writeGuilds(guilds) {
fs.writeFile('./guilds.json', Circular.stringify(guilds, null, 4), err => {
if (err) {
throw err;
}
});
},
};