From 7e63d56025d94b57cb48bbbba6beb6d76e43df81 Mon Sep 17 00:00:00 2001 From: Wizzard Date: Sat, 17 Aug 2024 14:58:17 -0400 Subject: [PATCH] Change a lot repeat command --- commands/queue.js | 4 +-- commands/repeat.js | 11 +++++++ commands/skip.js | 2 +- utils/queueManager.js | 68 ++++++++++++++++++++++++++----------------- 4 files changed, 56 insertions(+), 29 deletions(-) create mode 100644 commands/repeat.js diff --git a/commands/queue.js b/commands/queue.js index b798e97..ac786ef 100644 --- a/commands/queue.js +++ b/commands/queue.js @@ -12,13 +12,13 @@ module.exports = { .setTitle('Current Queue'); if (currentTrack) { - embed.addFields({ name: 'Currently playing', value: `${currentTrack}`, inline: false }); + embed.addFields({ name: 'Currently playing', value: `${currentTrack.title}`, inline: false }); } if (queue.length > 0) { embed.addFields({ name: 'Up next', - value: queue.map((track, index) => `${index + 1}. ${track}`).join('\n'), + value: queue.map((track, index) => `${index + 1}. ${track.title}`).join('\n'), inline: false }); } else if (!currentTrack) { diff --git a/commands/repeat.js b/commands/repeat.js new file mode 100644 index 0000000..0f10e58 --- /dev/null +++ b/commands/repeat.js @@ -0,0 +1,11 @@ +const { toggleRepeat } = require('../utils/queueManager'); + +module.exports = { + name: 'repeat', + description: 'Toggle repeat mode for the currently playing track', + execute(message) { + const guildId = message.guild.id; + const isRepeatOn = toggleRepeat(guildId); + message.channel.send(`Repeat mode is now ${isRepeatOn ? 'ON' : 'OFF'}.`); + }, +}; \ No newline at end of file diff --git a/commands/skip.js b/commands/skip.js index f5a8407..0ed38d2 100644 --- a/commands/skip.js +++ b/commands/skip.js @@ -10,7 +10,7 @@ module.exports = { return message.reply('You need to be in a voice channel to skip music!'); } - skipTrack(guildId); + skipTrack(guildId, voiceChannel); message.channel.send('Skipped the current track!'); } }; \ No newline at end of file diff --git a/utils/queueManager.js b/utils/queueManager.js index 4bfd8b8..9453c26 100644 --- a/utils/queueManager.js +++ b/utils/queueManager.js @@ -4,12 +4,13 @@ const fs = require('fs'); const queueMap = new Map(); const playerMap = new Map(); const currentTrackMap = new Map(); +const repeatMap = new Map(); -function addToQueue(guildId, filePath) { +function addToQueue(guildId, filePath, title) { if (!queueMap.has(guildId)) { queueMap.set(guildId, []); } - queueMap.get(guildId).push(filePath); + queueMap.get(guildId).push({ filePath, title }); } function getQueue(guildId) { @@ -20,45 +21,54 @@ function getCurrentTrack(guildId) { return currentTrackMap.get(guildId) || null; } +function toggleRepeat(guildId) { + const currentRepeat = repeatMap.get(guildId) || false; + repeatMap.set(guildId, !currentRepeat); + return !currentRepeat; +} + function playNextInQueue(guildId, voiceChannel) { const queue = getQueue(guildId); + const currentTrack = getCurrentTrack(guildId); + + if (currentTrack) { + return false; + } if (queue.length === 0) return false; - const currentTrack = getCurrentTrack(guildId); - if (currentTrack) return false; - const connection = joinVoiceChannel({ channelId: voiceChannel.id, guildId: guildId, adapterCreator: voiceChannel.guild.voiceAdapterCreator, }); - const audioPlayer = createAudioPlayer(); + const audioPlayer = playerMap.get(guildId) || createAudioPlayer(); playerMap.set(guildId, audioPlayer); - const filePath = queue.shift(); - currentTrackMap.set(guildId, filePath); + const trackToPlay = queue.shift(); + currentTrackMap.set(guildId, trackToPlay); - if (!fs.existsSync(filePath)) { - console.error('Audio file not found:', filePath); + if (!fs.existsSync(trackToPlay.filePath)) { + console.error('Audio file not found:', trackToPlay.filePath); return false; } - const resource = createAudioResource(filePath); + const resource = createAudioResource(trackToPlay.filePath); audioPlayer.play(resource); connection.subscribe(audioPlayer); audioPlayer.on(AudioPlayerStatus.Idle, () => { currentTrackMap.delete(guildId); + if (queue.length > 0) { playNextInQueue(guildId, voiceChannel); } else { connection.destroy(); } - fs.unlink(filePath, (err) => { - if (err) console.error('Error deleting file:', filePath, err); + fs.unlink(trackToPlay.filePath, (err) => { + if (err) console.error('Error deleting file:', trackToPlay.filePath, err); }); }); @@ -71,24 +81,30 @@ function playNextInQueue(guildId, voiceChannel) { return true; } -function skipTrack(guildId) { +function skipTrack(guildId, voiceChannel) { const player = playerMap.get(guildId); const queue = getQueue(guildId); - const currentTrack = getCurrentTrack(guildId); + const repeat = repeatMap.get(guildId); - if (currentTrack || queue.length > 0) { - if (player) { - player.stop(); - } + if (!player) { + console.error('No player found for this guild.'); + return; + } - if (queue.length > 0) { - currentTrackMap.set(guildId, queue.shift()); - } else { - currentTrackMap.delete(guildId); - } + currentTrackMap.delete(guildId); + + if (repeat) { + repeatMap.set(guildId, false); + } + + player.stop(); + + if (queue.length > 0) { + playNextInQueue(guildId, voiceChannel); } else { - console.error('There are no tracks to skip!'); + player.stop(); } } -module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack }; + +module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat };