Preconditions

Using preconditions, If a user does not meet the requirements to run a command I want the bot delete the message instead of sending a message, how can I do it?
5 Replies
-Carlos👑
-Carlos👑OP•16mo ago
bump
Silver
Silver•16mo ago
you've already explained how you'd do it so I don't really get what you're stuck on? 😅 you'd just iterate over the command's preconditions to check if any of them fail and if so, delete the message based on that
Lioness100
Lioness100•16mo ago
You can use the messageCommandDenied event. I modified the code from https://www.sapphirejs.dev/docs/Guide/preconditions/reporting-precondition-failure to fit this use case. It would be something like:
import { Events, Listener, type MessageCommandDeniedPayload, type UserError } from '@sapphire/framework';

export class MessageCommandDenied extends Listener<typeof Events.MessageCommandDenied> {
public run(error: UserError, { message }: MessageCommandDeniedPayload) {
// Maybe do some logging?
return message.delete().catch(() => null);
}
}
import { Events, Listener, type MessageCommandDeniedPayload, type UserError } from '@sapphire/framework';

export class MessageCommandDenied extends Listener<typeof Events.MessageCommandDenied> {
public run(error: UserError, { message }: MessageCommandDeniedPayload) {
// Maybe do some logging?
return message.delete().catch(() => null);
}
}
I added the .catch(() => null) to silently fail if the bot can't delete the message (it was deleted already, the bot doesn't have permissions, etc.)
Sapphire Framework
Reporting precondition failure | Sapphire
When a precondition fails, it's usually important for the user to know why. For example, if they hit a cooldown or lack
Lioness100
Lioness100•16mo ago
The above would function for all preconditions. If you only want it for a single precondition, then delete the message the same way in the messageRun method. Suppose you only want it for certain preconditions. In that case, you can add shouldDeleteMessage (or similar) to the context, and read that in the messageCommandDenied event to see if you should delete the message. See the guide page I linked for examples of using precondition context.
-Carlos👑
-Carlos👑OP•16mo ago
Let me look into it. Thanks!
Want results from more Discord servers?
Add your server