If you are not using player.play() and handling queue creation as well as other logic on your own, you may need to use AsyncQueue on your command, otherwise you may face this issue:
play.js
const { useMainPlayer } = require("discord-player");...const player = useMainPlayer();const queue = player.nodes.create(...);const result = await player.search(...);// acquire task entryconst entry = queue.tasksQueue.acquire();// wait for previous task to be released and our task to be resolvedawait entry.getTask();// add track(s) (this will add playlist or single track from the result)queue.addTrack(result);try { // if player node was not previously playing, play a song if (!queue.isPlaying()) await queue.node.play();} finally { // release the task we acquired to let other tasks to be executed // make sure you are releasing your entry, otherwise your bot won't // accept new play requests queue.tasksQueue.release();}
const { useQueue } = require("discord-player");...const queue = useQueue(interaction.guild.id);const tracks = queue.tracks.toArray(); //Converts the queue into a array of tracksconst currentTrack = queue.currentTrack; //Gets the current track being played
This example shows how you can toggle the pause state for a queue with a single command
pause.js
const { useQueue } = require("discord-player");...const queue = useQueue(interaction.guild.id);queue.node.setPaused(!queue.node.isPaused());//isPaused() returns true if that player is already paused
Play related songs automatically based on your existing queue
loop.js
const { useQueue } = require("discord-player");...const queue = useQueue(interaction.guild.id);queue.setRepeatMode(mode); //Pass the value for the mode here
const { useQueue } = require("discord-player");...const queue = useQueue(interaction.guild.id);queue.node.setVolume(volume); //Pass the value for the volume here
const player = useMainPlayer();...await player.play(interaction.member.voice.channel, searchResultOrQuery, { nodeOptions: { metadata: interaction.channel, bufferingTimeout: 15000, //How long the player should attempt buffering before giving up leaveOnStop: true, //If player should leave the voice channel after user stops the player leaveOnStopCooldown: 5000, //Cooldown in ms leaveOnEnd: true, //If player should leave after the whole queue is over leaveOnEndCooldown: 15000, //Cooldown in ms leaveOnEmpty: true, //If the player should leave when the voice channel is empty leaveOnEmptyCooldown: 300000, //Cooldown in ms skipOnNoStream: true, },});