How to Create a Discord Bot with Slash Commands Using discord.js¶
In this tutorial, we'll walk you through creating a Discord bot that responds to slash commands using the discord.js library. Slash commands are a new way for users to interact with bots on Discord, providing a more intuitive and user-friendly experience. Let's get started!
Prerequisites¶
Before we begin, make sure you have the following: - Node.js installed on your machine. - A Discord account and a Discord server where you can test your bot. - Basic knowledge of JavaScript.
Step 1: Setting Up Your Project¶
First, create a new directory for your project and initialize it with npm:
mkdir discord-bot
cd discord-bot
npm init -y
Step 2: Installing Dependencies¶
Next, install the required dependencies:
npm install discord.js @discordjs/rest discord-api-types @discordjs/builders
Step 3: Creating the Bot Code¶
Create a file named index.js in your project directory and add the following code:
const { Client, Intents } = require('discord.js');
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { SlashCommandBuilder } = require('@discordjs/builders');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const TOKEN = 'YOUR_BOT_TOKEN';
const CLIENT_ID = 'YOUR_CLIENT_ID';
const GUILD_ID = 'YOUR_GUILD_ID';
// Define the slash command
const commands = [
new SlashCommandBuilder()
.setName('hello')
.setDescription('Replies with Hello, world!'),
].map(command => command.toJSON());
const rest = new REST({ version: '9' }).setToken(TOKEN);
// Register the slash command
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands(CLIENT_ID, GUILD_ID),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
if (commandName === 'hello') {
await interaction.reply('Hello, world!');
}
});
client.login(TOKEN);
Step 4: Replacing Placeholders¶
Replace YOUR_BOT_TOKEN, YOUR_CLIENT_ID, and YOUR_GUILD_ID with your bot token, client ID, and guild ID, respectively. You can find these in the Discord Developer Portal.
Step 5: Running Your Bot¶
Finally, run your bot using Node.js:
node index.js
Conclusion¶
You should now have a basic Discord bot that responds to the /hello slash command. This example can be expanded with more commands and features as needed. Slash commands provide a powerful way to enhance user interaction with your bot, making it more accessible and easier to use.
Happy coding!