@sapphire/framework
Version:
Discord bot framework built for advanced and amazing bots.
89 lines (87 loc) • 3.95 kB
JavaScript
"use strict";
const require_lib_errors_Identifiers = require('../../errors/Identifiers.cjs');
const require_lib_errors_UserError = require('../../errors/UserError.cjs');
let __sapphire_pieces = require("@sapphire/pieces");
let __sapphire_result = require("@sapphire/result");
//#region src/lib/utils/preconditions/PreconditionContainerSingle.ts
/**
* An {@link IPreconditionContainer} which runs a single precondition from {@link SapphireClient.preconditions}.
* @since 1.0.0
*/
var PreconditionContainerSingle = class {
constructor(data) {
if (typeof data === "string") {
this.context = {};
this.name = data;
} else {
this.context = Reflect.get(data, "context") ?? {};
this.name = data.name;
}
}
/**
* Runs the container.
* @since 1.0.0
* @param message The message that ran this precondition.
* @param command The command the message invoked.
* @param context The context for the message precondition.
*/
messageRun(message, command, context = {}) {
const precondition = __sapphire_pieces.container.stores.get("preconditions").get(this.name);
if (precondition) return precondition.messageRun ? precondition.messageRun(message, command, {
...context,
...this.context
}) : precondition.error({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionMissingMessageHandler,
message: `The precondition "${precondition.name}" is missing a "messageRun" handler, but it was requested for the "${command.name}" command.`
});
return (0, __sapphire_result.err)(new require_lib_errors_UserError.UserError({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionUnavailable,
message: `The precondition "${this.name}" is not available.`
}));
}
/**
* Runs the container.
* @since 3.0.0
* @param interaction The interaction that ran this precondition.
* @param command The command the interaction invoked.
* @param context The context for the chat input command precondition.
*/
chatInputRun(interaction, command, context = {}) {
const precondition = __sapphire_pieces.container.stores.get("preconditions").get(this.name);
if (precondition) return precondition.chatInputRun ? precondition.chatInputRun(interaction, command, {
...context,
...this.context
}) : precondition.error({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionMissingChatInputHandler,
message: `The precondition "${precondition.name}" is missing a "chatInputRun" handler, but it was requested for the "${command.name}" command.`
});
return (0, __sapphire_result.err)(new require_lib_errors_UserError.UserError({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionUnavailable,
message: `The precondition "${this.name}" is not available.`
}));
}
/**
* Runs the container.
* @since 3.0.0
* @param interaction The interaction that ran this precondition.
* @param command The command the interaction invoked.
* @param context The context for the context menu command precondition.
*/
contextMenuRun(interaction, command, context = {}) {
const precondition = __sapphire_pieces.container.stores.get("preconditions").get(this.name);
if (precondition) return precondition.contextMenuRun ? precondition.contextMenuRun(interaction, command, {
...context,
...this.context
}) : precondition.error({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionMissingContextMenuHandler,
message: `The precondition "${precondition.name}" is missing a "contextMenuRun" handler, but it was requested for the "${command.name}" command.`
});
return (0, __sapphire_result.err)(new require_lib_errors_UserError.UserError({
identifier: require_lib_errors_Identifiers.Identifiers.PreconditionUnavailable,
message: `The precondition "${this.name}" is not available.`
}));
}
};
//#endregion
exports.PreconditionContainerSingle = PreconditionContainerSingle;
//# sourceMappingURL=PreconditionContainerSingle.cjs.map