Discord Player
Discord Player is a powerful framework for JavaScript and TypeScript, built on top of @discord.js/voice (opens in a new tab) library. It provides easy set of customizable tools to develop Discord Music bots.
(opens in a new tab) (opens in a new tab) (opens in a new tab)
Why Discord Player?
- Beginner friendly, easy to understand
- TypeScript support
- 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 (opens in a new tab) has resources to help with that.
Main Library
$ npm install discord-player # main library
$ npm install @discord-player/extractor # extractors provider
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:
$ npm install @discordjs/opus
$ npm install opusscript
FFmpeg or Avconv
FFmpeg or Avconv is required for media transcoding. You can get it from https://ffmpeg.org (opens in a new tab) or by installing it from npm:
$ npm install ffmpeg-static
You can get avconv from https://libav.org/download (opens in a new tab).
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:
$ npm install ytdl-core
$ npm install play-dl
$ npm install @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 client = new Discord.Client({
// Make sure you have 'GuildVoiceStates' intent enabled
intents: ['GuildVoiceStates' /* Other intents */]
});
// this is the entrypoint for discord-player based application
const player = new Player(client);
Did You Know? Discord Player is by default a singleton.
Now, let's add some event listeners:
// this event is emitted whenever discord-player starts to play a track
player.events.on('playerStart', (queue, track) => {
// we will later define queue.metadata object while creating the queue
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!'); // make sure we have a voice channel
const query = interaction.options.getString('query', true); // we need input/query to play
// let's defer the interaction as things can take time to process
await interaction.deferReply();
try {
const { track } = await player.play(channel, query, {
nodeOptions: {
// nodeOptions are the options for guild node (aka your queue in simple word)
metadata: interaction // we can access this metadata object using queue.metadata later on
}
});
return interaction.followUp(`**${track.title}** enqueued!`);
} catch (e) {
// let's return error if something failed
return interaction.followUp(`Something went wrong: ${e}`);
}
}
That's all it takes to build your own music bot.
Check out the Documentation (opens in a new tab) for more info.
Example bots made with Discord Player
These bots are made by the community, they can help you build your own!
- Discord Music Bot (opens in a new tab) by Androz2091 (opens in a new tab)
- Karasu-Music-Bot (opens in a new tab) by ItsAuric (opens in a new tab)
- Dodong (opens in a new tab) by nizeic (opens in a new tab)
- Musico (opens in a new tab) by Whirl21 (opens in a new tab)
- Melody (opens in a new tab) by NerdyTechy (opens in a new tab)
- Eyesense-Music-Bot (opens in a new tab) by naseif (opens in a new tab)
- Music-bot (opens in a new tab) by ZerioDev (opens in a new tab)
- AtlantaBot (opens in a new tab) by Androz2091 (opens in a new tab) (outdated)
- Discord-Music (opens in a new tab) by inhydrox (opens in a new tab) (outdated)