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');
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) {

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!');
}
skipTrack(guildId);
skipTrack(guildId, voiceChannel);
message.channel.send('Skipped the current track!');
}
};

View File

@ -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 };