Fix the queue system

This commit is contained in:
Wizzard 2024-08-17 11:23:30 -04:00
parent 196697dd24
commit 5eb594efa4
3 changed files with 30 additions and 14 deletions

View File

@ -1,4 +1,5 @@
const { getQueue } = require('../utils/queueManager');
const path = require('path');
module.exports = {
name: 'queue',
@ -7,7 +8,7 @@ module.exports = {
if (queue.length === 0) {
return message.channel.send('The queue is empty!');
}
const queueString = queue.map((track, index) => `${index + 1}. ${track.title}`).join('\n');
const queueString = queue.map((track, index) => `${index + 1}. ${path.basename(track)}`).join('\n');
message.channel.send(`Current queue:\n${queueString}`);
}
};

View File

@ -1,9 +1,18 @@
const { skipTrack } = require('../utils/queueManager');
const { skipTrack, getQueue, playNextInQueue } = require('../utils/queueManager');
module.exports = {
name: 'skip',
execute(message) {
skipTrack(message.guild.id);
message.channel.send('Skipped the current track!');
const guildId = message.guild.id;
const queue = getQueue(guildId);
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!');
}
}
};

View File

@ -2,6 +2,7 @@ const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChan
const fs = require('fs');
const queueMap = new Map();
const currentTrackMap = new Map();
function addToQueue(guildId, filePath) {
if (!queueMap.has(guildId)) {
@ -26,7 +27,7 @@ function playNextInQueue(guildId, voiceChannel) {
const audioPlayer = createAudioPlayer();
const filePath = queue.shift();
const filePath = queue[0];
if (!fs.existsSync(filePath)) {
console.error('Audio file not found:', filePath);
@ -37,15 +38,20 @@ 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();
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) => {
@ -53,13 +59,13 @@ function playNextInQueue(guildId, voiceChannel) {
connection.destroy();
});
return true;
return audioPlayer;
}
function skipTrack(guildId) {
function skipTrack(guildId, audioPlayer) {
const queue = getQueue(guildId);
if (queue.length > 0) {
queue.shift();
audioPlayer.stop();
}
}