To get started with DisTube, install the core library along with the necessary voice dependencies. You must initialize a DisTube instance by passing your discord.js client and an options object.
Note that when using DisTube, your Discord client must have the following GatewayIntentBits enabled:
GuildsGuildVoiceStatesGuildMessagesMessageContent
You can listen to events like playSong to interact with the queue and use distube.play() to start music playback in a voice channel.
const { DisTube } = require('distube');
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildVoiceStates,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
],
});
const distube = new DisTube(client, {
emitNewSongOnly: true,
});
distube.on('playSong', (queue, song) =>
queue.textChannel.send(`Playing \`${song.name}\` - \`${song.formatDuration()}\``)
);
client.on('messageCreate', message => {
if (message.content.startsWith('!play')) {
distube.play(message.member.voice.channel, message.content.slice(6), {
message,
textChannel: message.channel,
member: message.member,
});
}
});
client.login('TOKEN');