@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
63 lines (61 loc) • 2.7 kB
JavaScript
import { Identifiers } from "../errors/Identifiers.mjs";
import { Precondition } from "./Precondition.mjs";
import { Store } from "@sapphire/pieces";
import { Result } from "@sapphire/result";
//#region src/lib/structures/PreconditionStore.ts
var PreconditionStore = class extends Store {
constructor() {
super(Precondition, { name: "preconditions" });
this.globalPreconditions = [];
}
async messageRun(message, command, context = {}) {
for (const precondition of this.globalPreconditions) {
const result = precondition.messageRun ? await precondition.messageRun(message, command, context) : await precondition.error({
identifier: Identifiers.PreconditionMissingMessageHandler,
message: `The precondition "${precondition.name}" is missing a "messageRun" handler, but it was requested for the "${command.name}" command.`
});
if (result.isErr()) return result;
}
return Result.ok();
}
async chatInputRun(interaction, command, context = {}) {
for (const precondition of this.globalPreconditions) {
const result = precondition.chatInputRun ? await precondition.chatInputRun(interaction, command, context) : await precondition.error({
identifier: Identifiers.PreconditionMissingChatInputHandler,
message: `The precondition "${precondition.name}" is missing a "chatInputRun" handler, but it was requested for the "${command.name}" command.`
});
if (result.isErr()) return result;
}
return Result.ok();
}
async contextMenuRun(interaction, command, context = {}) {
for (const precondition of this.globalPreconditions) {
const result = precondition.contextMenuRun ? await precondition.contextMenuRun(interaction, command, context) : await precondition.error({
identifier: Identifiers.PreconditionMissingContextMenuHandler,
message: `The precondition "${precondition.name}" is missing a "contextMenuRun" handler, but it was requested for the "${command.name}" command.`
});
if (result.isErr()) return result;
}
return Result.ok();
}
set(key, value) {
if (value.position !== null) {
const index = this.globalPreconditions.findIndex((precondition) => precondition.position >= value.position);
if (index === -1) this.globalPreconditions.push(value);
else this.globalPreconditions.splice(index, 0, value);
}
return super.set(key, value);
}
delete(key) {
const index = this.globalPreconditions.findIndex((precondition) => precondition.name === key);
if (index !== -1) this.globalPreconditions.splice(index, 1);
return super.delete(key);
}
clear() {
this.globalPreconditions.length = 0;
return super.clear();
}
};
//#endregion
export { PreconditionStore };
//# sourceMappingURL=PreconditionStore.mjs.map