forked from AlphaCode-YourBotGuide/AlphaCode-1Music
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
346 lines (267 loc) · 10.8 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
const Discord = require('discord.js');
const Util = require('discord.js');
const getYoutubeID = require('get-youtube-id');
const fetchVideoInfo = require('youtube-info');
const YouTube = require('simple-youtube-api');
const youtube = new YouTube("AIzaSyAdORXg7UZUo7sePv97JyoDqtQVi3Ll0b8");
const queue = new Map();
const ytdl = require('ytdl-core');
const fs = require('fs');
const gif = require("gif-search");
const client = new Discord.Client({disableEveryone: true});
const prefix = "!";
/////////////////////////
////////////////////////
client.on('message', async msg =>{
if (msg.author.bot) return undefined;
if (!msg.content.startsWith(prefix)) return undefined;
let args = msg.content.split(' ');
let command = msg.content.toLowerCase().split(" ")[0];
command = command.slice(prefix.length)
if(command === `ping`) {
let embed = new Discord.RichEmbed()
.setColor(3447003)
.setTitle("Pong!!")
.setDescription(`${client.ping} ms,`)
.setFooter(`Requested by | ${msg.author.tag}`);
msg.delete().catch(O_o=>{})
msg.channel.send(embed);
}
});
/////////////////////////
////////////////////////
//////////////////////
client.on('message', async msg =>{
if (msg.author.bot) return undefined;
if (!msg.content.startsWith(prefix)) return undefined;
let args = msg.content.split(' ');
let command = msg.content.toLowerCase().split(" ")[0];
command = command.slice(prefix.length)
if(command === `avatar`){
if(msg.channel.type === 'dm') return msg.channel.send("Nope Nope!! u can't use avatar command in DMs (:")
let mentions = msg.mentions.members.first()
if(!mentions) {
let sicon = msg.author.avatarURL
let embed = new Discord.RichEmbed()
.setImage(msg.author.avatarURL)
.setColor("#5074b3")
msg.channel.send({embed})
} else {
let sicon = mentions.user.avatarURL
let embed = new Discord.RichEmbed()
.setColor("#5074b3")
.setImage(sicon)
msg.channel.send({embed})
}
};
});
/////////////////////////
////////////////////////
//////////////////////
/////////////////////////
////////////////////////
//////////////////////
/////////////////////////
////////////////////////
//////////////////////
/////////////////////////
////////////////////////
//////////////////////
client.on('message', async msg => {
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] ? args[1].replace(/<(.+)>/g, '$1') : '';
const serverQueue = queue.get(msg.guild.id);
let command = msg.content.toLowerCase().split(" ")[0];
command = command.slice(prefix.length)
if (command === `play`) {
const voiceChannel = msg.member.voiceChannel;
if (!voiceChannel) return msg.channel.send("I can't find you in any voice channel!");
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has('CONNECT')) {
return msg.channel.send("I don't have enough permissions to join your voice channel!");
}
if (!permissions.has('SPEAK')) {
return msg.channel.send("I don't have enough permissions to speak in your voice channel!");
}
if (!permissions.has('EMBED_LINKS')) {
return msg.channel.sendMessage("I don't have enough permissions to insert a URLs!")
}
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);
}
return msg.channel.send(`**${playlist.title}**, Just added to the queue!`);
} else {
try {
var video = await youtube.getVideo(url);
} catch (error) {
try {
var videos = await youtube.searchVideos(searchString, 5);
let index = 0;
const embed1 = new Discord.RichEmbed()
.setTitle(":mag_right: YouTube Search Results :")
.setDescription(`
${videos.map(video2 => `${++index}. **${video2.title}**`).join('\n')}`)
.setColor("#f7abab")
msg.channel.sendEmbed(embed1).then(message =>{message.delete(20000)})
/////////////////
try {
var response = await msg.channel.awaitMessages(msg2 => msg2.content > 0 && msg2.content < 11, {
maxMatches: 1,
time: 15000,
errors: ['time']
});
} catch (err) {
console.error(err);
return msg.channel.send('No one respone a number!!');
}
const videoIndex = parseInt(response.first().content);
var video = await youtube.getVideoByID(videos[videoIndex - 1].id);
} catch (err) {
console.error(err);
return msg.channel.send("I didn't find any results!");
}
}
return handleVideo(video, msg, voiceChannel);
}
} else if (command === `skip`) {
if (!msg.member.voiceChannel) return msg.channel.send("You Must be in a Voice channel to Run the Music commands!");
if (!serverQueue) return msg.channel.send("There is no Queue to skip!!");
serverQueue.connection.dispatcher.end('Ok, skipped!');
return undefined;
} else if (command === `stop`) {
if (!msg.member.voiceChannel) return msg.channel.send("You Must be in a Voice channel to Run the Music commands!");
if (!serverQueue) return msg.channel.send("There is no Queue to stop!!");
serverQueue.songs = [];
serverQueue.connection.dispatcher.end('Ok, stopped & disconnected from your Voice channel');
return undefined;
} else if (command === `vol`) {
if (!msg.member.voiceChannel) return msg.channel.send("You Must be in a Voice channel to Run the Music commands!");
if (!serverQueue) return msg.channel.send('You only can use this command while music is playing!');
if (!args[1]) return msg.channel.send(`The bot volume is **${serverQueue.volume}**`);
serverQueue.volume = args[1];
serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 50);
return msg.channel.send(`Volume Now is **${args[1]}**`);
} else if (command === `np`) {
if (!serverQueue) return msg.channel.send('There is no Queue!');
const embedNP = new Discord.RichEmbed()
.setDescription(`Now playing **${serverQueue.songs[0].title}**`)
return msg.channel.sendEmbed(embedNP);
} else if (command === `queue`) {
if (!serverQueue) return msg.channel.send('There is no Queue!!');
let index = 0;
// // //
const embedqu = new Discord.RichEmbed()
.setTitle("The Queue Songs :")
.setDescription(`
${serverQueue.songs.map(song => `${++index}. **${song.title}**`).join('\n')}
**Now playing :** **${serverQueue.songs[0].title}**`)
.setColor("#f7abab")
return msg.channel.sendEmbed(embedqu);
} else if (command === `pause`) {
if (serverQueue && serverQueue.playing) {
serverQueue.playing = false;
serverQueue.connection.dispatcher.pause();
return msg.channel.send('Ok, paused');
}
return msg.channel.send('There is no Queue to Pause!');
} else if (command === "resume") {
if (serverQueue && !serverQueue.playing) {
serverQueue.playing = true;
serverQueue.connection.dispatcher.resume();
return msg.channel.send('Ok, resumed!');
}
return msg.channel.send('Queue is empty!');
}
return undefined;
});
async function handleVideo(video, msg, voiceChannel, playlist = false) {
const serverQueue = queue.get(msg.guild.id);
console.log(video);
const song = {
id: video.id,
title: Util.escapeMarkdown(video.title),
url: `https://www.youtube.com/watch?v=${video.id}`
};
if (!serverQueue) {
const queueConstruct = {
textChannel: msg.channel,
voiceChannel: voiceChannel,
connection: null,
songs: [],
volume: 5,
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.error(`I could not join the voice channel: ${error}!`);
queue.delete(msg.guild.id);
return msg.channel.send(`Can't join this channel: ${error}!`);
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
if (playlist) return undefined;
else return msg.channel.send(`**${song.title}**, just added to the queue! `);
}
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();
play(guild, serverQueue.songs[0]);
})
.on('error', error => console.error(error));
dispatcher.setVolumeLogarithmic(serverQueue.volume / 5);
serverQueue.textChannel.send(`**${song.title}**, is now playing!`);
}
client.on('message', message => {
if (message.content === 'help') {
let helpEmbed = new Discord.RichEmbed()
.setTitle('**أوامر الميوزك...**')
.setDescription('**برفكس البوت (!)**')
.addField('play', 'لتشغيل اغنية')
.addField('join', 'دخول رومك الصوتي')
.addField('disconnect', 'الخروج من رومك الصوتي')
.addField('skip', 'تخطي الأغنية')
.addField('pause', 'ايقاف الاغنية مؤقتا')
.addField('resume', 'تكملة الاغنية')
.addField('queue', 'اظهار قائمة التشغيل')
.addField('np', 'اظهار الاغنية اللي انت مشغلها حاليا')
.setFooter('(general_commands) لاظهار الاوامر العامة')
message.channel.send(helpEmbed);
}
});
client.on('message', message => {
if (message.content === 'general_commands') {
let helpEmbed = new Discord.RichEmbed()
.setTitle('**أوامر عامة...**')
.addField('avatar', "افاتار الشخص المطلوب")
.addField('gif', 'البحث عن جيف انت تطلبه')
.addField('ping', 'معرفة ping البوت')
.setFooter('المزيد قريبا ان شاء الله!')
message.channel.send(helpEmbed);
}
});
client.login(process.env.BOT_TOKEN);