Discord Player
Discord Player is a powerful framework for JavaScript and TypeScript, built on top of @discord.js/voice library.
It provides easy set of customizable tools to develop Discord Music bots.
![discordBadge](https://img.shields.io/discord/558328638911545423?style=for-the-badge&color=7289da)
Why Discord Player?
- Beginner friendly, easy to understand
- TypeScript support
- Supports audio player sharing
- Quick and easy to set up
- Wide range of player management features
- 64+ built-in audio filter presets
- Highly customizable
- Automatic queue management
- Query caching support
- Wide range of extendable sources via Extractors API
- Object oriented
- Built in stats tracker
Installation
Before you start
Discord Player requires Discord.js 14.0 or higher. PLease make sure you have a compatible version using npm list discord.js
in your terminal. If you're using an earlier version please update it. The Discord.JS Guide has resources to help with that.
Main Library
$ yarn add discord-player
$ yarn add @discord-player/extractor
Discord Player recognizes @discord-player/extractor
and loads it automatically by default.
Opus Library
Discord Player is a high level framework for Discord VoIP. Discord only accepts opus packets, thus you need to install opus library. You can install any of these:
$ yarn add @discordjs/opus
$ yarn add opusscript
FFmpeg or Avconv
FFmpeg or Avconv is required for media transcoding. You can get it from https://ffmpeg.org or by installing it from npm (ffmpeg-static or other binaries are not recommended):
$ yarn add ffmpeg-static
$ yarn add @ffmpeg-installer/ffmpeg
$ yarn add @node-ffmpeg/node-ffmpeg-installer
$ yarn add ffmpeg-binaries
Use FFMPEG_PATH
environment variable to load ffmpeg from custom path.
Streaming Library
You also need to install streaming library if you want to add support for youtube playback. You can install one of these libraries:
$ yarn add ytdl-core
$ yarn add play-dl
$ yarn add @distube/ytdl-core
Done with all these? Let's write a simple music bot then.
Setup
Let's create a master player instance.
const { Player } = require('discord-player');
const { SpotifyExtractor, SoundCloudExtractor } = require('@discord-player/extractor');
const client = new Discord.Client({
intents: ['GuildVoiceStates' ]
});
const player = new Player(client);
await player.extractors.loadDefault();
await player.extractors.register(SpotifyExtractor, {});
await player.extractors.register(SoundCloudExtractor, {});
Did You Know? Discord Player is by default a singleton.
Now, let's add some event listeners:
player.events.on('playerStart', (queue, track) => {
queue.metadata.channel.send(`Started playing **${track.title}**!`);
});
Let's write the command part. You can define the command as you desire. We will only check the command handler part:
async function execute(interaction) {
const channel = interaction.message.member.voice.channel;
if (!channel) return interaction.reply('You are not connected to a voice channel!');
const query = interaction.options.getString('query', true);
await interaction.deferReply();
try {
const { track } = await player.play(channel, query, {
nodeOptions: {
metadata: interaction
}
});
return interaction.followUp(`**${track.title}** enqueued!`);
} catch (e) {
return interaction.followUp(`Something went wrong: ${e}`);
}
}
That's all it takes to build your own music bot.
Check out the Documentation for more info.
A curated list of resources (such as open source music bots, extractors, etc.) built by Discord Player community.
https://discord-player.js.org/docs/guides/community-resources