Documentation Index
Fetch the complete documentation index at: https://mintlify.com/betterdiscord/betterdiscord/llms.txt
Use this file to discover all available pages before exploring further.
BdApi.Commands is a utility class for managing Discord slash commands. This allows plugins to register and manage their own custom commands.
Types
The Commands API exposes several type enums for building commands:
BdApi.Commands.Types = {
OptionTypes, // Types for command options
CommandTypes, // Types of commands
InputTypes, // Input field types
MessageEmbedTypes // Message embed types
}
Methods
register
Registers a new command.
BdApi.Commands.register(callerOrCommand: string | object, command?: object): Function | undefined
Caller name (plugin name) or command object if caller is preset
Command object (optional if caller is preset)
Returns: Function | undefined - Unregister function that can be called to remove the command
Command object structure:
Unique identifier for the command
Name of the command (displayed to users)
Description of what the command does
Array of command options/parameters
Function to execute when the command is used
Example
const unregister = BdApi.Commands.register("MyPlugin", {
id: "hello",
name: "hello",
description: "Say hello to someone",
options: [
{
type: BdApi.Commands.Types.OptionTypes.STRING,
name: "user",
description: "User to greet",
required: true
}
],
execute: async (data, context) => {
const userName = data.find(o => o.name === "user").value;
return {
content: `Hello, ${userName}!`
};
}
});
// Later, to unregister:
// unregister();
unregister
Unregisters a specific command.
BdApi.Commands.unregister(callerOrCommandId: string, commandId?: string): void
Caller name or command ID if caller is preset
Command ID (optional if caller is preset)
Example
BdApi.Commands.unregister("MyPlugin", "hello");
unregisterAll
Unregisters all commands for a specific caller.
BdApi.Commands.unregisterAll(caller: string): void
Name of the caller whose commands should be unregistered
Example
// Unregister all commands from MyPlugin
BdApi.Commands.unregisterAll("MyPlugin");
getCommandsByCaller
Gets all commands registered by a specific caller.
BdApi.Commands.getCommandsByCaller(caller: string): Array<object>
Name of the caller whose commands should be retrieved
Returns: Array<object> - Array of command objects registered by the caller
Example
const myCommands = BdApi.Commands.getCommandsByCaller("MyPlugin");
console.log(`Registered commands: ${myCommands.length}`);
Complete example
Here’s a complete example of a plugin that registers multiple commands:
module.exports = class MyPlugin {
constructor() {
this.commands = [];
}
start() {
// Register a simple command
this.commands.push(
BdApi.Commands.register("MyPlugin", {
id: "status",
name: "status",
description: "Check plugin status",
execute: async () => {
return {
content: "Plugin is running!"
};
}
})
);
// Register a command with options
this.commands.push(
BdApi.Commands.register("MyPlugin", {
id: "echo",
name: "echo",
description: "Echo a message",
options: [
{
type: BdApi.Commands.Types.OptionTypes.STRING,
name: "message",
description: "Message to echo",
required: true
},
{
type: BdApi.Commands.Types.OptionTypes.BOOLEAN,
name: "uppercase",
description: "Convert to uppercase",
required: false
}
],
execute: async (data, context) => {
let message = data.find(o => o.name === "message").value;
const uppercase = data.find(o => o.name === "uppercase")?.value;
if (uppercase) {
message = message.toUpperCase();
}
return { content: message };
}
})
);
// Register a command with embed response
this.commands.push(
BdApi.Commands.register("MyPlugin", {
id: "info",
name: "info",
description: "Display plugin information",
execute: async () => {
return {
embeds: [{
color: 0x3a71c1,
title: "MyPlugin",
description: "A BetterDiscord plugin",
fields: [
{ name: "Version", value: "1.0.0", inline: true },
{ name: "Author", value: "Me", inline: true }
]
}]
};
}
})
);
}
stop() {
// Unregister all commands
this.commands.forEach(unregister => unregister());
this.commands = [];
// Alternative: BdApi.Commands.unregisterAll("MyPlugin");
}
};
Command with choices
BdApi.Commands.register("MyPlugin", {
id: "theme",
name: "theme",
description: "Manage themes",
options: [
{
type: BdApi.Commands.Types.OptionTypes.STRING,
name: "action",
description: "Action to perform",
required: true,
choices: [
{ name: "Enable", value: "enable" },
{ name: "Disable", value: "disable" },
{ name: "List", value: "list" }
]
}
],
execute: async (data, context) => {
const action = data.find(o => o.name === "action").value;
// Handle action...
}
});