UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

145 lines (143 loc) 6.5 kB
"use strict"; const require_lib_utils_preconditions_PreconditionContainerSingle = require('./PreconditionContainerSingle.cjs'); const require_lib_utils_preconditions_conditions_PreconditionConditionAnd = require('./conditions/PreconditionConditionAnd.cjs'); const require_lib_utils_preconditions_conditions_PreconditionConditionOr = require('./conditions/PreconditionConditionOr.cjs'); let discord_js = require("discord.js"); //#region src/lib/utils/preconditions/PreconditionContainerArray.ts /** * The run mode for a {@link PreconditionContainerArray}. * @since 1.0.0 */ let PreconditionRunMode = /* @__PURE__ */ function(PreconditionRunMode$1) { /** * The entries are run sequentially, this is the default behaviour and can be slow when doing long asynchronous * tasks, but is performance savvy. * @since 1.0.0 */ PreconditionRunMode$1[PreconditionRunMode$1["Sequential"] = 0] = "Sequential"; /** * All entries are run in parallel using `Promise.all`, then the results are processed after all of them have * completed. * @since 1.0.0 */ PreconditionRunMode$1[PreconditionRunMode$1["Parallel"] = 1] = "Parallel"; return PreconditionRunMode$1; }({}); /** * The condition for a {@link PreconditionContainerArray}. */ let PreconditionRunCondition = /* @__PURE__ */ function(PreconditionRunCondition$1) { /** * Defines a condition where all the entries must pass. This uses {@link PreconditionConditionAnd}. * @since 1.0.0 */ PreconditionRunCondition$1[PreconditionRunCondition$1["And"] = 0] = "And"; /** * Defines a condition where at least one entry must pass. This uses {@link PreconditionConditionOr}. * @since 1.0.0 */ PreconditionRunCondition$1[PreconditionRunCondition$1["Or"] = 1] = "Or"; return PreconditionRunCondition$1; }({}); function isSingle(entry) { return typeof entry === "string" || Reflect.has(entry, "name"); } /** * An {@link IPreconditionContainer} that defines an array of multiple {@link IPreconditionContainer}s. * * By default, array containers run either of two conditions: AND and OR ({@link PreconditionRunCondition}), the top level * will always default to AND, where the nested one flips the logic (OR, then children arrays are AND, then OR...). * * This allows `['Connect', ['Moderator', ['DJ', 'SongAuthor']]]` to become a thrice-nested precondition container, where: * - Level 1: [Single(Connect), Array] runs AND, both containers must return a successful value. * - Level 2: [Single(Moderator), Array] runs OR, either container must return a successful value. * - Level 3: [Single(DJ), Single(SongAuthor)] runs AND, both containers must return a successful value. * * In other words, it is identical to doing: * ```typescript * Connect && (Moderator || (DJ && SongAuthor)); * ``` * @remark More advanced logic can be accomplished by adding more {@link IPreconditionCondition}s (e.g. other operators), * see {@link PreconditionContainerArray.conditions} for more information. * @since 1.0.0 */ var PreconditionContainerArray = class PreconditionContainerArray { constructor(data = [], parent = null) { this.entries = []; this.runCondition = parent?.runCondition === PreconditionRunCondition.And ? PreconditionRunCondition.Or : PreconditionRunCondition.And; if (Array.isArray(data)) { const casted = data; this.mode = parent?.mode ?? PreconditionRunMode.Sequential; this.parse(casted); } else { const casted = data; this.mode = casted.mode; this.parse(casted.entries); } } /** * Adds a new entry to the array. * @since 1.0.0 * @param entry The value to add to the entries. */ add(entry) { this.entries.push(entry); return this; } append(entry) { this.entries.push(entry instanceof PreconditionContainerArray ? entry : new require_lib_utils_preconditions_PreconditionContainerSingle.PreconditionContainerSingle(entry)); return this; } /** * 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 command precondition. */ messageRun(message, command, context = {}) { return this.mode === PreconditionRunMode.Sequential ? this.condition.messageSequential(message, command, this.entries, context) : this.condition.messageParallel(message, command, this.entries, context); } /** * 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 precondition. */ chatInputRun(interaction, command, context = {}) { return this.mode === PreconditionRunMode.Sequential ? this.condition.chatInputSequential(interaction, command, this.entries, context) : this.condition.chatInputParallel(interaction, command, this.entries, context); } /** * 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 precondition. */ contextMenuRun(interaction, command, context = {}) { return this.mode === PreconditionRunMode.Sequential ? this.condition.contextMenuSequential(interaction, command, this.entries, context) : this.condition.contextMenuParallel(interaction, command, this.entries, context); } /** * Parses the precondition entry resolvables, and adds them to the entries. * @since 1.0.0 * @param entries The entries to parse. */ parse(entries) { for (const entry of entries) this.add(isSingle(entry) ? new require_lib_utils_preconditions_PreconditionContainerSingle.PreconditionContainerSingle(entry) : new PreconditionContainerArray(entry, this)); return this; } /** * Retrieves a condition from {@link PreconditionContainerArray.conditions}, assuming existence. * @since 1.0.0 */ get condition() { return PreconditionContainerArray.conditions.get(this.runCondition); } }; PreconditionContainerArray.conditions = new discord_js.Collection([[PreconditionRunCondition.And, require_lib_utils_preconditions_conditions_PreconditionConditionAnd.PreconditionConditionAnd], [PreconditionRunCondition.Or, require_lib_utils_preconditions_conditions_PreconditionConditionOr.PreconditionConditionOr]]); //#endregion exports.PreconditionContainerArray = PreconditionContainerArray; exports.PreconditionRunCondition = PreconditionRunCondition; exports.PreconditionRunMode = PreconditionRunMode; //# sourceMappingURL=PreconditionContainerArray.cjs.map