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 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);
|
playNextInQueue(guildId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,31 +60,31 @@ function playNextInQueue(guildId) {
|
|||||||
const currentTrack = getCurrentTrack(guildId);
|
const currentTrack = getCurrentTrack(guildId);
|
||||||
const repeat = repeatMap.get(guildId);
|
const repeat = repeatMap.get(guildId);
|
||||||
const voiceChannel = voiceChannelMap.get(guildId);
|
const voiceChannel = voiceChannelMap.get(guildId);
|
||||||
|
const player = playerMap.get(guildId);
|
||||||
|
const isPlaying = player && player.state.status === AudioPlayerStatus.Playing;
|
||||||
|
|
||||||
if (!voiceChannel) {
|
if (!voiceChannel) {
|
||||||
console.error("Voice channel is undefined. Cannot play track.");
|
console.error("Voice channel is undefined. Cannot play track.");
|
||||||
return false;
|
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;
|
return false;
|
||||||
}
|
} else {
|
||||||
|
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
||||||
let trackToPlay = currentTrack;
|
if (connection && connection.state.status !== 'destroyed') {
|
||||||
|
connection.destroy();
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
playTrack(guildId, voiceChannel, trackToPlay);
|
playTrack(guildId, voiceChannel, trackToPlay);
|
||||||
@ -102,15 +104,19 @@ function playTrack(guildId, voiceChannel, track) {
|
|||||||
playerMap.set(guildId, audioPlayer);
|
playerMap.set(guildId, audioPlayer);
|
||||||
|
|
||||||
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
audioPlayer.on(AudioPlayerStatus.Idle, () => {
|
||||||
if (!repeatMap.get(guildId)) {
|
const isRepeatEnabled = repeatMap.get(guildId);
|
||||||
|
const queue = getQueue(guildId);
|
||||||
|
|
||||||
|
if (!isRepeatEnabled) {
|
||||||
currentTrackMap.delete(guildId);
|
currentTrackMap.delete(guildId);
|
||||||
fs.unlink(track.filePath, (err) => {
|
fs.unlink(track.filePath, (err) => {
|
||||||
if (err) console.error('Error deleting file:', track.filePath, err);
|
if (err) console.error('Error deleting file:', track.filePath, err);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const queue = getQueue(guildId);
|
if (queue.length > 0) {
|
||||||
if (queue.length > 0 || repeatMap.get(guildId)) {
|
playNextInQueue(guildId);
|
||||||
|
} else if (isRepeatEnabled) {
|
||||||
playNextInQueue(guildId);
|
playNextInQueue(guildId);
|
||||||
} else {
|
} else {
|
||||||
if (connection && connection.state.status !== 'destroyed') {
|
if (connection && connection.state.status !== 'destroyed') {
|
||||||
@ -164,6 +170,7 @@ function playTrack(guildId, voiceChannel, track) {
|
|||||||
function skipTrack(guildId) {
|
function skipTrack(guildId) {
|
||||||
const player = playerMap.get(guildId);
|
const player = playerMap.get(guildId);
|
||||||
const queue = getQueue(guildId);
|
const queue = getQueue(guildId);
|
||||||
|
const wasRepeatEnabled = repeatMap.get(guildId);
|
||||||
|
|
||||||
if (!player) {
|
if (!player) {
|
||||||
console.error('No player found for this guild.');
|
console.error('No player found for this guild.');
|
||||||
@ -171,10 +178,21 @@ function skipTrack(guildId) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
currentTrackMap.delete(guildId);
|
currentTrackMap.delete(guildId);
|
||||||
|
|
||||||
|
if (wasRepeatEnabled) {
|
||||||
|
repeatMap.set(guildId, false);
|
||||||
|
}
|
||||||
|
|
||||||
player.stop();
|
player.stop();
|
||||||
|
|
||||||
if (queue.length > 0) {
|
if (queue.length > 0) {
|
||||||
playNextInQueue(guildId);
|
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 {
|
} else {
|
||||||
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
const connection = playerMap.get(guildId)?._state?.subscription?.connection;
|
||||||
if (connection && connection.state.status !== 'destroyed') {
|
if (connection && connection.state.status !== 'destroyed') {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user