Change a lot repeat command

This commit is contained in:
Wizzard 2024-08-17 14:58:17 -04:00
parent d7bdc851fe
commit 7e63d56025
4 changed files with 56 additions and 29 deletions

View File

@ -12,13 +12,13 @@ module.exports = {
.setTitle('Current Queue'); .setTitle('Current Queue');
if (currentTrack) { 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) { if (queue.length > 0) {
embed.addFields({ embed.addFields({
name: 'Up next', 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 inline: false
}); });
} else if (!currentTrack) { } else if (!currentTrack) {

11
commands/repeat.js Normal file
View File

@ -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'}.`);
},
};

View File

@ -10,7 +10,7 @@ module.exports = {
return message.reply('You need to be in a voice channel to skip music!'); 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!'); message.channel.send('Skipped the current track!');
} }
}; };

View File

@ -4,12 +4,13 @@ const fs = require('fs');
const queueMap = new Map(); const queueMap = new Map();
const playerMap = new Map(); const playerMap = new Map();
const currentTrackMap = new Map(); const currentTrackMap = new Map();
const repeatMap = new Map();
function addToQueue(guildId, filePath) { function addToQueue(guildId, filePath, title) {
if (!queueMap.has(guildId)) { if (!queueMap.has(guildId)) {
queueMap.set(guildId, []); queueMap.set(guildId, []);
} }
queueMap.get(guildId).push(filePath); queueMap.get(guildId).push({ filePath, title });
} }
function getQueue(guildId) { function getQueue(guildId) {
@ -20,45 +21,54 @@ function getCurrentTrack(guildId) {
return currentTrackMap.get(guildId) || null; return currentTrackMap.get(guildId) || null;
} }
function toggleRepeat(guildId) {
const currentRepeat = repeatMap.get(guildId) || false;
repeatMap.set(guildId, !currentRepeat);
return !currentRepeat;
}
function playNextInQueue(guildId, voiceChannel) { function playNextInQueue(guildId, voiceChannel) {
const queue = getQueue(guildId); const queue = getQueue(guildId);
const currentTrack = getCurrentTrack(guildId);
if (currentTrack) {
return false;
}
if (queue.length === 0) return false; if (queue.length === 0) return false;
const currentTrack = getCurrentTrack(guildId);
if (currentTrack) return false;
const connection = joinVoiceChannel({ const connection = joinVoiceChannel({
channelId: voiceChannel.id, channelId: voiceChannel.id,
guildId: guildId, guildId: guildId,
adapterCreator: voiceChannel.guild.voiceAdapterCreator, adapterCreator: voiceChannel.guild.voiceAdapterCreator,
}); });
const audioPlayer = createAudioPlayer(); const audioPlayer = playerMap.get(guildId) || createAudioPlayer();
playerMap.set(guildId, audioPlayer); playerMap.set(guildId, audioPlayer);
const filePath = queue.shift(); const trackToPlay = queue.shift();
currentTrackMap.set(guildId, filePath); currentTrackMap.set(guildId, trackToPlay);
if (!fs.existsSync(filePath)) { if (!fs.existsSync(trackToPlay.filePath)) {
console.error('Audio file not found:', filePath); console.error('Audio file not found:', trackToPlay.filePath);
return false; return false;
} }
const resource = createAudioResource(filePath); const resource = createAudioResource(trackToPlay.filePath);
audioPlayer.play(resource); audioPlayer.play(resource);
connection.subscribe(audioPlayer); connection.subscribe(audioPlayer);
audioPlayer.on(AudioPlayerStatus.Idle, () => { audioPlayer.on(AudioPlayerStatus.Idle, () => {
currentTrackMap.delete(guildId); currentTrackMap.delete(guildId);
if (queue.length > 0) { if (queue.length > 0) {
playNextInQueue(guildId, voiceChannel); playNextInQueue(guildId, voiceChannel);
} else { } else {
connection.destroy(); connection.destroy();
} }
fs.unlink(filePath, (err) => { fs.unlink(trackToPlay.filePath, (err) => {
if (err) console.error('Error deleting file:', filePath, err); if (err) console.error('Error deleting file:', trackToPlay.filePath, err);
}); });
}); });
@ -71,24 +81,30 @@ function playNextInQueue(guildId, voiceChannel) {
return true; return true;
} }
function skipTrack(guildId) { function skipTrack(guildId, voiceChannel) {
const player = playerMap.get(guildId); const player = playerMap.get(guildId);
const queue = getQueue(guildId); const queue = getQueue(guildId);
const currentTrack = getCurrentTrack(guildId); const repeat = repeatMap.get(guildId);
if (currentTrack || queue.length > 0) { if (!player) {
if (player) { console.error('No player found for this guild.');
player.stop(); return;
} }
if (queue.length > 0) { currentTrackMap.delete(guildId);
currentTrackMap.set(guildId, queue.shift());
} else { if (repeat) {
currentTrackMap.delete(guildId); repeatMap.set(guildId, false);
} }
player.stop();
if (queue.length > 0) {
playNextInQueue(guildId, voiceChannel);
} else { } 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 };