-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathplay.js
259 lines (239 loc) Β· 16 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
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
const { Util, MessageEmbed } = require("discord.js");
const { TrackUtils, Player } = require("erela.js");
const prettyMilliseconds = require("pretty-ms");
module.exports = {
name: "play",
description: "Play your favorite songs",
usage: "[Song Name|Song URL]",
permissions: {
channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"],
member: [],
},
aliases: ["p"],
/**
*
* @param {import("../structures/DiscordMusicBot")} client
* @param {import("discord.js").Message} message
* @param {string[]} args
* @param {*} param3
*/
run: async (client, message, args, { GuildDB }) => {
if (!message.member.voice.channel) return client.sendTime(message.channel, "β | **You must be in a voice channel to play something!**");
//else if(message.guild.me.voice && message.guild.me.voice.channel.id !== message.member.voice.channel.id)return client.sendTime(message.channel, "β | **You must be in same voice channel as the bot is in to play something!**");
let SearchString = args.join(" ");
if (!SearchString) return client.sendTime(message.channel, `**Usage - **\`${GuildDB.prefix}play [Song Name|Song URL]\``);
let CheckNode = client.Manager.nodes.get(client.config.Lavalink.id);
let Searching = await message.channel.send(":mag_right: Searching...");
if (!CheckNode || !CheckNode.connected) {
return client.sendTime(message.channel,"β | Lavalink node not connected.");
}
const player = client.Manager.create({
guild: message.guild.id,
voiceChannel: message.member.voice.channel.id,
textChannel: message.channel.id,
selfDeafen: false,
});
let SongAddedEmbed = new MessageEmbed().setColor("RANDOM");
if (!player) return client.sendTime(message.channel, "β | **Nothing is playing right now...**");
if (player.state != "CONNECTED") await player.connect();
try {
if (SearchString.match(client.Lavasfy.spotifyPattern)) {
await client.Lavasfy.requestToken();
let node = client.Lavasfy.nodes.get(client.config.Lavalink.id);
let Searched = await node.load(SearchString);
if (Searched.loadType === "PLAYLIST_LOADED") {
let songs = [];
for (let i = 0; i < Searched.tracks.length; i++) songs.push(TrackUtils.build(Searched.tracks[i], message.author));
player.queue.add(songs);
if (!player.playing && !player.paused && player.queue.totalSize === Searched.tracks.length) player.play();
SongAddedEmbed.setAuthor(`Playlist added to queue`, message.author.displayAvatarURL());
SongAddedEmbed.addField("Enqueued", `\`${Searched.tracks.length}\` songs`, false);
//SongAddedEmbed.addField("Playlist duration", `\`${prettyMilliseconds(Searched.tracks, { colonNotation: true })}\``, false)
Searching.edit(SongAddedEmbed);
} else if (Searched.loadType.startsWith("TRACK")) {
player.queue.add(TrackUtils.build(Searched.tracks[0], message.author));
if (!player.playing && !player.paused && !player.queue.size) player.play();
SongAddedEmbed.setAuthor(`Added to queue`, client.config.IconURL);
SongAddedEmbed.setDescription(`[${Searched.tracks[0].info.title}](${Searched.tracks[0].info.uri})`);
SongAddedEmbed.addField("Author", Searched.tracks[0].info.author, true);
//SongAddedEmbed.addField("Duration", `\`${prettyMilliseconds(Searched.tracks[0].length, { colonNotation: true })}\``, true);
if (player.queue.totalSize > 1) SongAddedEmbed.addField("Position in queue", `${player.queue.size - 0}`, true);
Searching.edit(SongAddedEmbed);
} else {
return client.sendTime(message.channel, "**No matches found for - **" + SearchString);
}
} else {
let Searched = await player.search(SearchString, message.author);
if (!player) return client.sendTime(message.channel, "β | **Nothing is playing right now...**");
if (Searched.loadType === "NO_MATCHES") return client.sendTime(message.channel, "**No matches found for - **" + SearchString);
else if (Searched.loadType == "PLAYLIST_LOADED") {
player.queue.add(Searched.tracks);
if (!player.playing && !player.paused && player.queue.totalSize === Searched.tracks.length) player.play();
SongAddedEmbed.setAuthor(`Playlist added to queue`, client.config.IconURL);
SongAddedEmbed.setThumbnail(Searched.tracks[0].displayThumbnail());
SongAddedEmbed.setDescription(`[${Searched.playlist.name}](${SearchString})`);
SongAddedEmbed.addField("Enqueued", `\`${Searched.tracks.length}\` songs`, false);
SongAddedEmbed.addField("Playlist duration", `\`${prettyMilliseconds(Searched.playlist.duration, { colonNotation: true })}\``, false);
Searching.edit(SongAddedEmbed);
} else {
player.queue.add(Searched.tracks[0]);
if (!player.playing && !player.paused && !player.queue.size) player.play();
SongAddedEmbed.setAuthor(`Added to queue`, client.config.IconURL);
SongAddedEmbed.setThumbnail(Searched.tracks[0].displayThumbnail());
SongAddedEmbed.setDescription(`[${Searched.tracks[0].title}](${Searched.tracks[0].uri})`);
SongAddedEmbed.addField("Author", Searched.tracks[0].author, true);
SongAddedEmbed.addField("Duration", `\`${prettyMilliseconds(Searched.tracks[0].duration, { colonNotation: true })}\``, true);
if (player.queue.totalSize > 1) SongAddedEmbed.addField("Position in queue", `${player.queue.size - 0}`, true);
Searching.edit(SongAddedEmbed);
}
}
} catch (e) {
console.log(e);
return client.sendTime(message.channel, "**No matches found for - **" + SearchString);
}
},
SlashCommand: {
options: [
{
name: "query",
value: "query",
type: 3,
required: true,
description: "Play music in the voice channel",
},
],
/**
*
* @param {import("../structures/DiscordMusicBot")} client
* @param {import("discord.js").Message} message
* @param {string[]} args
* @param {*} param3
*/
run: async (client, interaction, args, { GuildDB }) => {
const guild = client.guilds.cache.get(interaction.guild_id);
const member = guild.members.cache.get(interaction.member.user.id);
const voiceChannel = member.voice.channel;
let awaitchannel = client.channels.cache.get(interaction.channel_id); /// thanks Reyansh for this idea ;-;
if (!member.voice.channel) return client.sendTime(interaction, "β | **You must be in a voice channel to use this command.**");
if (guild.me.voice.channel && !guild.me.voice.channel.equals(member.voice.channel)) return client.sendTime(interaction, `β | **You must be in ${guild.me.voice.channel} to use this command.**`);
let CheckNode = client.Manager.nodes.get(client.config.Lavalink.id);
if (!CheckNode || !CheckNode.connected) {
return client.sendTime(interaction,"β | Lavalink node not connected.");
}
let player = client.Manager.create({
guild: interaction.guild_id,
voiceChannel: voiceChannel.id,
textChannel: interaction.channel_id,
selfDeafen: false,
});
if (player.state != "CONNECTED") await player.connect();
let search = interaction.data.options[0].value;
let res;
if (search.match(client.Lavasfy.spotifyPattern)) {
await client.Lavasfy.requestToken();
let node = client.Lavasfy.nodes.get(client.config.Lavalink.id);
let Searched = await node.load(search);
switch (Searched.loadType) {
case "LOAD_FAILED":
if (!player.queue.current) player.destroy();
return client.sendError(interaction, `β | **There was an error while searching**`);
case "NO_MATCHES":
if (!player.queue.current) player.destroy();
return client.sendTime(interaction, "β | **No results were found.**");
case "TRACK_LOADED":
player.queue.add(TrackUtils.build(Searched.tracks[0], member.user));
if (!player.playing && !player.paused && !player.queue.length) player.play();
let SongAddedEmbed = new MessageEmbed();
SongAddedEmbed.setAuthor(`Added to queue`, client.config.IconURL);
SongAddedEmbed.setColor("RANDOM");
SongAddedEmbed.setDescription(`[${Searched.tracks[0].info.title}](${Searched.tracks[0].info.uri})`);
SongAddedEmbed.addField("Author", Searched.tracks[0].info.author, true);
if (player.queue.totalSize > 1) SongAddedEmbed.addField("Position in queue", `${player.queue.size - 0}`, true);
return interaction.send(SongAddedEmbed);
case "SEARCH_RESULT":
player.queue.add(TrackUtils.build(Searched.tracks[0], member.user));
if (!player.playing && !player.paused && !player.queue.length) player.play();
let SongAdded = new MessageEmbed();
SongAdded.setAuthor(`Added to queue`, client.config.IconURL);
SongAdded.setColor("RANDOM");
SongAdded.setDescription(`[${Searched.tracks[0].info.title}](${Searched.tracks[0].info.uri})`);
SongAdded.addField("Author", Searched.tracks[0].info.author, true);
if (player.queue.totalSize > 1) SongAdded.addField("Position in queue", `${player.queue.size - 0}`, true);
return interaction.send(SongAdded);
case "PLAYLIST_LOADED":
let songs = [];
for (let i = 0; i < Searched.tracks.length; i++) songs.push(TrackUtils.build(Searched.tracks[i], member.user));
player.queue.add(songs);
if (!player.playing && !player.paused && player.queue.totalSize === Searched.tracks.length) player.play();
let Playlist = new MessageEmbed();
Playlist.setAuthor(`Playlist added to queue`, client.config.IconURL);
Playlist.setDescription(`[${Searched.playlistInfo.name}](${interaction.data.options[0].value})`);
Playlist.addField("Enqueued", `\`${Searched.tracks.length}\` songs`, false);
return interaction.send(Playlist);
}
} else {
try {
res = await player.search(search, member.user);
if (res.loadType === "LOAD_FAILED") {
if (!player.queue.current) player.destroy();
return interaction.send(`There was an error while searching`);
}
} catch (err) {
return client.sendTime(interaction, `There was an error while searching: ${err.message}`);
}
switch (res.loadType) {
case "NO_MATCHES":
if (!player.queue.current) player.destroy();
return client.sendTime(interaction, "β | **No results were found.**");
case "TRACK_LOADED":
player.queue.add(res.tracks[0]);
if (!player.playing && !player.paused && !player.queue.length) player.play();
let SongAddedEmbed = new MessageEmbed();
SongAddedEmbed.setAuthor(`Added to queue`, client.config.IconURL);
SongAddedEmbed.setThumbnail(res.tracks[0].displayThumbnail());
SongAddedEmbed.setColor("RANDOM");
SongAddedEmbed.setDescription(`[${res.tracks[0].title}](${res.tracks[0].uri})`);
SongAddedEmbed.addField("Author", res.tracks[0].author, true);
SongAddedEmbed.addField("Duration", `\`${prettyMilliseconds(res.tracks[0].duration, { colonNotation: true })}\``, true);
if (player.queue.totalSize > 1) SongAddedEmbed.addField("Position in queue", `${player.queue.size - 0}`, true);
return interaction.send(SongAddedEmbed);
case "PLAYLIST_LOADED":
player.queue.add(res.tracks);
await player.play();
let SongAdded = new MessageEmbed();
SongAdded.setAuthor(`Playlist added to queue`, client.config.IconURL);
SongAdded.setThumbnail(res.tracks[0].displayThumbnail());
SongAdded.setDescription(`[${res.playlist.name}](${interaction.data.options[0].value})`);
SongAdded.addField("Enqueued", `\`${res.tracks.length}\` songs`, false);
SongAdded.addField("Playlist duration", `\`${prettyMilliseconds(res.playlist.duration, { colonNotation: true })}\``, false);
return interaction.send(SongAdded);
case "SEARCH_RESULT":
const track = res.tracks[0];
player.queue.add(track);
if (!player.playing && !player.paused && !player.queue.length) {
let SongAddedEmbed = new MessageEmbed();
SongAddedEmbed.setAuthor(`Added to queue`, client.config.IconURL);
SongAddedEmbed.setThumbnail(track.displayThumbnail());
SongAddedEmbed.setColor("RANDOM");
SongAddedEmbed.setDescription(`[${track.title}](${track.uri})`);
SongAddedEmbed.addField("Author", track.author, true);
SongAddedEmbed.addField("Duration", `\`${prettyMilliseconds(track.duration, { colonNotation: true })}\``, true);
if (player.queue.totalSize > 1) SongAddedEmbed.addField("Position in queue", `${player.queue.size - 0}`, true);
player.play();
return interaction.send(SongAddedEmbed);
} else {
let SongAddedEmbed = new MessageEmbed();
SongAddedEmbed.setAuthor(`Added to queue`, client.config.IconURL);
SongAddedEmbed.setThumbnail(track.displayThumbnail());
SongAddedEmbed.setColor("RANDOM");
SongAddedEmbed.setDescription(`[${track.title}](${track.uri})`);
SongAddedEmbed.addField("Author", track.author, true);
SongAddedEmbed.addField("Duration", `\`${prettyMilliseconds(track.duration, { colonNotation: true })}\``, true);
if (player.queue.totalSize > 1) SongAddedEmbed.addField("Position in queue", `${player.queue.size - 0}`, true);
interaction.send(SongAddedEmbed);
}
}
}
},
},
};