Discord Player

Complete framework to simplify the implementation of music commands using d...

README

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.

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 has resources to help with that.

Main Library


  1. ```bash
  2. $ yarn add discord-player # main library
  3. $ yarn add @discord-player/extractor # extractors provider
  4. ```

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:

  1. ```bash
  2. $ yarn add @discordjs/opus
  3. # or
  4. $ yarn add opusscript
  5. ```

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 is not recommended):

  1. ```bash
  2. $ yarn add ffmpeg-static
  3. ```

You can get avconv from https://libav.org/download.

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:

  1. ```bash
  2. $ yarn add ytdl-core
  3. # or
  4. $ yarn add play-dl
  5. # or
  6. $ yarn add @distube/ytdl-core
  7. ```

Done with all these? Let's write a simple music bot then.

Setup


Let's create a master player instance.

  1. ```js
  2. const { Player } = require('discord-player');
  3. const client = new Discord.Client({
  4.     // Make sure you have 'GuildVoiceStates' intent enabled
  5.     intents: ['GuildVoiceStates' /* Other intents */]
  6. });

  7. // this is the entrypoint for discord-player based application
  8. const player = new Player(client);
  9. ```

Did You Know? _Discord Player is by default a singleton._


Now, let's add some event listeners:

  1. ```js
  2. // this event is emitted whenever discord-player starts to play a track
  3. player.events.on('playerStart', (queue, track) => {
  4.     // we will later define queue.metadata object while creating the queue
  5.     queue.metadata.channel.send(`Started playing **${track.title}**!`);
  6. });
  7. ```

Let's write the command part. You can define the command as you desire. We will only check the command handler part:

  1. ```js
  2. async function execute(interaction) {
  3.     const channel = interaction.message.member.voice.channel;
  4.     if (!channel) return interaction.reply('You are not connected to a voice channel!'); // make sure we have a voice channel
  5.     const query = interaction.options.getString('query', true); // we need input/query to play

  6.     // let's defer the interaction as things can take time to process
  7.     await interaction.deferReply();

  8.     try {
  9.         const { track } = await player.play(channel, query, {
  10.             nodeOptions: {
  11.                 // nodeOptions are the options for guild node (aka your queue in simple word)
  12.                 metadata: interaction // we can access this metadata object using queue.metadata later on
  13.             }
  14.         });

  15.         return interaction.followUp(`**${track.title}** enqueued!`);
  16.     } catch (e) {
  17.         // let's return error if something failed
  18.         return interaction.followUp(`Something went wrong: ${e}`);
  19.     }
  20. }
  21. ```

That's all it takes to build your own music bot.

Check out the Documentation for more info.


Community Resources


A curated list of resources (such as open source music bots, extractors, etc.) built by Discord Player community.