Skip to main content
Version: v2.10.1

Registering commandsโ€‹

note

This step is only required if you are using slash commands. If you are using message commands exclusively, you can skip this page.

note

For fully functional slash commands, three pieces need to be in place:

  1. Individual command files with their definitions and functionality.
  2. The command handler that dynamically reads and executes those files.
  3. 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:

warning

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 .