discord-player-play-dl/README.md

186 lines
7.8 KiB
Markdown
Raw Normal View History

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
**Note**: this module uses recent discordjs features and requires discord.js version 12 and Node.js 14.
2020-01-12 00:01:18 +05:00
2020-06-21 18:47:37 +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**! It doesn't require any api key, as it uses **scraping**.
2020-01-12 00:01:18 +05:00
## Installation
```sh
2020-11-24 01:16:22 +05:00
npm install --save discord-player
2020-01-12 00:01:18 +05:00
```
2020-06-21 18:47:37 +05:00
Install **@discordjs/opus**:
2020-04-28 18:58:13 +05:00
2020-01-12 00:01:18 +05:00
```sh
2020-06-21 18:47:37 +05:00
npm install --save @discordjs/opus
2020-01-12 00:01:18 +05:00
```
Install [FFMPEG](https://www.ffmpeg.org/download.html) and you're done!
## Features
🤘 Easy to use!
🎸 You can apply some cool filters (bassboost, reverse, 8D, etc...)
2020-07-04 18:24:05 +05:00
🎼 Manage your server queues with simple functions (add songs, skip the current song, pause the music, resume it, etc...)!
🌐 Multi-servers support
## Getting Started
Here is the code you will need to get started with discord-player. Then, you will be able to use `client.player` everywhere in your code!
2020-01-12 00:01:18 +05:00
```js
const Discord = require("discord.js"),
client = new Discord.Client(),
settings = {
prefix: "!",
token: "Your Discord Token"
};
const { Player } = require("discord-player");
2020-06-21 18:47:37 +05:00
// Create a new Player (you don't need any API Key)
const player = new Player(client);
2020-01-12 00:01:18 +05:00
// To easily access the player
client.player = player;
2020-08-27 10:42:29 +05:00
// add the trackStart event so when a song will be played this message will be sent
client.player.on('trackStart', (message, track) => message.channel.send(`Now playing ${track.title}...`))
2020-01-12 00:01:18 +05:00
client.on("ready", () => {
console.log("I'm ready !");
});
client.on("message", async (message) => {
2020-01-12 00:01:18 +05:00
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"){
client.player.play(message, args[0]);
2020-08-27 10:42:29 +05:00
// as we registered the event above, no need to send a success message here
2020-01-12 00:01:18 +05:00
}
});
2020-06-21 18:47:37 +05:00
client.login(settings.token);
2020-06-21 18:47:37 +05:00
```
## [Documentation](https://discord-player.js.org)
2020-06-21 18:47:37 +05:00
You will find many examples in the documentation to understand how the package works!
2020-06-21 18:47:37 +05:00
### Methods overview
2020-06-21 18:47:37 +05:00
You need to **init the guild queue using the play() function**, then you are able to manage the queue and the music using the following functions. Click on a function name to get an example code and explanations.
2020-06-21 18:47:37 +05:00
2020-08-27 10:42:29 +05:00
#### Play a track
2020-12-27 15:50:40 +05:00
* [play(message, query)](https://discord-player.js.org/Player.html#play) - play a track in a server
2020-01-12 00:01:18 +05:00
2020-08-27 10:42:29 +05:00
#### Check if a track is being played
2020-04-28 18:58:13 +05:00
2020-08-27 10:42:29 +05:00
* [isPlaying(message)](https://discord-player.js.org/Player.html#isPlaying) - check if there is a queue for a specific server
2020-01-12 00:01:18 +05:00
2020-08-27 10:42:29 +05:00
#### Manage the queue
2020-01-12 00:01:18 +05:00
2020-08-27 10:42:29 +05:00
* [getQueue(message)](https://discord-player.js.org/Player.html#getQueue) - get the server queue
* [clearQueue(message)](https://discord-player.js.org/Player.html#clearQueue) - clear the server queue
* [remove(message, track)](https://discord-player.js.org/Player.html#remove) - remove a track from the server queue
2021-02-20 20:45:38 +05:00
* [shuffle(message)](https://discord-player.js.org/Player.html#shuffle) - shuffle the server queue
2020-08-27 10:42:29 +05:00
* [nowPlaying(message)](https://discord-player.js.org/Player.html#nowPlaying) - get the current track
2020-01-12 00:01:18 +05:00
#### Manage music stream
2020-01-12 00:01:18 +05:00
2020-08-27 10:42:29 +05:00
* [skip(message)](https://discord-player.js.org/Player.html#skip) - skip the current track
* [back(message)](https://discord-player.js.org/Player.html#back) - play the previous track
2020-08-27 10:42:29 +05:00
* [pause(message)](https://discord-player.js.org/Player.html#pause) - pause the current track
* [resume(message)](https://discord-player.js.org/Player.html#resume) - resume the current track
* [stop(message)](https://discord-player.js.org/Player.html#stop) - stop the current track
* [setFilters(message, newFilters)](https://discord-player.js.org/Player.html#setFilters) - update filters (bassboost for example)
* [setRepeatMode(message, boolean)](https://discord-player.js.org/Player.html#setRepeatMode) - enable or disable repeat mode for the server (play the song again and again)
* [setLoopMode(message, boolean)](https://discord-player.js.org/Player.html#setLoopMode) - enable or disable loop mode for the server (play the queue again and again)
2021-02-20 20:45:38 +05:00
* [seek(message, time)](https://discord-player.js.org/Player.html#seek) - seek to a specific position
* [moveTo(message, channel)](https://discord-player.js.org/Player.html#moveTo) - move the bot to another channel
### Utils
* [createProgressBar(message, options)](https://discord-player.js.org/Player.html#createProgressBar) - generate a progress bar for the current song/queue
2020-01-12 00:01:18 +05:00
### Event messages
2020-01-12 00:01:18 +05:00
```js
// Then add some messages that will be sent when the events will be triggered
2020-08-27 10:42:29 +05:00
client.player
// Send a message when a track starts
.on('trackStart', (message, track) => message.channel.send(`Now playing ${track.title}...`))
// Send a message when something is added to the queue
.on('trackAdd', (message, queue, track) => message.channel.send(`${track.title} has been added to the queue!`))
.on('playlistAdd', (message, queue, playlist) => message.channel.send(`${playlist.title} has been added to the queue (${playlist.tracks.length} songs)!`))
2020-08-27 10:42:29 +05:00
// Send messages to format search results
.on('searchResults', (message, query, tracks) => {
const embed = new Discord.MessageEmbed()
.setAuthor(`Here are your search results for ${query}!`)
.setDescription(tracks.map((t, i) => `${i}. ${t.title}`))
.setFooter('Send the number of the song you want to play!')
message.channel.send(embed);
})
.on('searchInvalidResponse', (message, query, tracks, content, collector) => {
if (content === 'cancel') {
collector.stop()
return message.channel.send('Search cancelled!')
}
message.channel.send(`You must send a valid number between 1 and ${tracks.length}!`)
})
2020-08-27 10:42:29 +05:00
.on('searchCancel', (message, query, tracks) => message.channel.send('You did not provide a valid response... Please send the command again!'))
.on('noResults', (message, query) => message.channel.send(`No results found on YouTube for ${query}!`))
// Send a message when the music is stopped
.on('queueEnd', (message, queue) => message.channel.send('Music stopped as there is no more music in the queue!'))
.on('channelEmpty', (message, queue) => message.channel.send('Music stopped as there is no more member in the voice channel!'))
.on('botDisconnect', (message) => message.channel.send('Music stopped as I have been disconnected from the channel!'))
2020-08-27 10:42:29 +05:00
// Error handling
.on('error', (error, message) => {
2020-08-27 10:42:29 +05:00
switch(error){
case 'NotPlaying':
message.channel.send('There is no music being played on this server!')
break;
case 'NotConnected':
message.channel.send('You are not connected in any voice channel!')
break;
case 'UnableToJoin':
message.channel.send('I am not able to join your voice channel, please check my permissions!')
break;
case 'LiveVideo':
message.channel.send('YouTube lives are not supported!')
break;
2021-02-20 19:59:55 +05:00
case 'VideoUnavailable':
message.channel.send('This YouTube video is not available!');
break;
2020-08-27 10:42:29 +05:00
default:
message.channel.send(`Something went wrong... Error: ${error}`)
}
})
```
## Examples of bots made with discord-player
These bots are made by the community, they can help you build your own!
2020-08-27 10:42:29 +05:00
* [AtlantaBot](https://github.com/Androz2091/AtlantaBot) by [me](https://github.com/Androz2091)
* [Discord-Music](https://github.com/inhydrox/discord-music) by [inhydrox](https://github.com/inhydrox)
* [Music-bot](https://github.com/ZerioDev/Music-bot) by [ZerioDev](https://github.com/ZerioDev)