Limit command to specified roles?

I honestly haven't done this in a while (not since pre-application commands) and am new to using Sapphire. Looking to restrict a command to only be visible/usable by the server's owner and any roles they define. How is this best handled with Sapphire and application commands? The bot in question has a database to store roles if that's needed. Using TypeScript, here's a basic command I wrote that I want to restrict as described:
// commands/example.ts
import { ApplyOptions } from '@sapphire/decorators';
import { Command } from '@sapphire/framework';

@ApplyOptions<Command.Options>({
description: 'An example command, that should only be accessed by server owner and users with roles defined'
})

export class UserCommand extends Command {
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder
.setName(this.name)
.setDescription(this.description)
);
}

public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
return interaction.reply(`Command example!`);
}
}
// commands/example.ts
import { ApplyOptions } from '@sapphire/decorators';
import { Command } from '@sapphire/framework';

@ApplyOptions<Command.Options>({
description: 'An example command, that should only be accessed by server owner and users with roles defined'
})

export class UserCommand extends Command {
public override registerApplicationCommands(registry: Command.Registry) {
registry.registerChatInputCommand((builder) =>
builder
.setName(this.name)
.setDescription(this.description)
);
}

public override async chatInputRun(interaction: Command.ChatInputCommandInteraction) {
return interaction.reply(`Command example!`);
}
}
Already looked over docs and previous questions, though maybe I've missed something, call me out if I have haha
Solution:
You'll have to write and handle your own precondition https://sapphirejs.dev/docs/Guide/preconditions/what-are-preconditions
Sapphire Framework
What are preconditions and how do they work? | Sapphire
Preconditions are classes that will determine whether or not a command should be ran according to certain conditions.
Jump to solution
9 Replies
Solution
Favna
Favna9mo ago
You'll have to write and handle your own precondition https://sapphirejs.dev/docs/Guide/preconditions/what-are-preconditions
Sapphire Framework
What are preconditions and how do they work? | Sapphire
Preconditions are classes that will determine whether or not a command should be ran according to certain conditions.
Tony
Tony9mo ago
I assume just storing role IDs in the database, then checking if the user has one of those roles? I was hoping there was a way to tell Discord to hide the command from people that didn't have the roles, though I have no idea what exists
KaydaFox
KaydaFox9mo ago
the only way with to do it using a role is through discords UI, but that's down to server admins to do, so if its multi-server probably not worth that way another way is using default_member_permissions and then setting a permission only that role should have, e.g. admin
Tony
Tony9mo ago
This is only for one server, is that using the intergrations UI in server settings?
KaydaFox
KaydaFox9mo ago
yes if you go in there, find your application itll give you a list of commands. you can select the one that's needed and make it only usable by that role ^^
KaydaFox
KaydaFox9mo ago
as a small example, "the gang" is the role i want to be able to use this command, so i enable it for that and disable it for @everyone
No description
Tony
Tony9mo ago
Ahh, very cool. Would this suffice instead of a preconditon, or should I do it just in case?
KaydaFox
KaydaFox9mo ago
i'd say it would suffice, its how i handle it where needed if you wanted you could combine them both but that's down to you although if you are also using message based commands as well as application commands, you'd need the precondition or atleast some sort of check
Tony
Tony9mo ago
Nah, just application commands. I might do it as a precondition still for when the bot's been added but it hasn't been set up in the settings anyway Might be useful to have a list of authorized roles for that later on anyway
Want results from more Discord servers?
Add your server