forked from NimoHax/MusicBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
415 lines (383 loc) · 17 KB
/
bot.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
const Discord = require('discord.js');
const { Client, Util} = require('discord.js');
const config = require("./config.json");
const YouTube = require('simple-youtube-api');
const ytdl = require('ytdl-core');
const client = new Client({ disableEveryone: true});
const youtube = new YouTube(config.GOOGLE_API_KEY);
const PREFIX = config.prefix;
const queue = new Map();
client.on('warn', console.warn);
client.on('error', console.error);
client.on('ready', () => console.log('I am ready!'));
client.on('disconnect', () => console.log('I disconnected!'));
client.on('reconnecting', () => console.log('I am disconnecting!'));
client.on('voiceStateUpdate', (oldMember, newMember) => {
let newUserChannel = newMember.voiceChannel
let oldUserChannel = oldMember.voiceChannel
const serverQueue = queue.get(oldMember.guild.id);
if(oldUserChannel === undefined && newUserChannel !== undefined) {
// User joines a voice channel
} else if(newUserChannel === undefined){
// User leaves a voice channel
if(oldMember.id === '514856260353392660'){
return console.log("BOT");
}
else{
if(client.guilds.get(oldMember.guild.id).voiceConnection != null){
if(client.guilds.get(oldMember.guild.id).voiceConnection.channel.id === oldUserChannel.id){
if(oldUserChannel.members.size < 2){
serverQueue.songs = [];
serverQueue.connection.dispatcher.end('No members left in the channel!')
}
}else{
return console.log('not in the same voice channel');
}
}else{
return undefined;
}
}
}
})
client.on('message', async msg => { // eslint-disable-line
if (msg.author.bot) return undefined;
if (!msg.content.startsWith(PREFIX)) return undefined;
const args = msg.content.split(' ');
const searchString = args.slice(1).join(' ');
const url = args[1];
const serverQueue = queue.get(msg.guild.id);
if(msg.content.startsWith(`${PREFIX}play`)){
const voiceChannel = msg.member.voiceChannel;
if(!voiceChannel){
var embedplay1 = new Discord.RichEmbed()
.setTitle(`**Please Connect To A Voice Channel To Play Something!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedplay1);
}
const permissions = voiceChannel.permissionsFor(msg.client.user);
if(!permissions.has('CONNECT')){
var embedplay2 = new Discord.RichEmbed()
.setTitle(`**I lack the right CONNECT to connect in these Voice Channel!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedplay2);
}
if (!permissions.has('SPEAK')){
var embedplay3 = new Discord.RichEmbed()
.setTitle(`**I do not have the right to SPEAK to connect in these Voice Channel!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedplay3);
}
if(url.match(/^https?:\/\/(www.youtube.com|youtube.com)\/playlist(.*)$/)){
const playlist = await youtube.getPlaylist(url);
const videos = await playlist.getVideos();
for(const video of Object.values(videos)){
const video2 = await youtube.getVideoByID(video.id);
await handleVideo(video2, msg, voiceChannel, true);
}
var embedplay4 = new Discord.RichEmbed()
.setTitle(`**Playlist: ${playlist.title} queued!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedplay4);
}else{
try{
var video = await youtube.getVideo(url);
}catch(error){
try{
var videos = await youtube.searchVideos(searchString, 10);
let index = 0;
var embedqueue5 = new Discord.RichEmbed()
.setTitle(`__**Song Play By Blackfox**__`)
.setDescription(`
${videos.map(video2 => `**${++index}-** ${video2.title}`).join('\n')}
**Please enter a number between 1-10 on,a Song select!**`)
.setColor([226, 50, 41])
msg.channel.sendEmbed(embedqueue5);
try{
var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
maxMatches: 1,
time: 10000,
errors: ['time']
});
}catch(err){
console.error(err);
var embedplay6 = new Discord.RichEmbed()
.setTitle(`**no or invalid number was entered. Demolition of the song selection!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedplay6);
}
const videoIndex = parseInt(response.first().content);
var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
}catch(err){
console.error(err);
var embedplay7 = new Discord.RichEmbed()
.setTitle(`**I could find no video!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedplay7);
}
}
return handleVideo(video, msg, voiceChannel);
}
} else if(msg.content.startsWith(`${PREFIX}skip`)) {
if(!msg.member.voiceChannel){
var embedskip1 = new Discord.RichEmbed()
.setTitle(`**You are in not in the Voice Channel!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedskip1);
}
if(!serverQueue){
var embedskip2 = new Discord.RichEmbed()
.setTitle(`**There is nothing to Skip!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedskip2);
}
serverQueue.connection.dispatcher.end('Skip command has been used!');
var embedskip3 = new Discord.RichEmbed()
.setTitle(`**The Bot has been Skipped!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedskip3);
}
else if (msg.content.startsWith(`${PREFIX}stop`)){
if(!msg.member.voiceChannel){
var embedstop1 = new Discord.RichEmbed()
.setTitle(`**you're not in the voice channel!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedstop1);
}
if(!serverQueue){
var embedstop2 = new Discord.RichEmbed()
.setTitle(`**There is nothing to stop!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedstop2);
}
serverQueue.songs = [];
serverQueue.connection.dispatcher.end('Stop command has been used!');
var embedstop3 = new Discord.RichEmbed()
.setTitle(`**The Bot has been Skipped!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedstop3);
}
else if(msg.content.startsWith(`${PREFIX}song`)){
if(!serverQueue){
var embedsong1 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedsong1);
}
var embedsong2 = new Discord.RichEmbed()
.setTitle(`__**${serverQueue.songs[0].title}**__`)
.setThumbnail(serverQueue.songs[0].thumbnail)
.setDescription(`
Von: ${serverQueue.songs[0].channel}
Dauer: ${serverQueue.songs[0].duration}
Link: ${serverQueue.songs[0].url}
`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedsong2);
}
else if(msg.content.startsWith(`${PREFIX}volume`)){
if(!serverQueue){
var embedvolume1 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedvolume1);}
if(!args[1]){
var embedvolume2 = new Discord.RichEmbed()
.setTitle(`**The current volume is: ${serverQueue.volume}**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedvolume2);
}
if(args[1]>0){
serverQueue.volume = args[1];
serverQueue.connection.dispatcher.setVolume(args[1] / 2000);
serverQueue.mute = false;
var embedvolume3 = new Discord.RichEmbed()
.setTitle(`**The volume is on ${args[1]} set**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedvolume3);
} else{
var embedvolume4 = new Discord.RichEmbed()
.setTitle(`**Please enter a number >0 on!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedvolume4);
}
}
else if(msg.content.startsWith(`${PREFIX}queue`)){
if(!serverQueue){
var embedqueue1 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedqueue1);
}
var embedqueue2 = new Discord.RichEmbed()
.setTitle(`__**Song Queue**__`)
.setDescription(`
${serverQueue.songs.map(song => `**-** ${song.title}`).join('\n')}
**Playing:** ${serverQueue.songs[0].title}`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedqueue2);
}
else if(msg.content.startsWith(`${PREFIX}pause`)){
if(serverQueue && serverQueue.playing) {
serverQueue.playing = false;
serverQueue.connection.dispatcher.pause();
var embedpause1 = new Discord.RichEmbed()
.setTitle(`**The song is stopped!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedpause1);
}
var embedpause2 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedpause2);
}
else if(msg.content.startsWith(`${PREFIX}resume`)){
if(serverQueue && !serverQueue.playing){
serverQueue.playing = true;
serverQueue.connection.dispatcher.resume();
var embedresume1 = new Discord.RichEmbed()
.setTitle(`**The song keeps playing on!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedresume1);
}
var embedresume2 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedresume2);
}
else if(msg.content.startsWith(`${PREFIX}mutemusic`)){
if(!serverQueue){
var embedmute1 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedmute1);
}
if(serverQueue.mute){
var embedmute2 = new Discord.RichEmbed()
.setTitle(`**The music Bot is already muted!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedmute2);
}
else{
serverQueue.mute = true;
serverQueue.connection.dispatcher.setVolume(0 / 2000);
var embedmute3 = new Discord.RichEmbed()
.setTitle(`**The music Bot was muted!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedmute3);
}
}
else if(msg.content.startsWith(`${PREFIX}unmutemusic`)){
if(!serverQueue){
var embedunmute1 = new Discord.RichEmbed()
.setTitle(`**It does nothing at the moment!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedunmute1);
}
if(!serverQueue.mute){
var embedunmute2 = new Discord.RichEmbed()
.setTitle(`**The Music Bot is already unmuted!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedunmute2);
}
else{
serverQueue.mute = false;
serverQueue.connection.dispatcher.setVolume(serverQueue.volume / 2000);
var embedunmute3 = new Discord.RichEmbed()
.setTitle(`**The Music Bot has been unmuted!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedunmute3);
}
}
else if(msg.content.startsWith(`${PREFIX}helpmusic`)){
var embedhelp = new Discord.RichEmbed()
.setTitle(`__**BlackfoxMusic Commands**__`)
.addField("^play [YouTube Link/Playlist]", "Usage: `^play` Description: To play See The YouTube Linke And playlist.", false)
.addField("^play [Suchbegriff(e)]", "Usage: `^play`<song name> Description: To play Music.", false)
.addField("^skip", "Usage: `^skip` Description: To skip music.", false)
.addField("^stop", "Usage: `^stop` Description: To Bot disconnected.", false)
.addField("^song", "Usage: `^song` Description: To Check The Current playing song.", false)
.addField("^queue", "Usage: `^queue` Description: To Check The Queue List.", false)
.addField("^volume", "Usage: `^volume` Description: To See Volume.", false)
.addField("^volume [Wert]", "Usage: `^volume` Description: To Changes the volume level to the specified value.", false)
.addField("^pause", "Usage: `^pause` Description: To pause The Current Playing Song.", false)
.addField("^resume", "Usage: `^resume` Description: To Resume The Paused Song.", false)
.addField("^mutemusic", "Usage: `^mutemusic` Description: To mute Bot.", false)
.addField("^unmutemusic", "Usage: `^unmutemusic` Description: To unmute Bot.", false)
.setColor([226, 50, 41])
.setThumbnail(client.user.avatarURL)
return msg.channel.sendEmbed(embedhelp);
}
return undefined;
});
async function handleVideo(video, msg, voiceChannel, playlist=false){
const serverQueue = queue.get(msg.guild.id);
const song = {
id: video.id,
title: Util.escapeMarkdown(video.title),
url: `https://www.youtube.com/watch?v=${video.id}`,
thumbnail: video.thumbnails.default.url,
channel: video.channel.title,
duration: `${video.duration.hours}hrs : ${video.duration.minutes}min : ${video.duration.seconds}sec`
};
if(!serverQueue){
const queueConstruct = {
textChannel: msg.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 100,
mute: false,
playing: true
};
queue.set(msg.guild.id, queueConstruct);
queueConstruct.songs.push(song);
try{
var connection = await voiceChannel.join();
queueConstruct.connection = connection;
play(msg.guild, queueConstruct.songs[0]);
}catch(error){
console.log(error);
queue.delete(msg.guild.id);
var embedfunc1 = new Discord.RichEmbed()
.setTitle(`**Bot could not VoiceChannel the joinen!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedfunc1);
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
if(playlist) return undefined;
else{
var embedfunc2 = new Discord.RichEmbed()
.setTitle(`**${song.title} queued!**`)
.setColor([226, 50, 41])
return msg.channel.sendEmbed(embedfunc2);
}
}
return undefined;
}
function play(guild, song){
const serverQueue = queue.get(guild.id);
if(!song){
serverQueue.voiceChannel.leave();
queue.delete(guild.id);
return;
}
console.log(serverQueue.songs);
const dispatcher = serverQueue.connection.playStream(ytdl(song.url))
.on('end', reason => {
if(reason === 'Stream is not generating quickly enough.') console.log('Song ended');
else console.log(reason);
serverQueue.songs.shift();
setTimeout(() => {
play(guild, serverQueue.songs[0]);
}, 250);
})
.on('error', error => console.log(error));
dispatcher.setVolume(serverQueue.volume / 2000);
var embedfunction1 = new Discord.RichEmbed()
.setTitle(`** Begin ${song.title} to play.** __Note:__ **You should use ^volume 2000 for better sound**`)
.setColor([226, 50, 41])
return serverQueue.textChannel.sendEmbed(embedfunction1);
}
client.login(process.env.BOT_TOKEN);