UNPKG

@sern/handler

Version:

A complete, customizable, typesafe, & reactive framework for discord bots.

73 lines 3.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = exports.reconstruct = void 0; const discord_js_1 = require("discord.js"); const enums_1 = require("./structures/enums"); const parseParams = (event, append) => { const hasSlash = event.customId.indexOf('/'); if (hasSlash === -1) { return { id: event.customId + append }; } const baseid = event.customId.substring(0, hasSlash); const params = event.customId.substring(hasSlash + 1); return { id: baseid + append, params }; }; /** * Construct unique ID for a given interaction object. * @param event The interaction object for which to create an ID. * @returns An array of unique string IDs based on the type and properties of the interaction object. */ function reconstruct(event) { switch (event.type) { case discord_js_1.InteractionType.MessageComponent: { const data = parseParams(event, `_C${event.componentType}`); return [data]; } case discord_js_1.InteractionType.ApplicationCommand: case discord_js_1.InteractionType.ApplicationCommandAutocomplete: return [{ id: `${event.commandName}_A${event.commandType}` }, { id: `${event.commandName}_B` }]; //Modal interactions are classified as components for sern case discord_js_1.InteractionType.ModalSubmit: { const data = parseParams(event, '_M'); return [data]; } } } exports.reconstruct = reconstruct; /** * * A magic number to represent any commandtype that is an ApplicationCommand. */ const PUBLISHABLE = 0b000000001111; const TypeMap = new Map([[enums_1.CommandType.Text, 0], [enums_1.CommandType.Both, 0], [enums_1.CommandType.Slash, discord_js_1.ApplicationCommandType.ChatInput], [enums_1.CommandType.CtxUser, discord_js_1.ApplicationCommandType.User], [enums_1.CommandType.CtxMsg, discord_js_1.ApplicationCommandType.Message], [enums_1.CommandType.Button, discord_js_1.ComponentType.Button], [enums_1.CommandType.StringSelect, discord_js_1.ComponentType.StringSelect], [enums_1.CommandType.Modal, discord_js_1.InteractionType.ModalSubmit], [enums_1.CommandType.UserSelect, discord_js_1.ComponentType.UserSelect], [enums_1.CommandType.MentionableSelect, discord_js_1.ComponentType.MentionableSelect], [enums_1.CommandType.RoleSelect, discord_js_1.ComponentType.RoleSelect], [enums_1.CommandType.ChannelSelect, discord_js_1.ComponentType.ChannelSelect]]); /* * Generates an id based on name and CommandType. * A is for any ApplicationCommand. C is for any ComponentCommand * Then, another number fetched from TypeMap */ function create(name, type) { if (type == enums_1.CommandType.Text) { return `${name}_T`; } if (type == enums_1.CommandType.Both) { return `${name}_B`; } if (type == enums_1.CommandType.Modal) { return `${name}_M`; } const am = (PUBLISHABLE & type) !== 0 ? 'A' : 'C'; return `${name}_${am}${TypeMap.get(type)}`; } exports.create = create; //# sourceMappingURL=id.js.map