2020-01-12 00:01:18 +05:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "utf-8" >
< meta name = "viewport" content = "width=device-width,initial-scale=1" >
< title > Home - Documentation< / title >
< script src = "scripts/prettify/prettify.js" > < / script >
< script src = "scripts/prettify/lang-css.js" > < / script >
<!-- [if lt IE 9]>
< script src = "//html5shiv.googlecode.com/svn/trunk/html5.js" > < / script >
<![endif]-->
< link type = "text/css" rel = "stylesheet" href = "https://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css" >
< link type = "text/css" rel = "stylesheet" href = "styles/prettify-tomorrow.css" >
< link type = "text/css" rel = "stylesheet" href = "styles/jsdoc-default.css" >
< / head >
< body >
< input type = "checkbox" id = "nav-trigger" class = "nav-trigger" / >
< label for = "nav-trigger" class = "navicon-button x" >
< div class = "navicon" > < / div >
< / label >
< label for = "nav-trigger" class = "overlay" > < / label >
< nav >
2020-01-18 17:04:22 +05:00
< li class = "nav-link nav-home-link" > < a href = "index.html" > Home< / a > < / li > < li class = "nav-heading" > Classes< / li > < li class = "nav-heading" > < span class = "nav-item-type type-class" > C< / span > < span class = "nav-item-name" > < a href = "Player.html" > Player< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#addToQueue" > addToQueue< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#clearQueue" > clearQueue< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#getQueue" > getQueue< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#isPlaying" > isPlaying< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#nowPlaying" > nowPlaying< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#pause" > pause< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#play" > play< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#resume" > resume< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#setQueue" > setQueue< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#setRepeatMode" > setRepeatMode< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#setVolume" > setVolume< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#skip" > skip< / a > < / span > < / li > < li class = "nav-item" > < span class = "nav-item-type type-function" > F< / span > < span class = "nav-item-name" > < a href = "Player.html#stop" > stop< / a > < / span > < / li > < li class = "nav-heading" > < span class = "nav-item-type type-class" > C< / span > < span class = "nav-item-name" > < a href = "Queue.html" > Queue< / a > < / span > < / li > < li class = "nav-heading" > < span class = "nav-item-type type-class" > C< / span > < span class = "nav-item-name" > < a href = "Song.html" > Song< / a > < / span > < / li > < li class = "nav-heading" > Events< / li > < li class = "nav-heading" > < span class = "nav-item-type type-event" > E< / span > < span class = "nav-item-name" > < a href = "Queue.html#event:channelEmpty" > channelEmpty< / a > < / span > < / li > < li class = "nav-heading" > < span class = "nav-item-type type-event" > E< / span > < span class = "nav-item-name" > < a href = "Queue.html#event:end" > end< / a > < / span > < / li > < li class = "nav-heading" > < span class = "nav-item-type type-event" > E< / span > < span class = "nav-item-name" > < a href = "Queue.html#event:songChanged" > songChanged< / a > < / span > < / li > < li class = "nav-heading" > < a href = "global.html" > Globals< / a > < / li >
2020-01-12 00:01:18 +05:00
< / nav >
< div id = "main" >
< section class = "readme" >
2020-01-12 00:10:45 +05:00
< article > < h1 > Discord Player< / h1 >
< p > < a href = "https://npmjs.com/discord-player" > < img src = "https://img.shields.io/npm/dt/discord-player?style=for-the-badge" alt = "downloadsBadge" > < / a >
< a href = "https://npmjs.com/discord-player" > < img src = "https://img.shields.io/npm/v/discord-player?style=for-the-badge" alt = "versionBadge" > < / a >
2020-01-12 00:01:18 +05:00
< a href = "https://patreon.com/Androz2091" > < img src = "https://img.shields.io/endpoint.svg?url=https%3A%2F%2Fshieldsio-patreon.herokuapp.com%2FAndroz2091%2Fpledges&style=for-the-badge" alt = "patreonBadge" > < / a > < / p >
< p > < strong > Note< / strong > : this module uses recent discordjs features and requires discord.js version 12.< / p >
2020-01-12 00:13:09 +05:00
< p > Discord Player is a powerful < a href = "https://nodejs.org" > Node.js< / a > module that allows you to easily implement music commands. < strong > Everything< / strong > is customizable, and everything is done to simplify your work < strong > without limiting you< / strong > !< / p >
2020-01-12 00:01:18 +05:00
< h2 > Installation< / h2 >
2020-01-12 00:10:45 +05:00
< pre class = "prettyprint source lang-sh" > < code > npm install --save discord-player
2020-01-12 00:01:18 +05:00
< / code > < / pre >
2020-01-12 00:10:45 +05:00
< p > Install < strong > opusscript< / strong > or < strong > node-opus< / strong > :< / p >
< pre class = "prettyprint source lang-sh" > < code > npm install --save opusscript
< / code > < / pre >
< p > Install < a href = "https://www.ffmpeg.org/download.html" > FFMPEG< / a > and you're done!< / p >
2020-01-12 00:01:18 +05:00
< h2 > Player< / h2 >
< pre class = "prettyprint source lang-js" > < code > const Discord = require(" discord.js" ),
client = new Discord.Client(),
settings = {
prefix: " !" ,
token: " Your Discord Token"
};
2020-01-18 15:15:26 +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);
< / code > < / pre >
2020-01-12 00:13:09 +05:00
< p > You can pass a third parameter when instantiating the class Player: the < strong > options< / strong > object:< br >
< strong > options.leaveOnEnd< / strong > : whether the bot should leave the voice channel when there is no more song in the queue.< br >
2020-01-18 15:15:26 +05:00
< strong > options.leaveOnStop< / strong > : whether the bot should leave the voice channel when the < code > stop()< / code > function is used.< br >
< strong > options.leaveOnEmpty< / strong > : whether the bot should leave the voice channel if there is no more member in it.< / p >
< h3 > Features Overview< / h3 >
< p > You need to < strong > init the guild queue using the play() function< / strong > , then you are able to manage the queue using the following functions:< / p >
< pre class = "prettyprint source lang-js" > < code > // Play a song in the voice channel and init the guild queue
client.player.play(voiceChannel, songName);
// Add a song to the queue
client.player.addToQueue(guildID, songName);
// Clear the queue
client.player.clearQueue(guildID);
// Get the queue
client.player.getQueue(guildID);
// Skip the current song
client.player.skip(guildID);
// Pause
client.player.pause(guildID);
// Resume
client.player.resume(guildID);
// Stop
client.player.stop(guildID);
// Check if music is playing in a guild
client.player.isPlaying(guildID);
// Get the currently playing song
client.player.nowPlaying(guildID);
// Current song will be repeated indefinitely
2020-02-02 15:20:21 +05:00
client.player.setRepeatMode(guildID, true);
2020-01-18 15:15:26 +05:00
// Current song will no longer be repeated indefinitely
2020-02-02 15:20:21 +05:00
client.player.setRepeatMode(guildID, false);
2020-01-18 15:15:26 +05:00
< / code > < / pre >
< h3 > Event messages< / h3 >
< pre class = "prettyprint source lang-js" > < code > client.player.getQueue(guildID)
.on('end', () => {
message.channel.send('There is no more music in the queue!');
})
.on('songChanged', (oldSong, newSong) => {
message.channel.send(`Now playing ${newSong.name}...`);
})
.on('channelEmpty', () => {
message.channel.send('Stop playing, there is no more member in the voice channel...');
});
< / code > < / pre >
< h2 > Examples< / h2 >
2020-01-12 00:01:18 +05:00
< h3 > Play< / h3 >
2020-01-18 15:15:26 +05:00
< p > To play a song, use the < code > client.player.play()< / code > function.< / p >
2020-01-12 00:01:18 +05:00
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.play(voiceChannel, songName);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// !play Despacito
// will play " Despacito" in the member voice channel
if(command === 'play'){
let song = await client.player.play(message.member.voice.channel, args[0])
message.channel.send(`Currently playing ${song.name}!`);
}
< / code > < / pre >
< h3 > Pause< / h3 >
2020-01-18 15:15:26 +05:00
< p > To pause the current song, use the < code > client.player.pause()< / code > function.< / p >
2020-01-12 00:01:18 +05:00
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.pause(guildID);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'pause'){
let song = await client.player.pause(message.guild.id);
message.channel.send(`${song.name} paused!`);
}
});
< / code > < / pre >
< h3 > Resume< / h3 >
2020-01-18 15:15:26 +05:00
< p > To resume the current song, use the < code > client.player.resume()< / code > function.< / p >
2020-01-12 00:01:18 +05:00
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.resume(guildID);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'resume'){
let song = await client.player.resume(message.guild.id);
message.channel.send(`${song.name} resumed!`);
}
});
< / code > < / pre >
< h3 > Stop< / h3 >
2020-01-18 15:15:26 +05:00
< p > To stop the music, use the < code > client.player.stop()< / code > function.< / p >
2020-01-12 00:01:18 +05:00
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.stop(guildID);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > 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!');
}
});
< / code > < / pre >
< h3 > SetVolume< / h3 >
2020-01-18 15:15:26 +05:00
< p > To update the volume, use the < code > client.player.setVolume()< / code > function.< / p >
2020-01-12 00:01:18 +05:00
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.setVolume(guildID, percent);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > 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]} !`);
}
});
< / code > < / pre >
< h3 > AddToQueue< / h3 >
< p > To add a song to the queue, use the < code > client.player.addToQueue()< / code > function.< / p >
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.addToQueue(guildID, songName);
< / code > < / pre >
< p > < strong > Example:< / strong > < / p >
< p > In this example, you will see how to add a song to the queue if one is already playing.< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'play'){
let aSongIsAlreadyPlaying = client.player.isPlaying(message.guild.id);
// If there's already a song playing
if(aSongIsAlreadyPlaying){
// Add the song to the queue
let song = await client.player.addToQueue(message.guild.id, args[0]);
message.channel.send(`${song.name} added to queue!`);
} else {
// Else, play the song
let song = await client.player.play(message.member.voice.channel, args[0]);
message.channel.send(`Currently playing ${song.name}!`);
}
}
});
< / code > < / pre >
< h3 > ClearQueue< / h3 >
< p > To clear the queue, use the < code > client.player.clearQueue()< / code > function.< / p >
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.clearQueue(guildID);
< / code > < / pre >
< p > < strong > Example:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > 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!');
}
});
< / code > < / pre >
< h3 > GetQueue< / h3 >
< p > To get the server queue, use the < code > client.player.getQueue()< / code > function.< / p >
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.getQueue(guildID);
< / code > < / pre >
< p > < strong > Example:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > 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);
message.channel.send('Server queue:\n'+(queue.songs.map((song, i) => {
return `${i === 0 ? 'Current' : `#${i+1}`} - ${song.name} | ${song.author}`
}).join('\n')));
}
/**
* Output:
*
* Server queue:
* Current - Despacito | Luis Fonsi
* #2 - Memories | Maroon 5
* #3 - Dance Monkey | Tones And I
* #4 - Circles | Post Malone
*/
2020-01-18 15:15:26 +05:00
});
< / code > < / pre >
< h3 > Skip< / h3 >
< p > To skip the current song, use the < code > client.player.skip()< / code > function.< / p >
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.skip(guildID);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', async (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'skip'){
let song = await client.player.skip(message.guild.id);
message.channel.send(`${song.name} skipped!`);
}
});
< / code > < / pre >
< h3 > Now Playing< / h3 >
< p > To get the currently playing song, use the < code > client.player.nowPlaying()< / code > function.< / p >
< p > < strong > Usage:< / strong > < / p >
< pre class = "prettyprint source lang-js" > < code > client.player.nowPlaying(guildID);
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > 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'){
let song = await client.player.nowPlaying(message.guild.id);
message.channel.send(`Currently playing ${song.name}...`);
}
});
< / code > < / pre >
< h3 > Repeat< / h3 >
< p > To repeat the current song, use the < code > client.player.setRepeatMode()< / code > function.< / p >
< p > < strong > Usage:< / strong > < / p >
2020-02-02 15:20:21 +05:00
< pre class = "prettyprint source lang-js" > < code > client.player.setRepeatMode(guildID, boolean);
2020-01-18 15:15:26 +05:00
< / code > < / pre >
< p > < strong > Example< / strong > :< / p >
< pre class = "prettyprint source lang-js" > < code > 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:21 +05:00
client.player.setRepeatMode(message.guild.id, true);
2020-01-18 15:15:26 +05:00
// Get the current song
let song = await client.player.nowPlaying(message.guild.id);
message.channel.send(`${song.name} will be repeated indefinitely!`);
}
if(command === 'disable-repeat'){
// Disable repeat mode
2020-02-02 15:20:21 +05:00
client.player.setRepeatMode(message.guild.id, false);
2020-01-18 15:15:26 +05:00
// Get the current song
let song = await client.player.nowPlaying(message.guild.id);
message.channel.send(`${song.name} will no longer be repeated indefinitely!`);
}
2020-01-12 00:01:18 +05:00
});
< / code > < / pre >
< h2 > Info Messages< / h2 >
< p > You can send a message when the queue ends or when the song changes:< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
if(command === 'play'){
let song = await client.player.play(message.member.voice.channel, args[0]);
song.queue.on('end', () => {
message.channel.send('The queue is empty, please add new songs!');
});
2020-01-18 15:15:26 +05:00
song.queue.on('songChanged', (oldSong, newSong, skipped, repeatMode) => {
if(repeatMode){
message.channel.send(`Playing ${newSong} again...`);
} else {
message.channel.send(`Now playing ${newSong}...`);
}
2020-01-12 00:01:18 +05:00
});
}
< / code > < / pre >
< h2 > Handle errors< / h2 >
< p > There are 2 main errors that you can handle like this:< / p >
< pre class = "prettyprint source lang-js" > < code > client.on('message', (message) => {
const args = message.content.slice(settings.prefix.length).trim().split(/ +/g);
const command = args.shift().toLowerCase();
// Error 1:
// Song not found
if(command === 'play'){
client.player.play(message.member.voice.channel, args[0]).then((song) => {
message.channel.send(`Currently playing ${song.name}!`);
}).catch(() => {
message.channel.send(`No song found for ${args[0]}`);
});
}
// Error 2:
// Not playing
if(command === 'queue'){
let playing = client.player.isPlaying(message.guild.id);
if(!playing) return message.channel.send(':x: No songs currently playing!');
// you are sure it works:
client.player.getQueue(message.guild.id);
}
});
2020-04-20 23:50:15 +05:00
< / code > < / pre >
< h2 > Examples of bots made with discord-player< / h2 >
< p > These bots are made by the community, they can help you build your own!< / p >
< ul >
< li > < a href = "https://github.com/hydraindia/discord-music" > Discord-Music< / a > by < a href = "https://github.com/hydraindia" > hydraindia< / a > < / li >
< li > < a href = "https://github.com/ZerioDev/Music-bot" > Music-bot< / a > by < a href = "https://github.com/ZerioDev" > ZerioDev< / a > < / li >
< / ul > < / article >
2020-01-12 00:01:18 +05:00
< / section >
< / div >
< br class = "clear" >
< footer >
2020-04-20 23:50:15 +05:00
Generated by < a href = "https://github.com/jsdoc3/jsdoc" > JSDoc 3.6.4< / a > using the Minami theme.
2020-01-12 00:01:18 +05:00
< / footer >
< script > prettyPrint ( ) ; < / script >
< script src = "scripts/linenumber.js" > < / script >
< / body >
< / html >