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
|
import { Message } from "discord.js";
import { AkairoClient } from "discord-akairo";
import ytdlDiscord from 'ytdl-core-discord';
import * as scdl from 'soundcloud-downloader';
import { StreamDispatcher } from "discord.js";
import { MessageCollector } from "discord.js";
import Util from '../utils/Utils';
module.exports = {
async play(song, client: AkairoClient, msg: Message) {
const { PRUNING, SOUNDCLOUD_CLIENT_ID } = require('../Config');
const queue = client.queue.get(msg.guild.id);
if (!song) {
queue.channel.leave();
client.queue.delete(msg.guild.id);
return queue.channel.send('Music queue ended.'); // catch
}
let stream = null;
try {
if (song.url.includes('youtube.com')) {
stream = await ytdlDiscord(song.url, { highWaterMark: 1 << 25 });
} else if (song.url.includes('soundcloud.com') && SOUNDCLOUD_CLIENT_ID) {
stream = await scdl.downloadFormat(song.url, scdl.FORMATS.OPUS, SOUNDCLOUD_CLIENT_ID);
}
} catch (error) {
if (queue) {
queue.songs.shift();
module.exports.play(queue.songs[0], msg);
}
// error
return msg.channel.send(`Error: ${error.message ? error.message : error}`);
}
queue.connection.on('disconnect', () => client.queue.delete(msg.guild.id));
const type = song.url.includes('youtube.com') ? 'opus' : 'ogg/opus';
const dispatcher: StreamDispatcher = queue.connection
.play(stream, { type: type })
.on('finish', () => {
if (collector && !collector.ended) collector.stop();
if (queue.loop) {
let lastSong = queue.songs.shift();
queue.songs.push(lastSong);
// export
} else {
queue.songs.shift();
// export
}
})
.on('error', (err) => {
// error
queue.songs.shift();
module.exports.play(queue.songs[0], msg);
});
dispatcher.setVolumeLogarithmic(queue.volume / 100);
try {
var playingMessage = await queue.channel.send(`Started playing: **${song.title}** ${song.url}`);
await playingMessage.react("⏭");
await playingMessage.react("⏯");
await playingMessage.react("🔁");
await playingMessage.react("⏹");
} catch (error) {
// error
}
const filter = (reaction, user) => user.id !== client.user.id;
var collector: MessageCollector = playingMessage.createReactionCollector(filter, {
time: song.duration > 0 ? song.duration * 1000 : 600000
});
collector.on('collect', (reaction, user) => {
if (!queue) return;
const member = msg.guild.member(user);
switch (reaction.emoji.name) {
case "⏭":
queue.playing = true;
reaction.users.remove(user).catch(console.error);
if (!Util.canModifyQueue(member)) return;
queue.connection.dispatcher.end();
queue.textChannel.send(`${user} ⏩ skipped the song`).catch(console.error);
collector.stop();
break;
case "⏯":
reaction.users.remove(user).catch(console.error);
if (!Util.canModifyQueue(member)) return;
if (queue.playing) {
queue.playing = !queue.playing;
queue.connection.dispatcher.pause(true);
queue.textChannel.send(`${user} ⏸ paused the music.`).catch(console.error);
} else {
queue.playing = !queue.playing;
queue.connection.dispatcher.resume();
queue.textChannel.send(`${user} ▶ resumed the music!`).catch(console.error);
}
break;
case "🔁":
reaction.users.remove(user).catch(console.error);
if (!Util.canModifyQueue(member)) return;
queue.loop = !queue.loop;
queue.textChannel.send(`Loop is now ${queue.loop ? "**on**" : "**off**"}`).catch(console.error);
break;
case "⏹":
reaction.users.remove(user).catch(console.error);
if (!Util.canModifyQueue(member)) return;
queue.songs = [];
queue.textChannel.send(`${user} ⏹ stopped the music!`).catch(console.error);
try {
queue.connection.dispatcher.end();
} catch (error) {
console.error(error);
queue.connection.disconnect();
}
collector.stop();
break;
default:
reaction.users.remove(user).catch(console.error);
break;
}
});
collector.on('end', () => {
playingMessage.reactions.removeAll(); // catch
if (PRUNING && playingMessage && !playingMessage.deleted)
playingMessage.delete({ timeout: 3000 }); // catch
});
}
}
|