Sunar

Registering commands

This section covers the essential steps and methods for registering commands in your Discord bot using Sunar.

Create a command

Define a new slash command or a context menu using Sunar's intuitive API and configure its properties such as name, description, options (if applicable), and permissions.

src/commands/ping.js
import { Slash, execute } from 'sunar';
 
const slash = new Slash({
	name: 'ping',
	description: "Ping the bot to check if it's online.",
});
 
execute(slash, (interaction) => {
	interaction.reply('Pong!');
});
 
export { slash };

Listen for the ready signal

Wait for the ready signal from discord.js, indicating that the bot is connected and ready to register commands.

src/signals/ready.js
import { Signal, execute } from 'sunar';
 
const signal = new Signal('ready', { once: true });
 
execute(signal, (client) => {
	console.log(`${client.user.tag} logged!`);
});
 
export { signal };

Register the commands

Now you can add the registerCommands function provided by Sunar to dynamically register all the stored commands.

src/signals/ready.js
import { Signal, execute } from 'sunar';
import { registerCommands } from 'sunar/registry'; 
 
const signal = new Signal('ready', { once: true });
 
execute(signal, async (client) => {
	await registerCommands(client.application); 
 
	console.log(`${client.user.tag} logged!`);
});
 
export { signal };

For more information about the registerCommands function, see dynamic registration.

See more

For global or specific server registration instructions, refer to the following links:

Last updated on

On this page

GitHubEdit on Github ↗