Fix skip and queue not working correctly

This commit is contained in:
Wizzard 2024-08-17 11:46:19 -04:00
parent 5eb594efa4
commit 46b937a107
3 changed files with 51 additions and 26 deletions

View File

@ -1,14 +1,19 @@
const { getQueue } = require('../utils/queueManager');
const { getQueue, getCurrentTrack } = require('../utils/queueManager');
const path = require('path');
module.exports = {
name: 'queue',
execute(message) {
const queue = getQueue(message.guild.id);
if (queue.length === 0) {
const currentTrack = getCurrentTrack(message.guild.id);
if (!currentTrack && queue.length === 0) {
return message.channel.send('The queue is empty!');
}
const queueString = queue.map((track, index) => `${index + 1}. ${path.basename(track)}`).join('\n');
let queueString = currentTrack ? `Currently playing: ${path.basename(currentTrack)}\n\n` : '';
queueString += queue.map((track, index) => `${index + 1}. ${path.basename(track)}`).join('\n');
message.channel.send(`Current queue:\n${queueString}`);
}
};

View File

@ -1,18 +1,16 @@
const { skipTrack, getQueue, playNextInQueue } = require('../utils/queueManager');
const { skipTrack } = require('../utils/queueManager');
module.exports = {
name: 'skip',
execute(message) {
const guildId = message.guild.id;
const queue = getQueue(guildId);
const voiceChannel = message.member.voice.channel;
if (queue.length > 0) {
const voiceChannel = message.member.voice.channel;
const audioPlayer = playNextInQueue(guildId, voiceChannel); // Get the audio player
skipTrack(guildId, audioPlayer); // Skip the current track
message.channel.send('Skipped the current track!');
} else {
message.channel.send('There are no tracks to skip!');
if (!voiceChannel) {
return message.reply('You need to be in a voice channel to skip music!');
}
skipTrack(guildId);
message.channel.send('Skipped the current track!');
}
};

View File

@ -2,6 +2,7 @@ const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChan
const fs = require('fs');
const queueMap = new Map();
const playerMap = new Map();
const currentTrackMap = new Map();
function addToQueue(guildId, filePath) {
@ -15,10 +16,18 @@ function getQueue(guildId) {
return queueMap.get(guildId) || [];
}
function getCurrentTrack(guildId) {
return currentTrackMap.get(guildId) || null;
}
function playNextInQueue(guildId, voiceChannel) {
const queue = getQueue(guildId);
if (queue.length === 0) return false;
const currentTrack = getCurrentTrack(guildId);
if (currentTrack) return false;
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: guildId,
@ -26,8 +35,10 @@ function playNextInQueue(guildId, voiceChannel) {
});
const audioPlayer = createAudioPlayer();
playerMap.set(guildId, audioPlayer);
const filePath = queue[0];
const filePath = queue.shift();
currentTrackMap.set(guildId, filePath);
if (!fs.existsSync(filePath)) {
console.error('Audio file not found:', filePath);
@ -38,35 +49,46 @@ function playNextInQueue(guildId, voiceChannel) {
audioPlayer.play(resource);
connection.subscribe(audioPlayer);
currentTrackMap.set(guildId, filePath);
audioPlayer.on(AudioPlayerStatus.Idle, () => {
fs.unlink(filePath, (err) => {
if (err) console.error('Error deleting file:', filePath, err);
});
queue.shift();
currentTrackMap.delete(guildId);
if (queue.length > 0) {
playNextInQueue(guildId, voiceChannel);
} else {
connection.destroy();
currentTrackMap.delete(guildId);
}
fs.unlink(filePath, (err) => {
if (err) console.error('Error deleting file:', filePath, err);
});
});
audioPlayer.on('error', (err) => {
console.error('AudioPlayer error:', err);
currentTrackMap.delete(guildId);
connection.destroy();
});
return audioPlayer;
return true;
}
function skipTrack(guildId, audioPlayer) {
function skipTrack(guildId) {
const player = playerMap.get(guildId);
const queue = getQueue(guildId);
if (queue.length > 0) {
audioPlayer.stop();
const currentTrack = getCurrentTrack(guildId);
if (currentTrack || queue.length > 0) {
if (player) {
player.stop();
}
if (queue.length > 0) {
currentTrackMap.set(guildId, queue.shift());
} else {
currentTrackMap.delete(guildId);
}
} else {
console.error('There are no tracks to skip!');
}
}
module.exports = { addToQueue, getQueue, playNextInQueue, skipTrack };
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack };