Creating a Discord music bot using play-dl can be done in a few simple steps. First, you will need to install the required packages discord.js and play-dl. You can install them by running the following command:
npm install discord.js play-dl
Next, create a file named index.js. This is where the bot's code will be stored. In this file, you will need to import and create instances of the following modules: require discord.js, play-dl, and create a bot instance.
Here's an example code for the music bot:
const { Client, Intents } = require('discord.js');
const { audioPlayer, createAudioResource, StreamType } = require('@discordjs/voice');
const { videoInfo, StreamExtractor } = require('play-dl');
const token = 'YOUR_BOT_TOKEN';
const client = new Client({ intents: [Intents.FLAGS.Guilds, Intents.FLAGS.GuildMessages, Intents.FLAGS.GuildVoiceStates] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('messageCreate', async message => {
if (message.author.bot) return;
if (message.content.startsWith('!play')) {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.reply('You must be in a voice channel to play music.');
const permissions = voiceChannel.permissionsFor(message.client.user);
if (!permissions.has('CONNECT') || !permissions.has('SPEAK')) return message.reply('I need permission to join and speak in your voice channel.');
const song = message.content.split('!play ')[1];
try {
const info = await videoInfo(song);
const stream = await StreamExtractor(song);
const resource = createAudioResource(stream.stream, { inputType: StreamType.Arbitrary, metadata: { title: info.videoDetails.title } });
const connection = await voiceChannel.join();
const player = connection.subscribe(audioPlayer);
player.play(resource);
message.channel.send(`Now playing: ${info.videoDetails.title}`);
} catch (error) {
console.error(error);
message.channel.send('Error playing the requested song');
}
}
if (message.content === '!stop') {
const voiceChannel = message.member.voice.channel;
if (!voiceChannel) return message.reply('You must be in a voice channel to stop the music.');
const connection = voiceChannel.leave();
if (connection) message.channel.send('Music stopped and disconnected from the voice channel.');
}
});
client.login(token);
This code sets up a basic Discord music bot that will respond to the !play and !stop commands. Replace 'YOUR_BOT_TOKEN' with the actual bot token.
To run your bot, execute the following command in your terminal:
node index.js
Finally, I'll provide a brief tutorial on how to implement this into your Discord bot.
-
Set up your Discord bot if you haven't already by visiting the Discord Developer Portal and creating a new application.
-
Copy the provided token (found under 'Bot') and replace the YOUR_BOT_TOKEN placeholder in the code above.
-
Add the bot to your server by following the OAuth2 URL generator in the Developer Portal, selecting the 'bot' scope, and choosing the appropriate permissions.
-
Once you have the API token and have invited the bot to your server, run the index.js script with the node index.js command. Your bot should appear online, and you can start playing music by typing !play <Youtube URL or search query> in your Discord channel.
-
To stop the music bot, use the !stop command. The bot will stop playing music and disconnect from the voice channel.
Note: This is a basic implementation of a music bot using play-dl. You can extend this functionality by adding a queue system, error handling, or other features as needed.