Best way to use Preconditions to determine which commands show up in a /help command?

I'm looking to make a dynamic help command that'll show a user all commands available to them, but I'm really not sure how to go about checking to make sure a command is able to be run by the user before adding it to the embed. Currently the core logic for the command looks like so:
public override async messageRun(message: Message) {
const commands = this.container.stores.get('commands');
const embed = new EmbedBuilder().setTitle('Help - List of Commands').setColor('Blue').setDescription('Here are the commands you can use:');

commands.forEach((cmd) => {
// Optional: Check if the user has permission to use the command
if (cmd.enabled && !cmd.options.preconditions?.includes('OwnerOnly')) {
embed.addFields({
name: cmd.name,
value: cmd.description
});
}
});

return await message.channel.send({ embeds: [embed] });
}
public override async messageRun(message: Message) {
const commands = this.container.stores.get('commands');
const embed = new EmbedBuilder().setTitle('Help - List of Commands').setColor('Blue').setDescription('Here are the commands you can use:');

commands.forEach((cmd) => {
// Optional: Check if the user has permission to use the command
if (cmd.enabled && !cmd.options.preconditions?.includes('OwnerOnly')) {
embed.addFields({
name: cmd.name,
value: cmd.description
});
}
});

return await message.channel.send({ embeds: [embed] });
}
I'm familiar with preconditions a bit, but unsure of how sapphire's framework actually does the checking behind the scenes to see if a user passes precondition checks, I was thinking maybe that could be a way I could test each command to see if the author of the message/user of the interaction (for chatInputRun) is able to run the command. I'm also aware I'll probably have to do some further filtering rather than just getting all the commands from the stores (since I believe there will be duplicate entries for commands that have messageRun/chatInputRun/contextMenuRun methods) but right now I'm focused on just figuring out a good way of figuring out whether or not to include a command based on the user using the help command first. Any ideas?
Solution:
First of all there won't be duplicate entries. The entry will simply have both messageRun and chatInputRun Secondly, for slash commands you do not need a help command because you have description fields for the commands and options already Thirdly, you can take inspiration from @Skyra https://github.com/skyra-project/skyra/blob/main/src/commands/General/help.ts#L189-L206...
Jump to solution
2 Replies
Solution
Favna
Favna12mo ago
First of all there won't be duplicate entries. The entry will simply have both messageRun and chatInputRun Secondly, for slash commands you do not need a help command because you have description fields for the commands and options already Thirdly, you can take inspiration from @Skyra https://github.com/skyra-project/skyra/blob/main/src/commands/General/help.ts#L189-L206
Doc Ker
Doc KerOP12mo ago
Yeah the help command is meant just for showing available messageRun commands, and thank you for the link I definitely will <3
Want results from more Discord servers?
Add your server