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.
export const owner = new RoleBuilder()
.setName('owner')
.setDescription('Bot owners')
.setPriority(1000)
.addPermissions(Permissions.Administrator)
.setMembers((process.env.OWNER_KEYS ?? '').split(',').filter(Boolean));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, silently —
no reply on the air, only commandDenied with channelUntrusted. Run it in a DM instead. See
Channels are untrusted for why.
Scopes
setScope() takes one or more of 'dm' | 'channel' | 'public' — where the command may be triggered. It
defaults to 'dm' and 'channel': the firmware's Public channel is never included unless a command opts in
explicitly, setScope('public') (alongside the others if it should answer everywhere). See
A well-behaved bot for why Public is opt-in and what else the framework refuses by
default.
Go further
- Model and built-in permissions for
Permissions.*. - Roles and members for member sources and hierarchy.
- A well-behaved bot for scopes, rate limits and
maxHops. - RoleBuilder in the reference.