Update queueManager.js

This commit is contained in:
Wizzard 2024-08-17 15:09:26 -04:00
parent 7e63d56025
commit a25d1202b1
1 changed files with 42 additions and 29 deletions

View File

@ -29,14 +29,30 @@ function toggleRepeat(guildId) {
function playNextInQueue(guildId, voiceChannel) {
const queue = getQueue(guildId);
const currentTrack = getCurrentTrack(guildId);
let currentTrack = getCurrentTrack(guildId);
const repeat = repeatMap.get(guildId);
if (currentTrack) {
if (currentTrack && repeat) {
playTrack(guildId, voiceChannel, currentTrack);
return;
}
if (queue.length === 0 && !repeat) {
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
if (connection && connection.state.status !== 'destroyed') {
connection.destroy();
}
return false;
}
if (queue.length === 0) return false;
if (queue.length > 0) {
currentTrack = queue.shift();
currentTrackMap.set(guildId, currentTrack);
playTrack(guildId, voiceChannel, currentTrack);
}
}
function playTrack(guildId, voiceChannel, track) {
const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: guildId,
@ -46,45 +62,45 @@ function playNextInQueue(guildId, voiceChannel) {
const audioPlayer = playerMap.get(guildId) || createAudioPlayer();
playerMap.set(guildId, audioPlayer);
const trackToPlay = queue.shift();
currentTrackMap.set(guildId, trackToPlay);
if (!fs.existsSync(trackToPlay.filePath)) {
console.error('Audio file not found:', trackToPlay.filePath);
if (!fs.existsSync(track.filePath)) {
console.error('Audio file not found:', track.filePath);
return false;
}
const resource = createAudioResource(trackToPlay.filePath);
const resource = createAudioResource(track.filePath);
audioPlayer.play(resource);
connection.subscribe(audioPlayer);
audioPlayer.on(AudioPlayerStatus.Idle, () => {
currentTrackMap.delete(guildId);
if (queue.length > 0) {
playNextInQueue(guildId, voiceChannel);
} else {
connection.destroy();
if (!repeatMap.get(guildId)) {
currentTrackMap.delete(guildId);
fs.unlink(track.filePath, (err) => {
if (err) console.error('Error deleting file:', track.filePath, err);
});
}
fs.unlink(trackToPlay.filePath, (err) => {
if (err) console.error('Error deleting file:', trackToPlay.filePath, err);
});
const queue = getQueue(guildId);
if (queue.length > 0 || repeatMap.get(guildId)) {
playNextInQueue(guildId, voiceChannel);
} else {
if (connection && connection.state.status !== 'destroyed') {
connection.destroy();
}
}
});
audioPlayer.on('error', (err) => {
console.error('AudioPlayer error:', err);
currentTrackMap.delete(guildId);
connection.destroy();
if (connection && connection.state.status !== 'destroyed') {
connection.destroy();
}
});
return true;
}
function skipTrack(guildId, voiceChannel) {
const player = playerMap.get(guildId);
const queue = getQueue(guildId);
const repeat = repeatMap.get(guildId);
if (!player) {
console.error('No player found for this guild.');
@ -92,19 +108,16 @@ function skipTrack(guildId, voiceChannel) {
}
currentTrackMap.delete(guildId);
if (repeat) {
repeatMap.set(guildId, false);
}
player.stop();
if (queue.length > 0) {
playNextInQueue(guildId, voiceChannel);
} else {
player.stop();
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
if (connection && connection.state.status !== 'destroyed') {
connection.destroy();
}
}
}
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat };