Fix skip and queue not working correctly
This commit is contained in:
parent
5eb594efa4
commit
46b937a107
|
@ -1,14 +1,19 @@
|
||||||
const { getQueue } = require('../utils/queueManager');
|
const { getQueue, getCurrentTrack } = require('../utils/queueManager');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'queue',
|
name: 'queue',
|
||||||
execute(message) {
|
execute(message) {
|
||||||
const queue = getQueue(message.guild.id);
|
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!');
|
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}`);
|
message.channel.send(`Current queue:\n${queueString}`);
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -1,18 +1,16 @@
|
||||||
const { skipTrack, getQueue, playNextInQueue } = require('../utils/queueManager');
|
const { skipTrack } = require('../utils/queueManager');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
name: 'skip',
|
name: 'skip',
|
||||||
execute(message) {
|
execute(message) {
|
||||||
const guildId = message.guild.id;
|
const guildId = message.guild.id;
|
||||||
const queue = getQueue(guildId);
|
|
||||||
|
|
||||||
if (queue.length > 0) {
|
|
||||||
const voiceChannel = message.member.voice.channel;
|
const voiceChannel = message.member.voice.channel;
|
||||||
const audioPlayer = playNextInQueue(guildId, voiceChannel); // Get the audio player
|
|
||||||
skipTrack(guildId, audioPlayer); // Skip the current track
|
if (!voiceChannel) {
|
||||||
message.channel.send('Skipped the current track!');
|
return message.reply('You need to be in a voice channel to skip music!');
|
||||||
} else {
|
|
||||||
message.channel.send('There are no tracks to skip!');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
skipTrack(guildId);
|
||||||
|
message.channel.send('Skipped the current track!');
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -2,6 +2,7 @@ const { createAudioPlayer, createAudioResource, AudioPlayerStatus, joinVoiceChan
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
const queueMap = new Map();
|
const queueMap = new Map();
|
||||||
|
const playerMap = new Map();
|
||||||
const currentTrackMap = new Map();
|
const currentTrackMap = new Map();
|
||||||
|
|
||||||
function addToQueue(guildId, filePath) {
|
function addToQueue(guildId, filePath) {
|
||||||
|
@ -15,10 +16,18 @@ function getQueue(guildId) {
|
||||||
return queueMap.get(guildId) || [];
|
return queueMap.get(guildId) || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getCurrentTrack(guildId) {
|
||||||
|
return currentTrackMap.get(guildId) || null;
|
||||||
|
}
|
||||||
|
|
||||||
function playNextInQueue(guildId, voiceChannel) {
|
function playNextInQueue(guildId, voiceChannel) {
|
||||||
const queue = getQueue(guildId);
|
const queue = getQueue(guildId);
|
||||||
|
|
||||||
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,
|
||||||
|
@ -26,8 +35,10 @@ function playNextInQueue(guildId, voiceChannel) {
|
||||||
});
|
});
|
||||||
|
|
||||||
const audioPlayer = createAudioPlayer();
|
const audioPlayer = createAudioPlayer();
|
||||||
|
playerMap.set(guildId, audioPlayer);
|
||||||
|
|
||||||
const filePath = queue[0];
|
const filePath = queue.shift();
|
||||||
|
currentTrackMap.set(guildId, filePath);
|
||||||
|
|
||||||
if (!fs.existsSync(filePath)) {
|
if (!fs.existsSync(filePath)) {
|
||||||
console.error('Audio file not found:', filePath);
|
console.error('Audio file not found:', filePath);
|
||||||
|
@ -38,35 +49,46 @@ function playNextInQueue(guildId, voiceChannel) {
|
||||||
audioPlayer.play(resource);
|
audioPlayer.play(resource);
|
||||||
connection.subscribe(audioPlayer);
|
connection.subscribe(audioPlayer);
|
||||||
|
|
||||||
currentTrackMap.set(guildId, filePath);
|
|
||||||
|
|
||||||
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
||||||
fs.unlink(filePath, (err) => {
|
currentTrackMap.delete(guildId);
|
||||||
if (err) console.error('Error deleting file:', filePath, err);
|
|
||||||
});
|
|
||||||
queue.shift();
|
|
||||||
|
|
||||||
if (queue.length > 0) {
|
if (queue.length > 0) {
|
||||||
playNextInQueue(guildId, voiceChannel);
|
playNextInQueue(guildId, voiceChannel);
|
||||||
} else {
|
} else {
|
||||||
connection.destroy();
|
connection.destroy();
|
||||||
currentTrackMap.delete(guildId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs.unlink(filePath, (err) => {
|
||||||
|
if (err) console.error('Error deleting file:', filePath, err);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
audioPlayer.on('error', (err) => {
|
audioPlayer.on('error', (err) => {
|
||||||
console.error('AudioPlayer error:', err);
|
console.error('AudioPlayer error:', err);
|
||||||
|
currentTrackMap.delete(guildId);
|
||||||
connection.destroy();
|
connection.destroy();
|
||||||
});
|
});
|
||||||
|
|
||||||
return audioPlayer;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function skipTrack(guildId, audioPlayer) {
|
function skipTrack(guildId) {
|
||||||
|
const player = playerMap.get(guildId);
|
||||||
const queue = getQueue(guildId);
|
const queue = getQueue(guildId);
|
||||||
|
const currentTrack = getCurrentTrack(guildId);
|
||||||
|
|
||||||
|
if (currentTrack || queue.length > 0) {
|
||||||
|
if (player) {
|
||||||
|
player.stop();
|
||||||
|
}
|
||||||
|
|
||||||
if (queue.length > 0) {
|
if (queue.length > 0) {
|
||||||
audioPlayer.stop();
|
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 };
|
||||||
|
|
Loading…
Reference in New Issue