Fix track restarting when a new song is added to the queue and repeat is enabled
This commit is contained in:
parent
66c016bfa3
commit
2f6cf886c3
@ -19,8 +19,10 @@ function addToQueue(guildId, filePath, title, voiceChannel, url, requester, avat
|
||||
}
|
||||
|
||||
const audioPlayer = playerMap.get(guildId);
|
||||
const isPlaying = audioPlayer && audioPlayer.state.status === AudioPlayerStatus.Playing;
|
||||
const repeat = repeatMap.get(guildId) || false;
|
||||
|
||||
if (!audioPlayer || audioPlayer.state.status !== AudioPlayerStatus.Playing) {
|
||||
if (!isPlaying) {
|
||||
playNextInQueue(guildId);
|
||||
}
|
||||
}
|
||||
@ -58,31 +60,31 @@ function playNextInQueue(guildId) {
|
||||
const currentTrack = getCurrentTrack(guildId);
|
||||
const repeat = repeatMap.get(guildId);
|
||||
const voiceChannel = voiceChannelMap.get(guildId);
|
||||
const player = playerMap.get(guildId);
|
||||
const isPlaying = player && player.state.status === AudioPlayerStatus.Playing;
|
||||
|
||||
if (!voiceChannel) {
|
||||
console.error("Voice channel is undefined. Cannot play track.");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (currentTrack && !repeat) {
|
||||
let trackToPlay = null;
|
||||
|
||||
if (currentTrack && repeat && isPlaying) {
|
||||
return true;
|
||||
} else if (currentTrack && repeat) {
|
||||
trackToPlay = currentTrack;
|
||||
} else if (queue.length > 0) {
|
||||
trackToPlay = queue.shift();
|
||||
currentTrackMap.set(guildId, trackToPlay);
|
||||
} else if (currentTrack && !repeat) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let trackToPlay = currentTrack;
|
||||
|
||||
if (!trackToPlay) {
|
||||
if (queue.length > 0) {
|
||||
trackToPlay = queue.shift();
|
||||
currentTrackMap.set(guildId, trackToPlay);
|
||||
} else if (repeat && currentTrack) {
|
||||
trackToPlay = currentTrack;
|
||||
} else {
|
||||
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
||||
if (connection && connection.state.status !== 'destroyed') {
|
||||
connection.destroy();
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
||||
if (connection && connection.state.status !== 'destroyed') {
|
||||
connection.destroy();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
playTrack(guildId, voiceChannel, trackToPlay);
|
||||
@ -102,15 +104,19 @@ function playTrack(guildId, voiceChannel, track) {
|
||||
playerMap.set(guildId, audioPlayer);
|
||||
|
||||
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
||||
if (!repeatMap.get(guildId)) {
|
||||
const isRepeatEnabled = repeatMap.get(guildId);
|
||||
const queue = getQueue(guildId);
|
||||
|
||||
if (!isRepeatEnabled) {
|
||||
currentTrackMap.delete(guildId);
|
||||
fs.unlink(track.filePath, (err) => {
|
||||
if (err) console.error('Error deleting file:', track.filePath, err);
|
||||
});
|
||||
}
|
||||
|
||||
const queue = getQueue(guildId);
|
||||
if (queue.length > 0 || repeatMap.get(guildId)) {
|
||||
if (queue.length > 0) {
|
||||
playNextInQueue(guildId);
|
||||
} else if (isRepeatEnabled) {
|
||||
playNextInQueue(guildId);
|
||||
} else {
|
||||
if (connection && connection.state.status !== 'destroyed') {
|
||||
@ -164,6 +170,7 @@ function playTrack(guildId, voiceChannel, track) {
|
||||
function skipTrack(guildId) {
|
||||
const player = playerMap.get(guildId);
|
||||
const queue = getQueue(guildId);
|
||||
const wasRepeatEnabled = repeatMap.get(guildId);
|
||||
|
||||
if (!player) {
|
||||
console.error('No player found for this guild.');
|
||||
@ -171,10 +178,21 @@ function skipTrack(guildId) {
|
||||
}
|
||||
|
||||
currentTrackMap.delete(guildId);
|
||||
|
||||
if (wasRepeatEnabled) {
|
||||
repeatMap.set(guildId, false);
|
||||
}
|
||||
|
||||
player.stop();
|
||||
|
||||
if (queue.length > 0) {
|
||||
playNextInQueue(guildId);
|
||||
} else if (wasRepeatEnabled) {
|
||||
repeatMap.set(guildId, true);
|
||||
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
||||
if (connection && connection.state.status !== 'destroyed') {
|
||||
connection.destroy();
|
||||
}
|
||||
} else {
|
||||
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
||||
if (connection && connection.state.status !== 'destroyed') {
|
||||
|
Loading…
x
Reference in New Issue
Block a user