Common Actions on Player/Queue
Learn how to perform common actions on the player and queue
Here are few basic examples on how to implement various actions.
This guide assumes that you already initialized the player in your index.js or corresponding file.
Also this does not perform checks on the current status of the queue. Refer to one of the example bots for more detailed info.
Getting the player instance from anywhere
const { useMainPlayer } = require("discord-player");
...
const player = useMainPlayer();Playing a new track
const { useMainPlayer } = require("discord-player");
...
const player = useMainPlayer();
await player.play(interaction.member.voice.channel, query);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:
const { useMainPlayer } = require("discord-player");
...
const player = useMainPlayer();
const queue = player.nodes.create(...);
const result = await player.search(...);
// acquire task entry
const entry = queue.tasksQueue.acquire();
// wait for previous task to be released and our task to be resolved
await 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();
}Inserting a new track to a specific position in queue
const { useQueue, useMainPlayer } = require("discord-player");
...
const player = useMainPlayer();
const queue = useQueue(interaction.guild.id);
const searchResult = await player.search(query, { requestedBy: interaction.user });
queue.insertTrack(searchResult.tracks[0], position); //Remember queue index starts from 0, not 1Removing a track from the queue
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
queue.removeTrack(trackNumber); //Remember queue index starts from 0, not 1Getting the current queue
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
const tracks = queue.tracks.toArray(); //Converts the queue into a array of tracks
const currentTrack = queue.currentTrack; //Gets the current track being playedPausing the queue
This example shows how you can toggle the pause state for a queue with a single command
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 pausedGoing back a track
If your queue is being looped then there won't be any tracks in history
const { useHistory } = require("discord-player");
...
const history = useHistory(interaction.guild.id);
await history.previous();Skipping a track
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
queue.node.skip()Shuffling the queue
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
queue.tracks.shuffle();Looping the queue
These are the various options for Loop Modes
| Mode | Value | Description |
|---|---|---|
| Off | 0 | Default mode with no loop active |
| Track | 1 | Loops the current track |
| Queue | 2 | Loops the current queue |
| Autoplay | 3 | Play related songs automatically based on your existing queue |
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
queue.setRepeatMode(mode); //Pass the value for the mode hereChanging the volume of the player
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
queue.node.setVolume(volume); //Pass the value for the volume hereStopping the queue
const { useQueue } = require("discord-player");
...
const queue = useQueue(interaction.guild.id);
queue.delete();Controlling the player behavior
You can find the whole list of available nodeOptions from GuildNodeCreateOptions
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,
},
});