Update queueManager.js
This commit is contained in:
parent
7e63d56025
commit
a25d1202b1
|
@ -29,14 +29,30 @@ function toggleRepeat(guildId) {
|
||||||
|
|
||||||
function playNextInQueue(guildId, voiceChannel) {
|
function playNextInQueue(guildId, voiceChannel) {
|
||||||
const queue = getQueue(guildId);
|
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;
|
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({
|
const connection = joinVoiceChannel({
|
||||||
channelId: voiceChannel.id,
|
channelId: voiceChannel.id,
|
||||||
guildId: guildId,
|
guildId: guildId,
|
||||||
|
@ -46,45 +62,45 @@ function playNextInQueue(guildId, voiceChannel) {
|
||||||
const audioPlayer = playerMap.get(guildId) || createAudioPlayer();
|
const audioPlayer = playerMap.get(guildId) || createAudioPlayer();
|
||||||
playerMap.set(guildId, audioPlayer);
|
playerMap.set(guildId, audioPlayer);
|
||||||
|
|
||||||
const trackToPlay = queue.shift();
|
if (!fs.existsSync(track.filePath)) {
|
||||||
currentTrackMap.set(guildId, trackToPlay);
|
console.error('Audio file not found:', track.filePath);
|
||||||
|
|
||||||
if (!fs.existsSync(trackToPlay.filePath)) {
|
|
||||||
console.error('Audio file not found:', trackToPlay.filePath);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const resource = createAudioResource(trackToPlay.filePath);
|
const resource = createAudioResource(track.filePath);
|
||||||
audioPlayer.play(resource);
|
audioPlayer.play(resource);
|
||||||
connection.subscribe(audioPlayer);
|
connection.subscribe(audioPlayer);
|
||||||
|
|
||||||
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
||||||
|
if (!repeatMap.get(guildId)) {
|
||||||
currentTrackMap.delete(guildId);
|
currentTrackMap.delete(guildId);
|
||||||
|
fs.unlink(track.filePath, (err) => {
|
||||||
if (queue.length > 0) {
|
if (err) console.error('Error deleting file:', track.filePath, err);
|
||||||
playNextInQueue(guildId, voiceChannel);
|
});
|
||||||
} else {
|
|
||||||
connection.destroy();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.unlink(trackToPlay.filePath, (err) => {
|
const queue = getQueue(guildId);
|
||||||
if (err) console.error('Error deleting file:', trackToPlay.filePath, err);
|
if (queue.length > 0 || repeatMap.get(guildId)) {
|
||||||
});
|
playNextInQueue(guildId, voiceChannel);
|
||||||
|
} else {
|
||||||
|
if (connection && connection.state.status !== 'destroyed') {
|
||||||
|
connection.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
audioPlayer.on('error', (err) => {
|
audioPlayer.on('error', (err) => {
|
||||||
console.error('AudioPlayer error:', err);
|
console.error('AudioPlayer error:', err);
|
||||||
currentTrackMap.delete(guildId);
|
currentTrackMap.delete(guildId);
|
||||||
|
if (connection && connection.state.status !== 'destroyed') {
|
||||||
connection.destroy();
|
connection.destroy();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function skipTrack(guildId, voiceChannel) {
|
function skipTrack(guildId, voiceChannel) {
|
||||||
const player = playerMap.get(guildId);
|
const player = playerMap.get(guildId);
|
||||||
const queue = getQueue(guildId);
|
const queue = getQueue(guildId);
|
||||||
const repeat = repeatMap.get(guildId);
|
|
||||||
|
|
||||||
if (!player) {
|
if (!player) {
|
||||||
console.error('No player found for this guild.');
|
console.error('No player found for this guild.');
|
||||||
|
@ -92,19 +108,16 @@ function skipTrack(guildId, voiceChannel) {
|
||||||
}
|
}
|
||||||
|
|
||||||
currentTrackMap.delete(guildId);
|
currentTrackMap.delete(guildId);
|
||||||
|
|
||||||
if (repeat) {
|
|
||||||
repeatMap.set(guildId, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
player.stop();
|
player.stop();
|
||||||
|
|
||||||
if (queue.length > 0) {
|
if (queue.length > 0) {
|
||||||
playNextInQueue(guildId, voiceChannel);
|
playNextInQueue(guildId, voiceChannel);
|
||||||
} else {
|
} 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 };
|
module.exports = { addToQueue, getQueue, getCurrentTrack, playNextInQueue, skipTrack, toggleRepeat };
|
||||||
|
|
Loading…
Reference in New Issue