-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
364 lines (311 loc) · 9.63 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
/*Global variables */
var voiceChannel;
var savedConn;
var songQueueURL=[]; // For song URL
var songQueueTitle=[]; // For song Title
var currentPlaying;
var dispatcher;
/*
We use the googleapis module in order to access the
Youtube API and after authentication we can use their
feature and functions.
*/
var {google} =require('googleapis');
var youtubeAPI=google.youtube({
version:'v3',
auth:'<API key>'
});
console.log("Youtube API authentication has been made");
/*
We usee the discord.js in order to access the API of the
discord so we can use it to setup the music bot we want.
*/
var Discord=require('discord.js');
var client=new Discord.Client();
var ytdl=require('ytdl-core'); //To fetch the videos from youtube
/* We login to the discord service with the given token */
client.login('<API Key>');
/* When the bot is ready */
client.on('ready',()=>{
console.log("Bot is active as "+client.user.tag+"!");
});
/* We print out the manual for our bot */
client.on('message',(message)=>{
if(message.content==='help'){
const embed = new Discord.RichEmbed()
.setTitle("List of Commands: ")
.setColor(0x00AE86)
.setDescription("All the commands should start with ! ")
.addField("play [SONG]","Plays a song with the given name.",false) //Done
.addField("disconnect","Disconnect the bot from the voice channel it is in.",false) // Done
.addField("np","Shows what song the bot is currently playing.",false) //Done
.addField("skip","Skips the currently playing song.",false) //Done
.addField("remove [NUMBER OF SONG]","Removes a certain entry from the queue.",false)
.addField("join","Summons the bot to your voice channel.",false) // Done
.addField("pause","Pauses the currently playing track.",false) //Done
.addField("continue","Continue the currently playling track.",false) //Done
.addField("volume [0-1]","Check or change the current volume.",false) //Done
.addField("queue","View the queue.",false) // Done
.addField("add [SONG]","Add a song to playlist",false) // Done
message.channel.send({embed});
}
});
/* Summons the bot to your voice channel */
client.on('message',(message)=>{
if(message.content==='!join'){
if(message.member.voiceChannel){
/* Save globally the voice channel */
voiceChannel=message.member.voiceChannel;
voiceChannel.join()
.then(connection =>{
/* Save the connection globally */
savedConn=connection;
const embed = new Discord.RichEmbed()
.setTitle("Connection Successfull: ")
.setColor(0x00AE86)
.setDescription(client.user.tag+" has joined "+voiceChannel.name);
/* Send the message */
message.channel.send({embed});
});
}
else{
/* Error you are not in a voice Channel */
const embed = new Discord.RichEmbed()
.setTitle("Connection Failed: ")
.setColor(0xB63A3A)
.setDescription("You should join a voice Channel.");
message.channel.send({embed});
}
}
});
/* Disconnect the bot from the voice channel it is in. */
client.on('message',(message)=>{
if(message.content==='!disconnect'){
if(voiceChannel){
const embed = new Discord.RichEmbed()
.setTitle("Disconnect : ")
.setColor(0x00AE86)
.setDescription(client.user.tag+" left "+voiceChannel.name);
message.channel.send({embed});
voiceChannel.leave();
}
else{
const embed = new Discord.RichEmbed()
.setTitle("Disconnect : ")
.setColor(0xB63A3A)
.setDescription(client.user.tag+" is not in a Voice Channel.");
message.channel.send({embed});
}
}
});
/* Add a song to playlist */
client.on('message',(message)=>{
if(message.content.startsWith('!add')){
/* Get the name of the song we want to search */
var splitContent=message.content.split(/\s+/g);
var theSong="";
for(var i=0;i<splitContent.length;i++){
if(i!=0){
theSong=theSong+splitContent[i]+" ";
}
}
if(theSong){
/* The search parameters */
var params={
type:'video',
part:'snippet',
maxResults:'10',
q:theSong,
};
/* Now we search the video */
youtubeAPI.search.list(params,function(err,data){
if(err){ console.log(err);}
else{
var firstID=data.data.items[0].id.videoId;
var tempURL='https://www.youtube.com/watch?v='+firstID;
songQueueURL.push(tempURL);
songQueueTitle.push(data.data.items[0].snippet.title);
/* We sent that a new song has been added */
const embed = new Discord.RichEmbed()
.setTitle("Adding a Song: ")
.setColor(0x00AE86)
.setDescription(data.data.items[0].snippet.title+" has been added to the playlist.");
message.channel.send({embed});
}
});
}
}
});
/* View the queue */
client.on('message',(message)=>{
if(message.content==='!queue'){
if(songQueueTitle.length!=0){
var songs="";
for(var i=0;i<songQueueTitle.length;i++){
songs=songs+(i+1)+"."+songQueueTitle[i]+"\n";
}
const embed = new Discord.RichEmbed()
.setTitle("Playlist: ")
.setColor(0x00AE86)
.setDescription(songs);
message.channel.send({embed});
}
else{
const embed = new Discord.RichEmbed()
.setTitle("Playlist is Empty")
.setColor(0xB63A3A)
message.channel.send({embed});
}
}
});
/* Shows what song the bot is currently playing. */
client.on('message',(message)=>{
if(message.content==='!np'){
if(currentPlaying){
var currentTitle;
for(var i=0;i<songQueueURL.length;i++){
if(currentPlaying === songQueueURL[i]){
currentTitle=songQueueTitle[i];
}
}
/* Send the current playing */
const embed = new Discord.RichEmbed()
.setTitle("Current Playing: ")
.setColor(0x00AE86)
.setDescription(currentTitle+" now Playing.");
message.channel.send({embed});
}
else{
const embed = new Discord.RichEmbed()
.setTitle("Nothing playing right now.")
.setColor(0xB63A3A)
message.channel.send({embed});
}
}
});
/* Plays a song with the given name. */
client.on('message',(message)=>{
if(message.content==='!play'){
/* If connection is made */
if(savedConn){
if(songQueueURL.length!=0){
currentPlaying=songQueueURL[0];
dispatcher=savedConn.playStream(ytdl(currentPlaying,{audioonly:true}));
/* Dispatcher at the end of the song */
dispatcher.on('end',()=>{
/* Remove the current playing */
var tempArraySong=new Array();
var tempTitle=new Array();
for(var i=0;i<songQueueURL.length;i++){
if(songQueueURL[i]!==currentPlaying){
tempArraySong.push(songQueueURL[i]);
tempTitle.push(songQueueTitle[i]);
}
}
songQueueURL=tempArraySong;
songQueueTitle=tempTitle;
if(songQueueURL.length=== 0){
const embed = new Discord.RichEmbed()
.setTitle("Playlis is empty.")
.setColor(0xB63A3A)
message.channel.send({embed});
}
else{
message.channel.send("!play");
}
});
}
else{
const embed = new Discord.RichEmbed()
.setTitle("Playlis is empty.")
.setColor(0xB63A3A)
message.channel.send({embed});
}
}
else{
const embed = new Discord.RichEmbed()
.setTitle("Invite "+client.user.tag+" to the Voice Channel.")
.setColor(0xB63A3A)
message.channel.send({embed});
}
}
});
/* Skip or pause or continue*/
client.on('message',(message)=>{
if(message.content==='!skip'){
dispatcher.end();
}
else if(message.content==='!pause'){
dispatcher.pause();
}
else if(message.content==='!continue'){
dispatcher.resume();
}
});
/* Volume */
client.on('message',(message)=>{
if(message.content.startsWith('!volume')){
var splitContent=message.content.split(/\s+/g)
if(splitContent[1]){
var number=splitContent[1].trim();
/* If the volume is out of the limits */
if(number > 1 || number < 0){
const embed = new Discord.RichEmbed()
.setTitle("The volume should be between 0 and 1")
.setColor(0xB63A3A)
message.channel.send({embed});
}
else{
dispatcher.setVolume(number);
const embed = new Discord.RichEmbed()
.setTitle("Volume has been set to: "+number)
.setColor(0x00AE86)
message.channel.send({embed});
}
}
}
});
/* Remove something from the playlist */
client.on('message',(message)=>{
if(message.content.startsWith('!remove')){
var splitContent=message.content.split(/\s+/g);
if(splitContent[1]){
var num=splitContent[1].trim();
console.log("Num: "+num);
if(num <= 0 || num > songQueueURL.length){
const embed = new Discord.RichEmbed()
.setTitle("The queue number is invalid")
.setColor(0xB63A3A)
message.channel.send({embed});
}
else{
/* Remove the current playing */
var tempArraySong=new Array();
var tempTitle=new Array();
var removedTitle;
for(var i=0;i<songQueueURL.length;i++){
if(i!=(num-1)){
tempArraySong.push(songQueueURL[i]);
tempTitle.push(songQueueTitle[i]);
}
else{
console.log("here");
removedTitle=songQueueTitle[i];
}
}
songQueueURL=tempArraySong;
songQueueTitle=tempTitle;
const embed = new Discord.RichEmbed()
.setTitle(removedTitle+" has been successfully removed!")
.setColor(0x00AE86)
message.channel.send({embed});
}
}
}
});
/* When the bot disconnects */
client.on('disconnect',()=>{
console.log("The Bot has been disconnected.");
console.log("Trying to reconnect....");
client.connect();
});