meshcore.js
Guides

Restrict who can run a command

A role for the owners and a permission on the command.

Only the owners can post on the channel. Everyone else gets a polite refusal.

roles/owner.ts
export const owner = new RoleBuilder()
  .setName('owner')
  .setDescription('Bot owners')
  .setPriority(1000)
  .addPermissions(Permissions.Administrator)
  .setMembers((process.env.OWNER_KEYS ?? '').split(',').filter(Boolean));
commands/announce.ts
export default new CommandBuilder()
  .setName('announce')
  .setDescription('Post on the club channel')
  .addStringArg((arg) => arg.setName('text').setRequired().setRest())
  .setRequiredPermissions(Permissions.Administrator)
  .setHandler(async (ctx) => {
    await ctx.client.channels.get('#club')?.send(ctx.args.text);
    return ctx.reply('Posted');
  });

What happens

The role lives under roles/, the command under commands/. A role has a priority, permissions and members. Members are public keys, or key prefixes of twelve characters or more. The bot never stores them. They come from your own source, here an environment variable.

setRequiredPermissions() on the command is checked before the handler runs. Without the permission the person gets Permission denied.

A channel author cannot be verified. A command with a required permission is refused on a channel and the bot asks for a DM instead.

Go further

On this page