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;
  }
}

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?
Was this page helpful?