From 2836e859f57cc5a3b15f94d0f14bb1a3ce9b8cab Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 15:16:17 -0400 Subject: [PATCH] Clear command and improved playNextInQueue --- commands/clear.js | 17 +++++++++++++++++ utils/queueManager.js | 40 ++++++++++++++++++++++++---------------- 2 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 commands/clear.js diff --git a/commands/clear.js b/commands/clear.js new file mode 100644 index 0000000..da3545e --- /dev/null +++ b/commands/clear.js @@ -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.'); + } +}; \ No newline at end of file diff --git a/utils/queueManager.js b/utils/queueManager.js index f7f44ea..e5ad360 100644 --- a/utils/queueManager.js +++ b/utils/queueManager.js @@ -29,27 +29,31 @@ function toggleRepeat(guildId) { function playNextInQueue(guildId, voiceChannel) { const queue = getQueue(guildId); - let currentTrack = getCurrentTrack(guildId); + const currentTrack = getCurrentTrack(guildId); const repeat = repeatMap.get(guildId); - 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(); - } + if (currentTrack && !repeat) { return false; } - if (queue.length > 0) { - currentTrack = queue.shift(); - currentTrackMap.set(guildId, currentTrack); - playTrack(guildId, voiceChannel, currentTrack); + let trackToPlay = currentTrack; + + if (!trackToPlay) { + 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) { @@ -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 }; \ No newline at end of file