Clear command and improved playNextInQueue

This commit is contained in:
Wizzard 2024-08-17 15:16:17 -04:00
parent 4752e52480
commit 2836e859f5
2 changed files with 41 additions and 16 deletions

17
commands/clear.js Normal file
View File

@ -0,0 +1,17 @@
const { clearQueue, getQueue } = require('../utils/queueManager');
module.exports = {
name: 'clear',
description: 'Clears the entire queue, except for the currently playing song.',
execute(message) {
const guildId = message.guild.id;
const queue = getQueue(guildId);
if (queue.length === 0) {
return message.reply('The queue is already empty!');
}
clearQueue(guildId);
message.channel.send('The queue has been cleared, except for the currently playing song.');
}
};

View File

@ -29,27 +29,31 @@ function toggleRepeat(guildId) {
function playNextInQueue(guildId, voiceChannel) { function playNextInQueue(guildId, voiceChannel) {
const queue = getQueue(guildId); const queue = getQueue(guildId);
let currentTrack = getCurrentTrack(guildId); const currentTrack = getCurrentTrack(guildId);
const repeat = repeatMap.get(guildId); const repeat = repeatMap.get(guildId);
if (currentTrack && repeat) { if (currentTrack && !repeat) {
playTrack(guildId, voiceChannel, currentTrack);
return;
}
if (queue.length === 0 && !repeat) {
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
if (connection && connection.state.status !== 'destroyed') {
connection.destroy();
}
return false; return false;
} }
if (queue.length > 0) { let trackToPlay = currentTrack;
currentTrack = queue.shift();
currentTrackMap.set(guildId, currentTrack); if (!trackToPlay) {
playTrack(guildId, voiceChannel, currentTrack); if (queue.length > 0) {
trackToPlay = queue.shift();
currentTrackMap.set(guildId, trackToPlay);
} else if (repeat && currentTrack) {
trackToPlay = currentTrack;
} else {
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
if (connection && connection.state.status !== 'destroyed') {
connection.destroy();
}
return false;
}
} }
playTrack(guildId, voiceChannel, trackToPlay);
} }
function playTrack(guildId, voiceChannel, track) { function playTrack(guildId, voiceChannel, track) {
@ -120,4 +124,8 @@ function skipTrack(guildId, voiceChannel) {
} }
} }
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat }; function clearQueue(guildId) {
queueMap.set(guildId, []);
}
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat, clearQueue };