`Precondition` doubts.

Hi, I'm rewriting my bot from Rust's poise framework to Sapphire. In the rust version, there was a config parameter that let me run a check every time, a command is ran. These are regular commands and not slash commands aka ?avatar. Is there anything similar for Sapphire where I can provide a function that will return either true or false to know if the command should be ran or not. Secondly, how do I handle preconditions that may take sometime to execute, something sort of a database lookup which may take from 50-300ms depending on the workload for the bot. I tried doing something like below
// command file
public constructor(context: Command.LoaderContext) {
super(context, {
preconditions: ["SuperUsersOnly"]
});
}
// src/preconditions/SuperUsersOnly.ts
export class SuperUsersOnlyPrecondition extends Precondition {
public override async chatInputRun(interaction: ChatInputCommandInteraction, command: ChatInputCommand, context: Precondition.Context) {
const it = await interaction.deferReply();
// simulate a db lookup latency
await delay(3000);
await it.delete();
return this.ok();
}
}
// command file
public constructor(context: Command.LoaderContext) {
super(context, {
preconditions: ["SuperUsersOnly"]
});
}
// src/preconditions/SuperUsersOnly.ts
export class SuperUsersOnlyPrecondition extends Precondition {
public override async chatInputRun(interaction: ChatInputCommandInteraction, command: ChatInputCommand, context: Precondition.Context) {
const it = await interaction.deferReply();
// simulate a db lookup latency
await delay(3000);
await it.delete();
return this.ok();
}
}
But I get the error that the interaction was already replied to. I don't see how I would send a defer reply when running the check and if the check passes, the command's chatInputRun can use it interaction response or if it fails pass that error to an external error handler and then respond inside that. Any help is much appreciated.
Solution:
Yeah, I recommend deferring it only in precondition and then use editReply or followUp in the command
Jump to solution
9 Replies
čamdžić
čamdžić2w ago
I didn’t really understand what did you meant, but if you want to do check for prefix commands you can run method messageRun In precondition But if you still want to use chatInput commands you can run chatInputRun in precondition just like you did Also defering a reply should be possible, you probably deferred a reply in command too So you should remove it from the command
ermcy
ermcyOP2w ago
Oh sorry I should've clarified, I'm moving everything from prefix commands to slash commands. Also if i defer in the precondition i shouldn't in the command? i.e. the command will reply to the deferred message from the precondition.
Solution
čamdžić
čamdžić2w ago
Yeah, I recommend deferring it only in precondition and then use editReply or followUp in the command
čamdžić
čamdžić2w ago
Also delete await it.delete() from precondition Lmk if it works
ermcy
ermcyOP2w ago
Ah this works now thanks! For the first part of the post, will I have to make a Precondition which will run before the command is actually run? In the rust version it looked something like this, the check was at the framework level instead of individual command level.
No description
ermcy
ermcyOP2w ago
I don't mind that approach just wondering if there's another method
čamdžić
čamdžić2w ago
Yeah, preconditions are ran before the commands are run I hope I explained it you well and you understand Preconditions are explained really good on guide so you should check that
ermcy
ermcyOP2w ago
Yeah I understand it now, thanks. idk what msg to mark as answer tho
čamdžić
čamdžić2w ago
This one That was your problem

Did you find this page helpful?