How to remove event listener from player

code:
async function reproduceSong(interaction, connection, player, channel) {
if (sp > 0) {
try {
await search(queue[0]);
} catch (error) {
//song not found
channel.send("Song " + queue[0] + " not found, skipping.")
removeTopSong();
reproduceSong(interaction, connection, player, channel)
}

const resource = createAudioResource('Path');

player.play(resource);
channel.send("Now playing " + videoLink);

player.addListener("stateChange", (oldOne, newOne) => {
if (newOne.status == "idle") {
console.log("Song finished, moving to next.");
removeTopSong();
reproduceSong(interaction, connection, player, channel);
}
});

} else { //queue ended
interaction.editReply("Queue has ended.");
connection.destroy();
return -1;
}
}
async function reproduceSong(interaction, connection, player, channel) {
if (sp > 0) {
try {
await search(queue[0]);
} catch (error) {
//song not found
channel.send("Song " + queue[0] + " not found, skipping.")
removeTopSong();
reproduceSong(interaction, connection, player, channel)
}

const resource = createAudioResource('Path');

player.play(resource);
channel.send("Now playing " + videoLink);

player.addListener("stateChange", (oldOne, newOne) => {
if (newOne.status == "idle") {
console.log("Song finished, moving to next.");
removeTopSong();
reproduceSong(interaction, connection, player, channel);
}
});

} else { //queue ended
interaction.editReply("Queue has ended.");
connection.destroy();
return -1;
}
}
So I have this recursive function that uses a queue to play songs, but I noticed that when I add a second song to the queue and it switches to that song, the old listener is still active and makes it skip a song since it removes the top song in the queue. Is there a way to eliminate that listener before recalling the function?
2 Replies
d.js toolkit
d.js toolkit12mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Cryingfreeman74
Cryingfreeman74OP12mo ago
nvm I resolved it myself, just needed to create a listener outside the recursive function
Want results from more Discord servers?
Add your server