Registering commandsโ
This step is only required if you are using slash commands. If you are using message commands exclusively, you can skip this page.
For fully functional slash commands, three pieces need to be in place:
- Individual command files with their definitions and functionality.
- The command handler that dynamically reads and executes those files.
- The deployment script on this page, to register commands with Discord so they appear in the interface.
These can be completed in any order, but all three are required before commands are functional. This page covers step 3.
Before you continueโ
Assuming you've followed the guide so far, your project directory should look something like this:
discord-bot/
โโโ node_modules/
โโโ src/
โ โโโ commands/
โ โ โโโ music/
โ โ โโโ play.js
โ โโโ events/
โ โ โโโ discord/
โ โ โ โโโ interactionCreate.js
โ โ โ โโโ messageCreate.js
โ โ โ โโโ raw.js
โ โ โ โโโ ready.js
โ โ โโโ magmastream/
โ โ โโโ nodeConnect.js
โ โ โโโ queueEnd.js
โ โ โโโ trackStart.js
โ โโโ config.js
โ โโโ index.js
โโโ package-lock.json
โโโ package.json
Creating the deployment scriptโ
Create src/deploy-commands.js. This file communicates with the Discord API to register your slash commands so they appear when a user types / in chat.
const config = require("./config.js");
const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v10");
module.exports = async (client) => {
const rest = new REST().setToken(config.token);
try {
console.log(`[INFO] Started refreshing application commands.`);
const data = [];
client.commands.forEach((command) => {
try {
const item = {
name: command.data.name,
description: command.data.description,
};
if (command.data.options) {
item.options = command.data.options;
}
data.push(item);
} catch (error) {}
});
await rest.put(Routes.applicationCommands(client.user.id), { body: data });
console.log(`[INFO] Successfully reloaded ${data.length} application commands.`);
} catch (error) {
console.error(`[ERROR] Failed to reload application commands: ${error}`);
}
};
Deploying the commandsโ
Call the deployment script from your ready.js event so commands are registered every time the bot starts:
Make sure to update src/events/discord/ready.js to call the deployment script once the bot is ready.
const { Events } = require("discord.js");
module.exports = {
name: Events.ClientReady,
once: true,
async execute(client) {
// Deploy slash commands
require("../../deploy-commands")(client);
// Initialize Lavalink
await client.manager.init({ clientId: client.user.id });
console.log(`[INFO] Ready! Logged in as ${client.user.tag}`);
},
};
Final project structureโ
discord-bot/
โโโ node_modules/
โโโ src/
โ โโโ commands/
โ โ โโโ music/
โ โ โโโ play.js
โ โโโ events/
โ โ โโโ discord/
โ โ โ โโโ interactionCreate.js
โ โ โ โโโ messageCreate.js
โ โ โ โโโ raw.js
โ โ โ โโโ ready.js
โ โ โโโ magmastream/
โ โ โโโ nodeConnect.js
โ โ โโโ queueEnd.js
โ โ โโโ trackStart.js
โ โโโ config.js
โ โโโ deploy-commands.js
โ โโโ index.js
โโโ package-lock.json
โโโ package.json
With that, the quick start guide is complete. Run node . in your terminal and you should have a fully functional bot capable of playing songs, titles, URLs, and playlists โ with all events handled in their own files.
Resulting codeโ
You can review the complete example on the GitHub repository here .