184 lines
6.7 KiB
Markdown
184 lines
6.7 KiB
Markdown
# Discord Player Play-dl
|
|
Complete framework to facilitate music commands using **[discord.js](https://discord.js.org)**.
|
|
|
|
[![wakatime](https://wakatime.com/badge/github/JonnyBro/discord-player.svg)](https://wakatime.com/badge/github/Androz2091/discord-player)
|
|
[![CodeFactor](https://www.codefactor.io/repository/github/jonnybro/discord-player/badge/v5)](https://www.codefactor.io/repository/github/androz2091/discord-player/overview/v5)
|
|
|
|
## Installation
|
|
|
|
### Install **[discord-player-play-dl](https://www.npmjs.com/package/discord-player-play-dl)**
|
|
|
|
```sh
|
|
$ npm install discord-player-play-dl
|
|
```
|
|
|
|
### 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 🎸
|
|
- Lightweight ☁️
|
|
- Custom extractors support 🌌
|
|
- Multiple sources support ✌
|
|
- Play in multiple servers at the same time 🚗
|
|
- 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 👑
|
|
|
|
## [Documentation](https://discord-player.js.org)
|
|
|
|
## Getting Started
|
|
|
|
First of all, you will need to register slash commands:
|
|
|
|
```js
|
|
const { REST } = require("@discordjs/rest");
|
|
const { Routes, ApplicationCommandOptionType } = require("discord.js");
|
|
|
|
const commands = [
|
|
{
|
|
name: "play",
|
|
description: "Plays a song!",
|
|
options: [
|
|
{
|
|
name: "query",
|
|
type: ApplicationCommandOptionType.String,
|
|
description: "The song you want to play",
|
|
required: true
|
|
}
|
|
]
|
|
}
|
|
];
|
|
|
|
const rest = new REST({ version: "10" }).setToken("BOT_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:
|
|
|
|
```js
|
|
const { Client } = require("discord.js");
|
|
const client = new Discord.Client({
|
|
intents: [
|
|
"Guilds",
|
|
"GuildVoiceStates"
|
|
]
|
|
});
|
|
const { Player } = require("discord-player");
|
|
|
|
// 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
|
|
player.on("trackStart", (queue, track) => queue.metadata.channel.send(`🎶 | Now playing **${track.title}**!`))
|
|
|
|
client.once("ready", () => {
|
|
console.log("I'm ready !");
|
|
});
|
|
|
|
client.on("interactionCreate", async (interaction) => {
|
|
if (!interaction.isChatInputCommand()) return;
|
|
|
|
// /play track:Despacito
|
|
// will play "Despacito" in the voice channel
|
|
if (interaction.commandName === "play") {
|
|
if (!interaction.member.voice.channelId) return await interaction.reply({ content: "You are not in a voice channel!", ephemeral: true });
|
|
if (interaction.guild.members.me.voice.channelId && interaction.member.voice.channelId !== interaction.guild.members.me.voice.channelId) return await interaction.reply({ content: "You are not in my voice channel!", ephemeral: true });
|
|
const query = interaction.options.getString("query");
|
|
const queue = player.createQueue(interaction.guild, {
|
|
metadata: {
|
|
channel: interaction.channel
|
|
}
|
|
});
|
|
|
|
// verify vc connection
|
|
try {
|
|
if (!queue.connection) await queue.connect(interaction.member.voice.channel);
|
|
} catch {
|
|
queue.destroy();
|
|
return await interaction.reply({ content: "Could not join your voice channel!", ephemeral: true });
|
|
}
|
|
|
|
await interaction.deferReply();
|
|
const track = await player.search(query, {
|
|
requestedBy: interaction.user
|
|
}).then(x => x.tracks[0]);
|
|
if (!track) return await interaction.followUp({ content: `❌ | Track **${query}** not found!` });
|
|
|
|
queue.play(track);
|
|
|
|
return await interaction.followUp({ content: `⏱️ | Loading track **${track.title}**!` });
|
|
}
|
|
});
|
|
|
|
client.login("BOT_TOKEN");
|
|
```
|
|
|
|
## Supported websites
|
|
|
|
By default, discord-player supports **YouTube**, **Spotify** and **SoundCloud** streams only.
|
|
|
|
### Optional dependencies
|
|
|
|
Discord Player Play-dl 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.
|
|
|
|
#### [@discord-player/extractor](https://github.com/DevAndromeda/discord-player-extractors) (optional)
|
|
|
|
Optional package that adds support for `vimeo`, `reverbnation`, `facebook`, `attachment links` and `lyrics`.
|
|
You just need to install it using `npm i --save @discord-player/extractor` (discord-player will automatically detect and use it).
|
|
|
|
#### [@discord-player/downloader](https://github.com/DevAndromeda/discord-player-downloader) (optional)
|
|
|
|
`@discord-player/downloader` is an optional package that brings support for +700 websites. The documentation is available [here](https://github.com/DevAndromeda/discord-player-downloader).
|
|
|
|
## Examples of bots made with Discord Player Play-dl
|
|
|
|
These bots are made by the community, they can help you build your own!
|
|
|
|
* **[JaBa Bot](https://canary.discord.com/api/oauth2/authorize?client_id=708637495054565426&permissions=8&scope=bot%20applications.commands)** by [Jonny_Bro](https://github.com/JonnyBro)
|
|
|
|
## Advanced
|
|
|
|
### Smooth Volume
|
|
|
|
Discord Player Play-dl 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:
|
|
|
|
```js
|
|
// CJS
|
|
require("discord-player/smoothVolume");
|
|
|
|
// ESM
|
|
import "discord-player/smoothVolume"
|
|
```
|
|
|
|
> ⚠️ Make sure that line is situated at the **TOP** of your **main** file.
|
|
|
|
### Custom stream Engine
|
|
|
|
Discord Player Play-dl by default uses **[play-dl](https://github.com/play-dl/play-dl)** for youtube and some other extractors for other sources.
|
|
If you need to modify this behavior without touching extractors, you need to use `onBeforeCreateStream` functionality of Discord player Play-dl.
|
|
|
|
`<Queue>.onBeforeCreateStream` is called before actually downloading the stream. It is a different concept from extractors, where you are **just** downloading
|
|
streams. `source` here will be a video source. Streams from `onBeforeCreateStream` are then piped to `FFmpeg` and finally sent to Discord voice servers.
|