-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathMusic.js
243 lines (208 loc) · 8.55 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
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
'use strict';
const config = require('./config.json');
const tool = require('./tool.js');
const ytdl = require('ytdl-core');
const ySearch = require("youtube-search");
const Song = require('./obj/Song.js');
const MusicPlayer = require('./obj/MusicPlayer.js');
const rp = require('request-promise');
module.exports.processCommand = processCommand;
let guilds = {};
function processCommand(msg) {
if (!msg.guild.available) return;
//Add guild to the guild list.
if (!guilds[msg.guild.id])
guilds[msg.guild.id] = new MusicPlayer();
let guild = guilds[msg.guild.id];
let musicCmd = msg.content.split(/\s+/)[1];
if (musicCmd)
musicCmd.toLowerCase();
switch (musicCmd) {
case 'play':
return processInput(msg, guild);
case 'next':
return guild.skipSong(msg);
case 'pause':
return guild.pauseSong();
case 'reprendre':
return guild.resumeSong();
case 'queue':
return guild.printQueue(msg);
case 'np':
return guild.nowPlaying(msg);
case 'vol':
return guild.setVolume(msg);
case 'purge':
return guild.purgeQueue(msg);
case 'start':
return guild.joinVc(msg);
case 'quitte':
return guild.leaveVc(msg);
default:
msg.channel.send(`SVP référez vous à ${tool.wrap('!aide music')}.`);
}
}
function processInput(msg, guild) {
let url = msg.content.split(/\s+/).slice(2).join(' ');
if (url) {
if (!url.startsWith('http')) { //Assume its a search.
processSearch(msg, guild, url);
} else if (url.search('youtube.com')) { //Youtube.
let playlist = url.match(/list=(\S+?)(&|\s|$|#)/); //Match playlist id.
if (playlist) { //Playlist.
processYoutube.playlist(msg, guild, playlist[1]);
} else if (url.search(/v=(\S+?)(&|\s|$|#)/)) { //Video.
processYoutube.song(msg, guild, url);
} else {
msg.channel.send(`Lien Youtube non valide !!`);
}
} else if (url.search('soundcloud.com')) { //Soundcloud.
msg.channel.send('Désolé, la musique de Soundcloud n\'est pas fonctionnelle pour le moment.');
} else {
msg.channel.send('Désolé, je ne supporte que Youtube maintenant.');
}
}
}
function processSearch(msg, guild, searchQuery) {
const opts = {
maxResults: 3,
key: config.youtube_api_key
};
ySearch(searchQuery, opts, function (err, results) {
if (err) {
msg.channel.send(`Désolé, je n'ai pas trouvé de musique correspondante.`);
return console.log(err);
}
for (var y = 0; results[y].kind === 'youtube#channel'; y++);
ytdl.getInfo(results[y].link, function (err, song) {
if (err) {
msg.channel.send(`Désolé, je n'ai pas trouvé de musique correspondante.`);
return console.log(err);
}
const author = msg.author.username + '#' + msg.author.discriminator;
guild.queueSong(new Song(song.title, song.video_url, 'youtube', author, time(song.length_seconds), song.iurlmq));
msg.channel.send(
`Mise en file d'attente ${tool.wrap(song.title.trim())} (\`${time(song.length_seconds)}\`) lancer par ${tool.wrap(author)}`
);
if (guild.status != 'playing')
guild.playSong(msg, guild);
});
});
}
/*
Processing functions for Youtube links.
*/
const processYoutube = {
/*
Processes a Youtube song, pushing it to the queue.
@param {String} url The URL of the new song.
*/
song(msg, guild, url) {
ytdl.getInfo(url, (err, song) => {
if (err) {
console.log(err);
msg.channel.send(`Désolé, je ne peux mettre votre musique dans la queue.`);
return;
}
const author = msg.author.username + '#' + msg.author.discriminator;
console.log(song);
guild.queueSong(new Song(song.title, url, 'youtube', author,time(song.length_seconds), song.iurlmq));
msg.channel.send(
`Mise en file d'attente ${tool.wrap(song.title.trim())} (\`${time(song.length_seconds)}\`) lancer par ${tool.wrap(author)}`
);
if (guild.status != 'playing') {
guild.playSong(msg);
}
});
},
/*
Processes a Youtube playlist.
@param {String} playlistId The ID of the Youtube playlist.
*/
playlist(msg, guild, playlistId) {
const youtubeApiUrl = 'https://www.googleapis.com/youtube/v3/';
Promise.all([getPlaylistName(), getPlaylistSongs([], null)])
.then(results => addToQueue(results[0], results[1]))
.catch(err => {
console.log(err);
msg.channel.send(
`Désolé, je n'ai pas pu ajouter votre playlist à la file d'attente. Réessayez plus tard.`
)
});
async function getPlaylistName() {
let options = {
url: `${youtubeApiUrl}playlists?id=${playlistId}&part=snippet&key=${config.youtube_api_key}`
}
let body = await rp(options);
let playlistTitle = JSON.parse(body).items[0].snippet.title;
return playlistTitle;
}
async function getPlaylistSongs(playlistItems, pageToken) {
pageToken = pageToken ?
`&pageToken=${pageToken}` :
'';
let options = {
url: `${youtubeApiUrl}playlistItems?playlistId=${playlistId}${pageToken}&part=snippet,contentDetails&fields=nextPageToken,items(snippet(title,resourceId/videoId,thumbnails),contentDetails)&maxResults=50&key=${config.youtube_api_key}`
}
let body = await rp(options);
let playlist = JSON.parse(body);
playlistItems = playlistItems.concat(playlist.items.filter( //Concat all non-deleted videos.
item => item.snippet.title != 'Deleted video'));
if (playlist.hasOwnProperty('nextPageToken')) { //More videos in playlist.
playlistItems = await getPlaylistSongs(playlistItems, playlist.nextPageToken);
}
return playlistItems;
}
async function addToQueue(playlistTitle, playlistItems) {
let queueLength = guild.queue.length;
const author = msg.author.username + '#' + msg.author.discriminator;
for (let i = 0; i < playlistItems.length; i++) {
let song = new Song(
playlistItems[i].snippet.title,
`https://www.youtube.com/watch?v=${playlistItems[i].snippet.resourceId.videoId}`,
'youtube', author, "0:00", (playlistItems[i].snippet.thumbnails.medium.url || playlistItems[i].snippet.thumbnails.default.url));
guild.queueSong(song, i + queueLength);
}
msg.channel.send(
`Enqueued ${tool.wrap(playlistItems.length)} songs from ${tool.wrap(playlistTitle)} requested by ${tool.wrap(msg.author.username + '#' + msg.author.discriminator)}`
);
if (guild.status != 'playing') {
guild.playSong(msg);
}
}
},
}
/*
Parser time for video.
*/
function time(timesec){
let upTimeOutput = "";
if (timesec<60) {
upTimeOutput = `${timesec}s`;
} else if (timesec<3600) {
upTimeOutput = `${Math.floor(timesec/60)}:${timesec%60}`;
} else if (timesec<86400) {
upTimeOutput = `${Math.floor(timesec/3600)}:${Math.floor(timesec%3600/60)}:${timesec%3600%60}`;
} else if (timesec<604800) {
upTimeOutput = `${Math.floor(timesec/86400)}:${Math.floor(timesec%86400/3600)}:${Math.floor(timesec%86400%3600/60)}:${timesec%86400%3600%60}`;
}
return upTimeOutput;
}
/*
Timer for inactivity. Leave voice channel after inactivity timer expires.
*/
function timer() {
for (let guildId in guilds) {
let guild = guilds[guildId];
if (guild.status == 'stopped' || guild.status == 'paused')
guild.inactivityTimer -= 10;
if (guild.inactivityTimer <= 0) {
guild.voiceConnection.disconnect();
guild.voiceConnection = null;
guild.musicChannel.send(
':no_entry_sign: RAPTOR-BOT a quitter le canal vocal pour inactivité.');
guild.changeStatus('offline');
}
}
}
setInterval(timer, 10000);