2021-04-04 21:34:41 +05:00
# Discord Player
Complete framework to facilitate music commands using ** [discord.js ](https://discord.js.org )**.
[![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)
2021-04-25 14:23:45 +05:00
[![discordBadge ](https://img.shields.io/discord/558328638911545423?style=for-the-badge&color=7289da )](https://androz2091.fr/discord)
2021-06-20 14:04:34 +05:00
[![wakatime ](https://wakatime.com/badge/github/Androz2091/discord-player.svg )](https://wakatime.com/badge/github/Androz2091/discord-player)
2021-06-24 14:17:45 +05:00
[![CodeFactor ](https://www.codefactor.io/repository/github/androz2091/discord-player/badge/v5 )](https://www.codefactor.io/repository/github/androz2091/discord-player/overview/v5)
2021-05-01 12:50:14 +05:00
2021-04-04 21:34:41 +05:00
## Installation
### Install **[discord-player](https://npmjs.com/package/discord-player)**
```sh
$ npm install --save discord-player
```
### Install **[@discordjs/opus](https://npmjs.com/package/@discordjs/opus)**
```sh
$ npm install --save @discordjs/opus
```
### Install FFmpeg or Avconv
- Official FFMPEG Website: ** [https://www.ffmpeg.org/download.html ](https://www.ffmpeg.org/download.html )**
- Node Module (FFMPEG): ** [https://npmjs.com/package/ffmpeg-static ](https://npmjs.com/package/ffmpeg-static )**
- Avconv: ** [https://libav.org/download ](https://libav.org/download )**
# Features
- Simple & easy to use 🤘
- Beginner friendly 😱
- Audio filters 🎸
2021-11-28 13:41:48 +05:00
- Lightweight ☁️
2021-04-10 11:14:50 +05:00
- Custom extractors support 🌌
2021-04-10 22:52:48 +05:00
- Multiple sources support ✌
- Play in multiple servers at the same time 🚗
2021-11-28 13:41:48 +05:00
- Does not inject anything to discord.js or your discord.js client 💉
- Allows you to have full control over what is going to be streamed 👑
2021-04-04 21:34:41 +05:00
2021-04-18 22:20:12 +05:00
## [Documentation](https://discord-player.js.org)
## Getting Started
2021-08-07 12:02:46 +05:00
First of all, you will need to register slash commands:
2021-04-18 22:20:12 +05:00
```js
2021-08-07 12:02:46 +05:00
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const commands = [{
name: "play",
description: "Plays a song!",
options: [
{
name: "query",
type: "STRING",
description: "The song you want to play",
required: true
}
]
}];
const rest = new REST({ version: "9" }).setToken(process.env.DISCORD_TOKEN);
(async () => {
try {
console.log("Started refreshing application [/] commands.");
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.log("Successfully reloaded application [/] commands.");
} catch (error) {
console.error(error);
}
})();
```
Now you can implement your bot's logic:
2021-04-18 22:20:12 +05:00
2021-08-07 12:02:46 +05:00
```js
const { Client, Intents } = require("discord.js");
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES] });
2021-08-06 13:35:19 +05:00
const { Player } = require("discord-player");
2021-04-18 22:20:12 +05:00
// Create a new Player (you don't need any API Key)
const player = new Player(client);
// add the trackStart event so when a song will be played this message will be sent
2021-08-07 20:03:34 +05:00
player.on("trackStart", (queue, track) => queue.metadata.channel.send(`🎶 | Now playing ** ${track.title}**!`))
2021-04-18 22:20:12 +05:00
client.once("ready", () => {
console.log("I'm ready !");
});
2021-08-07 12:02:46 +05:00
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
2021-04-18 22:20:12 +05:00
2021-10-27 15:07:05 +05:00
// /play track:Despacito
2021-04-18 22:20:12 +05:00
// will play "Despacito" in the voice channel
2021-08-07 12:02:46 +05:00
if (interaction.commandName === "play") {
2021-08-16 23:53:48 +05:00
if (!interaction.member.voice.channelId) return await interaction.reply({ content: "You are not in a voice channel!", ephemeral: true });
if (interaction.guild.me.voice.channelId & & interaction.member.voice.channelId !== interaction.guild.me.voice.channelId) return await interaction.reply({ content: "You are not in my voice channel!", ephemeral: true });
2021-08-07 12:02:46 +05:00
const query = interaction.options.get("query").value;
2021-08-15 01:43:48 +05:00
const queue = player.createQueue(interaction.guild, {
2021-08-07 20:03:34 +05:00
metadata: {
channel: interaction.channel
}
2021-06-18 00:09:02 +05:00
});
// verify vc connection
try {
2021-08-07 12:02:46 +05:00
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
2021-06-18 00:09:02 +05:00
} catch {
queue.destroy();
2021-08-16 23:53:48 +05:00
return await interaction.reply({ content: "Could not join your voice channel!", ephemeral: true });
2021-06-18 00:09:02 +05:00
}
2021-08-15 01:53:35 +05:00
await interaction.deferReply();
2021-08-07 12:02:46 +05:00
const track = await player.search(query, {
2021-08-16 21:18:10 +05:00
requestedBy: interaction.user
2021-08-15 18:24:28 +05:00
}).then(x => x.tracks[0]);
2021-08-07 12:02:46 +05:00
if (!track) return await interaction.followUp({ content: `❌ | Track **${query}** not found!` });
2021-06-18 00:09:02 +05:00
queue.play(track);
2021-04-18 22:20:12 +05:00
2021-08-07 12:02:46 +05:00
return await interaction.followUp({ content: `⏱️ | Loading track **${track.title}**!` });
}
2021-04-18 22:20:12 +05:00
});
2021-08-07 12:02:46 +05:00
client.login(process.env.DISCORD_TOKEN);
2021-04-18 22:20:12 +05:00
```
2021-04-19 18:59:17 +05:00
## Supported websites
By default, discord-player supports **YouTube** , **Spotify** and **SoundCloud** streams only.
2021-04-19 19:01:36 +05:00
### Optional dependencies
2021-04-19 18:59:17 +05:00
2021-04-19 19:01:36 +05:00
Discord Player provides an **Extractor API** that enables you to use your custom stream extractor with it. Some packages have been made by the community to add new features using this API.
2021-04-19 18:59:17 +05:00
2021-04-19 19:01:36 +05:00
#### [@discord-player/extractor](https://github.com/Snowflake107/discord-player-extractors) (optional)
2021-04-19 18:59:17 +05:00
2021-04-19 19:01:36 +05:00
Optional package that adds support for `vimeo` , `reverbnation` , `facebook` , `attachment links` and `lyrics` .
2021-04-19 19:46:58 +05:00
You just need to install it using `npm i --save @discord-player/extractor` (discord-player will automatically detect and use it).
2021-04-19 18:59:17 +05:00
2021-04-19 19:01:36 +05:00
#### [@discord-player/downloader](https://github.com/DevSnowflake/discord-player-downloader) (optional)
2021-04-19 18:59:17 +05:00
`@discord-player/downloader` is an optional package that brings support for +700 websites. The documentation is available [here ](https://github.com/DevSnowflake/discord-player-downloader ).
## Examples of bots made with Discord Player
These bots are made by the community, they can help you build your own!
2021-08-20 22:08:26 +05:00
* **[Discord Music Bot](https://github.com/Androz2091/discord-music-bot)** by [Androz2091 ](https://github.com/Androz2091 )
2021-11-07 15:52:14 +05:00
* [Dodong ](https://github.com/nizeic/Dodong ) by [nizeic ](https://github.com/nizeic )
2021-11-05 11:56:23 +05:00
* [Musico ](https://github.com/Whirl21/Musico ) by [Whirl21 ](https://github.com/Whirl21 )
2021-11-17 15:08:07 +05:00
* [Eyesense-Music-Bot ](https://github.com/naseif/Eyesense-Music-Bot ) by [naseif ](https://github.com/naseif )
2021-11-05 11:56:23 +05:00
* [Music-bot ](https://github.com/ZerioDev/Music-bot ) by [ZerioDev ](https://github.com/ZerioDev )
2021-08-20 22:07:23 +05:00
* [AtlantaBot ](https://github.com/Androz2091/AtlantaBot ) by [Androz2091 ](https://github.com/Androz2091 ) (**outdated**)
* [Discord-Music ](https://github.com/inhydrox/discord-music ) by [inhydrox ](https://github.com/inhydrox ) (**outdated**)
2021-04-19 18:59:17 +05:00
2021-04-25 14:18:56 +05:00
## Advanced
2021-04-19 18:59:17 +05:00
2022-01-26 00:29:20 +05:00
### Smooth Volume
2022-01-31 16:45:29 +05:00
Discord Player will by default try to implement this. If smooth volume does not work, you need to add this line at the top of your main file:
2022-01-26 00:29:20 +05:00
```js
// CJS
require("discord-player/smoothVolume");
// ESM
import "discord-player/smoothVolume"
```
> ⚠️ Make sure that line is situated at the **TOP** of your **main** file.
2021-04-25 14:18:56 +05:00
### Use cookies
2021-04-18 22:20:12 +05:00
```js
const player = new Player(client, {
2021-06-19 17:13:39 +05:00
ytdlOptions: {
2021-04-18 22:20:12 +05:00
requestOptions: {
headers: {
cookie: "YOUR_YOUTUBE_COOKIE"
}
}
}
});
```
2021-04-25 14:18:56 +05:00
### Use custom proxies
2021-04-18 22:20:12 +05:00
```js
const HttpsProxyAgent = require("https-proxy-agent");
// Remove "user:pass@" if you don't need to authenticate to your proxy.
const proxy = "http://user:pass@111.111.111.111:8080";
const agent = HttpsProxyAgent(proxy);
const player = new Player(client, {
2021-06-19 17:13:39 +05:00
ytdlOptions: {
2021-04-18 22:20:12 +05:00
requestOptions: { agent }
}
});
```
2021-09-07 09:48:56 +05:00
2021-11-28 12:57:25 +05:00
> You may also create a simple proxy server and forward requests through it.
> See **[https://github.com/http-party/node-http-proxy](https://github.com/http-party/node-http-proxy)** for more info.
2021-09-07 09:48:56 +05:00
### Custom stream Engine
Discord Player by default uses ** [node-ytdl-core ](https://github.com/fent/node-ytdl-core )** for youtube and some other extractors for other sources.
If you need to modify this behavior without touching extractors, you need to use `createStream` functionality of discord player.
Here's an example on how you can use ** [play-dl ](https://npmjs.com/package/play-dl )** to download youtube streams instead of using ytdl-core.
```js
const playdl = require("play-dl");
// other code
2021-09-25 20:35:19 +05:00
const queue = player.createQueue(..., {
...,
async onBeforeCreateStream(track, source, _queue) {
2021-09-07 09:48:56 +05:00
// only trap youtube source
if (source === "youtube") {
// track here would be youtube track
2022-01-26 00:12:52 +05:00
return (await playdl.stream(track.url, { discordPlayerCompatibility : true })).stream;
2021-09-07 09:48:56 +05:00
// we must return readable stream or void (returning void means telling discord-player to look for default extractor)
}
2021-09-25 20:35:19 +05:00
}
});
2021-09-07 09:48:56 +05:00
```
2021-09-25 20:35:19 +05:00
`<Queue>.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
2021-11-05 11:56:23 +05:00
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.