-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.js
74 lines (67 loc) · 2.49 KB
/
play.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
const { Util } = require('discord.js');
const ytdl = require('ytdl-core');
const ytdlDiscord = require('ytdl-core-discord');
module.exports = {
name: 'play',
description: 'Play command.',
usage: '[command name]',
args: true,
cooldown: 5,
async execute(message, args) {
const { voiceChannel } = message.member;
if (!voiceChannel) return message.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT')) return message.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
if (!permissions.has('SPEAK')) return message.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
const serverQueue = message.client.queue.get(message.guild.id);
const songInfo = await ytdl.getInfo(args[0]);
const song = {
id: songInfo.video_id,
title: Util.escapeMarkdown(songInfo.title),
url: songInfo.video_url
};
if (serverQueue) {
serverQueue.songs.push(song);
console.log(serverQueue.songs);
return message.channel.send(`✅ **${song.title}** has been added to the queue!`);
}
const queueConstruct = {
textChannel: message.channel,
voiceChannel,
connection: null,
songs: [],
volume: 2,
playing: true
};
message.client.queue.set(message.guild.id, queueConstruct);
queueConstruct.songs.push(song);
const play = async song => {
const queue = message.client.queue.get(message.guild.id);
if (!song) {
queue.voiceChannel.leave();
message.client.queue.delete(message.guild.id);
return;
}
const dispatcher = queue.connection.playOpusStream(await ytdlDiscord(song.url), { passes: 3 })
.on('end', reason => {
if (reason === 'Stream is not generating quickly enough.') console.log('Song ended.');
else console.log(reason);
queue.songs.shift();
play(queue.songs[0]);
})
.on('error', error => console.error(error));
dispatcher.setVolumeLogarithmic(queue.volume / 5);
queue.textChannel.send(`🎶 Start playing: **${song.title}**`);
};
try {
const connection = await voiceChannel.join();
queueConstruct.connection = connection;
play(queueConstruct.songs[0]);
} catch (error) {
console.error(`I could not join the voice channel: ${error}`);
message.client.queue.delete(message.guild.id);
await voiceChannel.leave();
return message.channel.send(`I could not join the voice channel: ${error}`);
}
}
};