-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
274 lines (233 loc) · 10.2 KB
/
index.js
File metadata and controls
274 lines (233 loc) · 10.2 KB
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
const http = require('http');
http.createServer(function(request,responce)
{
responce.writeHead(200, {'Content-Type': 'text/plain'});
}).listen(3000);
const { Client, Util } = require('discord.js');
const PREFIX = '/';
const GOOGLE_API_KEY = 'AIzaSyBqFMs2l5Pwrr3Yx19TCHq6lXhgT9HWYSE';
const COLOR = 'GOLD';
const STATUS = 'Chill Arena Zone 🎶';
const YouTube = require('simple-youtube-api');
const ytdl = require('ytdl-core');
const Discord = require("discord.js");
const client = new Client({ disableEveryone: true });
const youtube = new YouTube(GOOGLE_API_KEY);
const queue = new Map();
client.on('message', message => {
if (message.content === `${PREFIX}ping`) {
message.reply("Pong | ");
}
});
client.on('message', message => {
if (message.content === `${PREFIX}help`) {
var embed = new Discord.RichEmbed()
.setTitle('MUSIC COMMANDS')
.setColor(`GOLD`)
.setDescription(`${PREFIX}play, ${PREFIX}skip, ${PREFIX}np, ${PREFIX}volume, ${PREFIX}stop, ${PREFIX}resume, ${PREFIX}queue, ${PREFIX}pause, ${PREFIX}clean`);
message.channel.send(embed);
}
});
client.on('warn', console.warn);
client.on('error', console.error);
client.on('ready', () => {
console.log(`${client.user.tag} Yo this ready!`)
client.user.setActivity(`${STATUS}`);
});
client.on('disconnect', () => console.log('I just disconnected, making sure you know, I will reconnect now...'));
client.on('reconnecting', () => console.log('I am reconnecting now!'));
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] ? 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\'m sorry but you need to be in a voice channel to play music!');
const permissions = voiceChannel.permissionsFor(msg.client.user);
if (!permissions.has('CONNECT')) {
return msg.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
}
if (!permissions.has('SPEAK')) {
return msg.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
}
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); // eslint-disable-line no-await-in-loop
await handleVideo(video2, msg, voiceChannel, true); // eslint-disable-line no-await-in-loop
}
var embed = new Discord.RichEmbed()
.setTitle("Song Selection")
.setDescription(`✅ Song has been added to the queue!`)
.setColor(`${COLOR}`)
return msg.channel.send(embed);
} else {
try {
var video = await youtube.getVideo(url);
} catch (error) {
try {
var videos = await youtube.searchVideos(searchString, 10);
let index = 0;
var embed = new Discord.RichEmbed()
.setTitle("🎺 Song Selection ✔")
.setDescription(`${videos.map(video2 => `*${++index}* \`${video2.title}\` `).join('\n')}`)
.setColor(`${COLOR}`)
.setFooter("Please provide a value to select one of the search results ranging from 1-10.")
msg.channel.send(embed);
// eslint-disable-next-line max-depth
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);
return msg.channel.send('No or invalid value entered, cancelling video selection.');
}
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 could not obtain any search results.');
}
}
return handleVideo(video, msg, voiceChannel);
}
} else if (command === 'skip') {
if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
if (!serverQueue) return msg.channel.send('There is nothing playing that I could skip for you.');
serverQueue.connection.dispatcher.end('Skip command has been used!');
const embed = new Discord.RichEmbed()
.setTitle('Song')
.setColor(`${COLOR}`)
.setDescription('✅ Successfully skipped the song');
msg.channel.send(embed);
return undefined;
} else if (command === 'stop') {
if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
if (!serverQueue) return msg.channel.send('There is nothing playing that I could stop for you.');
serverQueue.songs = [];
serverQueue.connection.dispatcher.end('Stop command has been used!');
msg.reply("*bot has been stopped !*");
return undefined;
} else if (command === 'volume') {
if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
if (!serverQueue) return msg.channel.send('There is nothing playing.');
if (!args[1]) return msg.channel.send(`The current volume is: *${serverQueue.volume}*`);
serverQueue.volume = args[1];
serverQueue.connection.dispatcher.setVolumeLogarithmic(args[1] / 4);
return msg.channel.send(`I set the volume to: *${args[1]}*`);
} else if (command === 'clean') {
if (!msg.member.voiceChannel) return msg.channel.send('You are not in a voice channel!');
if (!serverQueue) return msg.channel.send('Quene is already empty');
msg.channel.send(`Cleared the queue`).then(m => serverQueue.connection.dispatcher.clear());
}
else if (command === 'np') {
var embed = new Discord.RichEmbed()
.setTitle("Song Detail")
.setDescription(`🎶 \`Now playing:\` *${serverQueue.songs[0].title}*`)
.setColor(`${COLOR}`)
if (!serverQueue) return msg.channel.send('There is nothing playing.');
return msg.channel.send(embed);
} else if (command === 'queue') {
if (!serverQueue) return msg.channel.send('There is nothing playing.');
var embed = new Discord.RichEmbed()
.setTitle("Song Queue")
.setDescription(`${serverQueue.songs.map(song => `*• * ${song.title}`).join('\n')}
🎵 \`Now playing:\` *${serverQueue.songs[0].title}*`)
.setColor(`${COLOR}`)
return msg.channel.send(embed);
} else if (command === 'pause') {
if (serverQueue && serverQueue.playing) {
serverQueue.playing = false;
serverQueue.connection.dispatcher.pause();
var embed = new Discord.RichEmbed()
.setTitle("Song")
.setDescription(`⏸ Paused the music for you!`)
.setColor(`${COLOR}`)
msg.channel.send(embed)
}
} else if (command === 'resume') {
if (serverQueue && !serverQueue.playing) {
serverQueue.playing = true;
serverQueue.connection.dispatcher.resume();
var embed = new Discord.RichEmbed()
.setTitle("Song")
.setDescription(`▶ Resumed the music for you!`)
.setColor(`${COLOR}`)
msg.channel.send(embed)
}
}
});
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: 10,
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(`I could not join the voice channel: ${error}`);
}
} else {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
if (playlist) return undefined;
var embed = new Discord.RichEmbed()
.setTitle("Song Selection")
.setDescription(`✅ Song has been added to the queue!`)
.setColor(`${COLOR}`)
return msg.channel.send(embed);
}
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,{filter: "audioonly"}))
.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 / 10);
var embed = new Discord.RichEmbed()
.setTitle("Song Selection")
.setDescription(`🎵 \`Start playing:\` *${song.title}*`)
.setColor(`${COLOR}`)
serverQueue.textChannel.send(embed);
}
client.login(process.env.TOKEN);