2020-01-12 00:05:34 +05:00
# Discord Player
2020-01-12 00:01:18 +05:00
2020-01-12 00:05:34 +05:00
[![downloadsBadge ](https://img.shields.io/npm/dt/discord-player?style=for-the-badge )](https://npmjs.com/discord-player)
[![versionBadge ](https://img.shields.io/npm/v/discord-player?style=for-the-badge )](https://npmjs.com/discord-player)
2020-01-12 00:01:18 +05:00
[![patreonBadge ](https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.herokuapp.com%2FAndroz2091%2Fpledges&style=for-the-badge )](https://patreon.com/Androz2091)
**Note**: this module uses recent discordjs features and requires discord.js version 12.
2020-01-12 00:13:09 +05:00
Discord Player is a powerful [Node.js ](https://nodejs.org ) module that allows you to easily implement music commands. **Everything** is customizable, and everything is done to simplify your work **without limiting you** !
2020-01-12 00:01:18 +05:00
## Installation
```sh
2020-01-12 00:05:34 +05:00
npm install --save discord-player
2020-01-12 00:01:18 +05:00
```
2020-04-25 15:44:03 +05:00
Install **opusscript** or ** @discordjs/opus **:
2020-04-28 18:58:13 +05:00
2020-01-12 00:01:18 +05:00
```sh
npm install --save opusscript
```
Install [FFMPEG ](https://www.ffmpeg.org/download.html ) and you're done!
## Player
```js
const Discord = require("discord.js"),
client = new Discord.Client(),
settings = {
prefix: "!",
token: "Your Discord Token"
};
2020-01-12 16:18:27 +05:00
const { Player } = require("discord-player");
2020-01-12 00:10:45 +05:00
// Create a new Player (Youtube API key is your Youtube Data v3 key)
const player = new Player(client, "YOUTUBE API KEY");
2020-01-12 00:01:18 +05:00
// To easily access the player
client.player = player;
client.on("ready", () => {
console.log("I'm ready !");
});
client.login(settings.token);
```
2020-01-12 00:13:09 +05:00
You can pass a third parameter when instantiating the class Player: the **options** object:
2020-06-02 16:20:05 +05:00
**options.leaveOnEnd**: whether the bot should leave the voice channel when there is no more track in the queue.
2020-01-13 00:40:21 +05:00
**options.leaveOnStop**: whether the bot should leave the voice channel when the `stop()` function is used.
2020-01-12 22:56:28 +05:00
**options.leaveOnEmpty**: whether the bot should leave the voice channel if there is no more member in it.
2020-01-12 00:10:45 +05:00
2020-01-12 16:25:25 +05:00
### Features Overview
You need to **init the guild queue using the play() function** , then you are able to manage the queue using the following functions:
```js
2020-06-02 16:20:05 +05:00
// Play a track in the voice channel and init the guild queue
client.player.play(voiceChannel, trackName);
2020-01-18 15:15:10 +05:00
2020-06-02 16:20:05 +05:00
// Add a track to the queue
client.player.addToQueue(guildID, trackName);
2020-01-12 16:25:25 +05:00
// Clear the queue
client.player.clearQueue(guildID);
// Get the queue
client.player.getQueue(guildID);
2020-06-02 16:20:05 +05:00
// Skip the current track
2020-01-12 21:30:05 +05:00
client.player.skip(guildID);
2020-06-02 16:20:05 +05:00
// Remove a track from the queue using the index number
client.player.remove(guildID, track);
2020-01-18 15:15:10 +05:00
2020-01-12 16:25:25 +05:00
// Pause
client.player.pause(guildID);
// Resume
client.player.resume(guildID);
// Stop
client.player.stop(guildID);
2020-01-18 15:15:10 +05:00
2020-01-12 16:25:25 +05:00
// Check if music is playing in a guild
client.player.isPlaying(guildID);
2020-06-02 16:20:05 +05:00
// Get the currently playing trac
2020-01-18 15:15:10 +05:00
client.player.nowPlaying(guildID);
2020-06-02 16:20:05 +05:00
// Current track will be repeated indefinitely
2020-02-02 15:20:31 +05:00
client.player.setRepeatMode(guildID, true);
2020-06-02 16:20:05 +05:00
// Current track will no longer be repeated indefinitely
2020-02-02 15:20:31 +05:00
client.player.setRepeatMode(guildID, false);
2020-01-12 16:25:25 +05:00
```
### Event messages
```js
client.player.getQueue(guildID)
.on('end', () => {
message.channel.send('There is no more music in the queue!');
})
2020-06-02 16:20:05 +05:00
.on('trackChanged', (oldTrack, newTrack) => {
message.channel.send(`Now playing ${newTrack.name}...`);
2020-01-12 22:56:28 +05:00
})
.on('channelEmpty', () => {
message.channel.send('Stop playing, there is no more member in the voice channel...');
2020-01-12 16:25:25 +05:00
});
```
## Examples
2020-01-12 00:01:18 +05:00
### Play
2020-06-02 16:20:05 +05:00
To play a track, use the `client.player.play()` function.
2020-01-12 00:01:18 +05:00
**Usage:**
```js
2020-06-02 16:20:05 +05:00
client.player.play(voiceChannel, trackName, requestedBy);
2020-01-12 00:01:18 +05:00
```
**Example**:
2020-04-28 18:58:13 +05:00
2020-01-12 00:01:18 +05:00
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
2020-04-28 18:58:13 +05:00
2020-01-12 00:01:18 +05:00
// !play Despacito
// will play "Despacito" in the member voice channel
if(command === 'play'){
2020-06-02 16:20:05 +05:00
let track = await client.player.play(message.member.voice.channel, args[0], message.member.user.tag);
message.channel.send(`Currently playing ${track.name}! - Requested by ${track.requestedBy}`);
2020-01-12 00:01:18 +05:00
}
```
### Pause
2020-06-02 16:20:05 +05:00
To pause the current track, use the `client.player.pause()` function.
2020-01-12 00:01:18 +05:00
**Usage:**
```js
client.player.pause(guildID);
```
**Example**:
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'pause'){
2020-06-02 16:20:05 +05:00
let track = await client.player.pause(message.guild.id);
message.channel.send(`${track.name} paused!`);
2020-01-12 00:01:18 +05:00
}
});
```
### Resume
2020-06-02 16:20:05 +05:00
To resume the current track, use the `client.player.resume()` function.
2020-01-12 00:01:18 +05:00
**Usage:**
```js
client.player.resume(guildID);
```
**Example**:
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'resume'){
2020-06-02 16:20:05 +05:00
let track = await client.player.resume(message.guild.id);
message.channel.send(`${track.name} resumed!`);
2020-01-12 00:01:18 +05:00
}
});
```
### Stop
2020-01-12 21:30:05 +05:00
To stop the music, use the `client.player.stop()` function.
2020-01-12 00:01:18 +05:00
**Usage:**
```js
client.player.stop(guildID);
```
**Example**:
```js
client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'stop'){
client.player.stop(message.guild.id);
message.channel.send('Music stopped!');
}
});
```
### SetVolume
2020-01-12 21:30:05 +05:00
To update the volume, use the `client.player.setVolume()` function.
2020-01-12 00:01:18 +05:00
**Usage:**
```js
client.player.setVolume(guildID, percent);
```
**Example**:
```js
client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'setvolume'){
client.player.setVolume(message.guild.id, parseInt(args[0]));
message.channel.send(`Volume set to ${args[0]} !`);
}
});
```
### AddToQueue
2020-06-02 16:20:05 +05:00
To add a track to the queue, use the `client.player.addToQueue()` function.
2020-01-12 00:01:18 +05:00
**Usage:**
```js
2020-06-02 16:20:05 +05:00
client.player.addToQueue(guildID, trackName);
2020-01-12 00:01:18 +05:00
```
**Example:**
2020-06-02 16:20:05 +05:00
In this example, you will see how to add a track to the queue if one is already playing.
2020-01-12 00:01:18 +05:00
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'play'){
2020-06-02 16:20:05 +05:00
let aTrackIsAlreadyPlaying = client.player.isPlaying(message.guild.id);
// If there's already a track playing
if(aTrackIsAlreadyPlaying){
// Add the track to the queue
let track = await client.player.addToQueue(message.guild.id, args[0]);
message.channel.send(`${track.name} added to queue!`);
2020-01-12 00:01:18 +05:00
} else {
2020-06-02 16:20:05 +05:00
// Else, play the track
let track = await client.player.play(message.member.voice.channel, args[0]);
message.channel.send(`Currently playing ${track.name}!`);
2020-01-12 00:01:18 +05:00
}
}
});
```
### ClearQueue
To clear the queue, use the `client.player.clearQueue()` function.
**Usage:**
```js
client.player.clearQueue(guildID);
```
**Example:**
```js
client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'clear-queue'){
client.player.clearQueue(message.guild.id);
message.channel.send('Queue cleared!');
}
});
```
### GetQueue
To get the server queue, use the `client.player.getQueue()` function.
**Usage:**
```js
client.player.getQueue(guildID);
```
**Example:**
```js
client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'queue'){
let queue = await client.player.getQueue(message.guild.id);
2020-06-02 16:20:05 +05:00
message.channel.send('Server queue:\n'+(queue.tracks.map((track, i) => {
return `${i === 0 ? 'Current' : ` #${i+1}`} - ${track.name} | ${track.author}`
2020-01-12 00:01:18 +05:00
}).join('\n')));
}
/**
* Output:
2020-04-28 18:58:13 +05:00
*
2020-01-12 00:01:18 +05:00
* Server queue:
* Current - Despacito | Luis Fonsi
* #2 - Memories | Maroon 5
* #3 - Dance Monkey | Tones And I
* #4 - Circles | Post Malone
*/
});
```
2020-01-12 21:30:05 +05:00
### Skip
2020-06-02 16:20:05 +05:00
To skip the current track, use the `client.player.skip()` function.
2020-01-12 21:30:05 +05:00
**Usage:**
```js
client.player.skip(guildID);
```
**Example**:
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'skip'){
2020-06-02 16:20:05 +05:00
let track = await client.player.skip(message.guild.id);
message.channel.send(`${track.name} skipped!`);
2020-01-18 15:15:10 +05:00
}
});
```
### Now Playing
2020-06-02 16:20:05 +05:00
To get the currently playing track, use the `client.player.nowPlaying()` function.
2020-01-18 15:15:10 +05:00
**Usage:**
```js
client.player.nowPlaying(guildID);
```
**Example**:
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'now-playing'){
2020-06-02 16:20:05 +05:00
let track = await client.player.nowPlaying(message.guild.id);
message.channel.send(`Currently playing ${track.name}...`);
2020-01-12 21:30:05 +05:00
}
});
```
2020-01-18 15:03:22 +05:00
### Repeat
2020-06-02 16:20:05 +05:00
To repeat the current track, use the `client.player.setRepeatMode()` function.
2020-01-18 15:03:22 +05:00
**Usage:**
```js
2020-02-02 15:20:31 +05:00
client.player.setRepeatMode(guildID, boolean);
2020-01-18 15:03:22 +05:00
```
**Example**:
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'enable-repeat'){
// Enable repeat mode
2020-02-02 15:20:31 +05:00
client.player.setRepeatMode(message.guild.id, true);
2020-06-02 16:20:05 +05:00
// Get the current track
let track = await client.player.nowPlaying(message.guild.id);
message.channel.send(`${track.name} will be repeated indefinitely!`);
2020-01-18 15:03:22 +05:00
}
if(command === 'disable-repeat'){
// Disable repeat mode
2020-02-02 15:20:31 +05:00
client.player.setRepeatMode(message.guild.id, false);
2020-06-02 16:20:05 +05:00
// Get the current track
let track = await client.player.nowPlaying(message.guild.id);
message.channel.send(`${track.name} will no longer be repeated indefinitely!`);
2020-01-18 15:03:22 +05:00
}
});
```
2020-04-26 20:07:56 +05:00
### Remove
2020-04-28 18:58:13 +05:00
2020-06-02 16:20:05 +05:00
To remove a track from the queue, use the `client.player.remove()` function.
2020-04-26 20:07:56 +05:00
**Usage:**
```js
2020-06-02 16:20:05 +05:00
client.player.remove(guildID, track);
2020-04-26 20:07:56 +05:00
```
**Example:**
```js
client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'remove'){
2020-06-02 16:20:05 +05:00
// Removes a track from the queue
2020-04-26 20:07:56 +05:00
client.player.remove(message.guild.id, args[0]).then(() => {
2020-06-02 16:20:05 +05:00
message.channel.send('Removed track!');
2020-04-26 20:07:56 +05:00
});
}
});
```
2020-01-12 00:01:18 +05:00
## Info Messages
2020-06-02 16:20:05 +05:00
You can send a message when the queue ends or when the track changes:
2020-04-28 18:58:13 +05:00
2020-01-12 00:01:18 +05:00
```js
client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'play'){
2020-06-02 16:20:05 +05:00
let track = await client.player.play(message.member.voice.channel, args[0]);
track.queue.on('end', () => {
message.channel.send('The queue is empty, please add new tracks!');
2020-01-12 00:01:18 +05:00
});
2020-06-02 16:20:05 +05:00
track.queue.on('trackChanged', (oldTrack, newTrack, skipped, repeatMode) => {
2020-01-18 15:03:22 +05:00
if(repeatMode){
2020-06-02 16:20:05 +05:00
message.channel.send(`Playing ${newTrack} again...`);
2020-01-18 15:03:22 +05:00
} else {
2020-06-02 16:20:05 +05:00
message.channel.send(`Now playing ${newTrack}...`);
2020-01-18 15:03:22 +05:00
}
2020-01-12 00:01:18 +05:00
});
}
```
## Handle errors
There are 2 main errors that you can handle like this:
```js
client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Error 1:
2020-06-02 16:20:05 +05:00
// Track not found
2020-01-12 00:01:18 +05:00
if(command === 'play'){
2020-06-02 16:20:05 +05:00
client.player.play(message.member.voice.channel, args[0]).then((track) => {
message.channel.send(`Currently playing ${track.name}!`);
2020-01-12 00:01:18 +05:00
}).catch(() => {
2020-06-02 16:20:05 +05:00
message.channel.send(`No track found for ${args[0]}`);
2020-01-12 00:01:18 +05:00
});
}
// Error 2:
// Not playing
if(command === 'queue'){
let playing = client.player.isPlaying(message.guild.id);
2020-06-02 16:20:05 +05:00
if(!playing) return message.channel.send(':x: No tracks currently playing!');
2020-01-12 00:01:18 +05:00
// you are sure it works:
client.player.getQueue(message.guild.id);
}
});
2020-04-08 13:23:08 +05:00
```
## Examples of bots made with discord-player
These bots are made by the community, they can help you build your own!
* [Discord-Music ](https://github.com/hydraindia/discord-music ) by [hydraindia ](https://github.com/hydraindia )
* [Music-bot ](https://github.com/ZerioDev/Music-bot ) by [ZerioDev ](https://github.com/ZerioDev )