ermcy
ermcy
SIASapphire - Imagine a Framework
Created by ermcy on 4/7/2025 in #sapphire-support
`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.
21 replies