-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathplayer.js
374 lines (321 loc) · 14.7 KB
/
player.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
const { Riffy } = require("riffy");
const { EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle, AttachmentBuilder, PermissionsBitField } = require("discord.js");
const { queueNames, requesters } = require("./commands/play");
const { Dynamic } = require("musicard");
const config = require("./config.js");
const musicIcons = require('./UI/icons/musicicons.js');
const colors = require('./UI/colors/colors');
const fs = require("fs");
const path = require("path");
const { autoplayCollection } = require('./mongodb.js');
async function sendMessageWithPermissionsCheck(channel, embed, attachment, actionRow1, actionRow2) {
try {
const permissions = channel.permissionsFor(channel.guild.members.me);
if (!permissions.has(PermissionsBitField.Flags.SendMessages) ||
!permissions.has(PermissionsBitField.Flags.EmbedLinks) ||
!permissions.has(PermissionsBitField.Flags.AttachFiles) ||
!permissions.has(PermissionsBitField.Flags.UseExternalEmojis)) {
console.error("Bot lacks necessary permissions to send messages in this channel.");
return;
}
const message = await channel.send({
embeds: [embed],
files: [attachment],
components: [actionRow1, actionRow2]
});
return message;
} catch (error) {
console.error("Error sending message:", error.message);
const errorEmbed = new EmbedBuilder()
.setColor('#FF0000')
.setDescription("⚠️ **Unable to send message. Check bot permissions.**");
await channel.send({ embeds: [errorEmbed] });
}
}
function initializePlayer(client) {
const nodes = config.nodes.map(node => ({
name: node.name,
host: node.host,
port: node.port,
password: node.password,
secure: node.secure,
reconnectTimeout: 5000,
reconnectTries: Infinity
}));
client.riffy = new Riffy(client, nodes, {
send: (payload) => {
const guildId = payload.d.guild_id;
if (!guildId) return;
const guild = client.guilds.cache.get(guildId);
if (guild) guild.shard.send(payload);
},
defaultSearchPlatform: "ytmsearch",
restVersion: "v4",
});
let currentTrackMessageId = null;
let collector = null;
client.riffy.on("nodeConnect", node => {
console.log(`${colors.cyan}[ LAVALINK ]${colors.reset} ${colors.green}Node ${node.name} Connected ✅${colors.reset}`);
});
client.riffy.on("nodeError", (node, error) => {
console.log(`${colors.cyan}[ LAVALINK ]${colors.reset} ${colors.red}Node ${node.name} Error ❌ | ${error.message}${colors.reset}`);
});
client.riffy.on("trackStart", async (player, track) => {
const channel = client.channels.cache.get(player.textChannel);
const trackUri = track.info.uri;
const requester = requesters.get(trackUri);
try {
const musicard = await Dynamic({
thumbnailImage: track.info.thumbnail || 'https://example.com/default_thumbnail.png',
backgroundColor: '#070707',
progress: 10,
progressColor: '#FF7A00',
progressBarColor: '#5F2D00',
name: track.info.title,
nameColor: '#FF7A00',
author: track.info.author || 'Unknown Artist',
authorColor: '#696969',
});
// Save the generated card to a file
const cardPath = path.join(__dirname, 'musicard.png');
fs.writeFileSync(cardPath, musicard);
// Prepare the attachment and embed
const attachment = new AttachmentBuilder(cardPath, { name: 'musicard.png' });
const embed = new EmbedBuilder()
.setAuthor({
name: 'Playing Song..',
iconURL: musicIcons.playerIcon,
url: config.SupportServer
})
.setFooter({ text: `Developed by SSRR | Prime Music v1.2`, iconURL: musicIcons.heartIcon })
.setTimestamp()
.setDescription(
`- **Title:** [${track.info.title}](${track.info.uri})\n` +
`- **Author:** ${track.info.author || 'Unknown Artist'}\n` +
`- **Length:** ${formatDuration(track.info.length)}\n` +
`- **Requester:** ${requester}\n` +
`- **Source:** ${track.info.sourceName}\n` + '**- Controls :**\n 🔁 `Loop`, ❌ `Disable`, ⏭️ `Skip`, 📜 `Queue`, 🗑️ `Clear`\n ⏹️ `Stop`, ⏸️ `Pause`, ▶️ `Resume`, 🔊 `Vol +`, 🔉 `Vol -`')
.setImage('attachment://musicard.png')
.setColor('#FF7A00');
const actionRow1 = createActionRow1(false);
const actionRow2 = createActionRow2(false);
const message = await sendMessageWithPermissionsCheck(channel, embed, attachment, actionRow1, actionRow2);
if (message) {
currentTrackMessageId = message.id;
if (collector) collector.stop();
collector = setupCollector(client, player, channel, message);
}
} catch (error) {
console.error("Error creating or sending music card:", error.message);
const errorEmbed = new EmbedBuilder()
.setColor('#FF0000')
.setDescription("⚠️ **Unable to load track card. Continuing playback...**");
await channel.send({ embeds: [errorEmbed] });
}
});
client.riffy.on("trackEnd", async (player) => {
await disableTrackMessage(client, player);
currentTrackMessageId = null;
});
client.riffy.on("playerDisconnect", async (player) => {
await disableTrackMessage(client, player);
currentTrackMessageId = null;
});
client.riffy.on("queueEnd", async (player) => {
const channel = client.channels.cache.get(player.textChannel);
const guildId = player.guildId;
try {
const autoplaySetting = await autoplayCollection.findOne({ guildId });
if (autoplaySetting?.autoplay) {
//console.log(`Autoplay is enabled for guild: ${guildId}`);
const nextTrack = await player.autoplay(player);
if (!nextTrack) {
player.destroy();
await channel.send("⚠️ **No more tracks to autoplay. Disconnecting...**");
}
} else {
console.log(`Autoplay is disabled for guild: ${guildId}`);
player.destroy();
await channel.send("🎶 **Queue has ended. Autoplay is disabled.**");
}
} catch (error) {
console.error("Error handling autoplay:", error);
player.destroy();
await channel.send("👾**Queue Empty! Disconnecting...**");
}
});
async function disableTrackMessage(client, player) {
const channel = client.channels.cache.get(player.textChannel);
if (!channel || !currentTrackMessageId) return;
try {
const message = await channel.messages.fetch(currentTrackMessageId);
if (message) {
const disabledRow1 = createActionRow1(true);
const disabledRow2 = createActionRow2(true);
await message.edit({ components: [disabledRow1, disabledRow2] });
}
} catch (error) {
console.error("Failed to disable message components:", error);
}
}
}
function formatDuration(ms) {
const seconds = Math.floor((ms / 1000) % 60);
const minutes = Math.floor((ms / (1000 * 60)) % 60);
const hours = Math.floor((ms / (1000 * 60 * 60)) % 24);
return [
hours > 0 ? `${hours}h` : null,
minutes > 0 ? `${minutes}m` : null,
`${seconds}s`,
]
.filter(Boolean)
.join(' ');
}
function setupCollector(client, player, channel, message) {
const filter = i => [
'loopToggle', 'skipTrack', 'disableLoop', 'showQueue', 'clearQueue',
'stopTrack', 'pauseTrack', 'resumeTrack', 'volumeUp', 'volumeDown'
].includes(i.customId);
const collector = message.createMessageComponentCollector({ filter, time: 600000 }); // Set timeout if desired
collector.on('collect', async i => {
await i.deferUpdate();
const member = i.member;
const voiceChannel = member.voice.channel;
const playerChannel = player.voiceChannel;
if (!voiceChannel || voiceChannel.id !== playerChannel) {
const vcEmbed = new EmbedBuilder()
.setColor(config.embedColor)
.setDescription('🔒 **You need to be in the same voice channel to use the controls!**');
const sentMessage = await channel.send({ embeds: [vcEmbed] });
setTimeout(() => sentMessage.delete().catch(console.error), config.embedTimeout * 1000);
return;
}
handleInteraction(i, player, channel);
});
collector.on('end', () => {
console.log("Collector stopped.");
});
return collector;
}
async function handleInteraction(i, player, channel) {
switch (i.customId) {
case 'loopToggle':
toggleLoop(player, channel);
break;
case 'skipTrack':
player.stop();
await sendEmbed(channel, "⏭️ **Player will play the next song!**");
break;
case 'disableLoop':
disableLoop(player, channel);
break;
case 'showQueue':
showQueue(channel);
break;
case 'clearQueue':
player.queue.clear();
await sendEmbed(channel, "🗑️ **Queue has been cleared!**");
break;
case 'stopTrack':
player.stop();
player.destroy();
await sendEmbed(channel, '⏹️ **Playback has been stopped and player destroyed!**');
break;
case 'pauseTrack':
if (player.paused) {
await sendEmbed(channel, '⏸️ **Playback is already paused!**');
} else {
player.pause(true);
await sendEmbed(channel, '⏸️ **Playback has been paused!**');
}
break;
case 'resumeTrack':
if (!player.paused) {
await sendEmbed(channel, '▶️ **Playback is already resumed!**');
} else {
player.pause(false);
await sendEmbed(channel, '▶️ **Playback has been resumed!**');
}
break;
case 'volumeUp':
adjustVolume(player, channel, 10);
break;
case 'volumeDown':
adjustVolume(player, channel, -10);
break;
}
}
async function sendEmbed(channel, message) {
const embed = new EmbedBuilder().setColor(config.embedColor).setDescription(message);
const sentMessage = await channel.send({ embeds: [embed] });
setTimeout(() => sentMessage.delete().catch(console.error), config.embedTimeout * 1000);
}
function adjustVolume(player, channel, amount) {
const newVolume = Math.min(100, Math.max(10, player.volume + amount));
if (newVolume === player.volume) {
sendEmbed(channel, amount > 0 ? '🔊 **Volume is already at maximum!**' : '🔉 **Volume is already at minimum!**');
} else {
player.setVolume(newVolume);
sendEmbed(channel, `🔊 **Volume changed to ${newVolume}%!**`);
}
}
function formatTrack(track) {
if (!track || typeof track !== 'string') return track;
const match = track.match(/\[(.*?) - (.*?)\]\((.*?)\)/);
if (match) {
const [, title, author, uri] = match;
return `[${title} - ${author}](${uri})`;
}
return track;
}
function toggleLoop(player, channel) {
player.setLoop(player.loop === "track" ? "queue" : "track");
sendEmbed(channel, player.loop === "track" ? "🔁 **Track loop is activated!**" : "🔁 **Queue loop is activated!**");
}
function disableLoop(player, channel) {
player.setLoop("none");
sendEmbed(channel, "❌ **Loop is disabled!**");
}
function showQueue(channel) {
if (queueNames.length === 0) {
sendEmbed(channel, "The queue is empty.");
return;
}
const queueChunks = [];
for (let i = 1; i < queueNames.length; i += 10) {
const chunk = queueNames.slice(i, i + 10)
.map((song, index) => `${i + index}. ${formatTrack(song)}`)
.join('\n');
queueChunks.push(chunk);
}
channel.send({
embeds: [new EmbedBuilder().setColor(config.embedColor).setDescription(nowPlaying)]
}).catch(console.error);
queueChunks.forEach(async (chunk) => {
const embed = new EmbedBuilder()
.setColor(config.embedColor)
.setDescription(`📜 **Queue:**\n${chunk}`);
await channel.send({ embeds: [embed] }).catch(console.error);
});
}
function createActionRow1(disabled) {
return new ActionRowBuilder()
.addComponents(
new ButtonBuilder().setCustomId("loopToggle").setEmoji('🔁').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("disableLoop").setEmoji('❌').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("skipTrack").setEmoji('⏭️').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("showQueue").setEmoji('📜').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("clearQueue").setEmoji('🗑️').setStyle(ButtonStyle.Secondary).setDisabled(disabled)
);
}
function createActionRow2(disabled) {
return new ActionRowBuilder()
.addComponents(
new ButtonBuilder().setCustomId("stopTrack").setEmoji('⏹️').setStyle(ButtonStyle.Danger).setDisabled(disabled),
new ButtonBuilder().setCustomId("pauseTrack").setEmoji('⏸️').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("resumeTrack").setEmoji('▶️').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("volumeUp").setEmoji('🔊').setStyle(ButtonStyle.Secondary).setDisabled(disabled),
new ButtonBuilder().setCustomId("volumeDown").setEmoji('🔉').setStyle(ButtonStyle.Secondary).setDisabled(disabled)
);
}
module.exports = { initializePlayer };