Getting started
1. Create the player
Connect discord.js gateway events to Discord Player.
You are reading the v8 docsDev
Create src/player.ts. It creates the Discord client and the Player, and connects them.
import { Client, Events, GatewayIntentBits } from 'discord.js';
import { Player } from 'discord-player';
import { directMedia } from './direct-media.ts';
export const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates],
});
export const player = new Player({
plugins: [directMedia()],
send(guildId, payload) {
const guild = client.guilds.cache.get(guildId);
if (!guild) throw new Error(`Guild ${guildId} is not cached, so its shard is unknown.`);
return guild.shard.send(payload);
},
});
client.on(Events.Raw, (packet) => player.receive(packet));
client.once(Events.ClientReady, async (ready) => {
await player.initialize(ready.user.id);
console.log(`Ready as ${ready.user.tag}`);
});
player.on('error', (error) => console.error('Player error:', error));What each part does
| Part | Purpose |
|---|---|
GuildVoiceStates intent | Lets the bot see voice state changes |
send | Sends voice join and leave requests to the shard that owns the guild |
player.receive() | Receives every raw gateway packet. Do not filter packets before calling it |
player.initialize() | Starts Remedia. Call it once, after the bot is ready |
plugins | Source plugins registered with this Player |
The Player has no nodes option here, so it uses one embedded Remedia node.
send throws when it cannot find the guild. A silent undefined return would leave a voice join waiting until it times out.
Use one Player
Create a single Player for the whole application and share it between commands.
Continue reading
2. Add a source