UNPKG

dressed

Version:

A sleek, serverless-ready Discord bot framework.

108 lines 5.2 kB
import { isMessageComponentSelectMenuInteraction } from "discord-api-types/utils"; import { ApplicationCommandType, ComponentType, InteractionType } from "discord-api-types/v10"; import { getField } from "./fields.js"; import { getOption } from "./options.js"; import { baseInteractionMethods } from "./responses.js"; export function createInteraction(input) { var _a; const methods = baseInteractionMethods(input); switch (input.type) { case InteractionType.ApplicationCommand: { const { data } = input; switch (data.type) { case ApplicationCommandType.Message: // @ts-expect-error Property is on return type input.target = data.resolved.messages[data.target_id]; break; case ApplicationCommandType.User: // @ts-expect-error Property is on return type input.target = { ...data.resolved.users[data.target_id], member: (_a = data.resolved.members) === null || _a === void 0 ? void 0 : _a[data.target_id] }; } return { ...input, ...methods, getOption: (n, r) => { var _a; return getOption(n, r !== null && r !== void 0 ? r : false, "options" in input.data ? ((_a = input.data.options) !== null && _a !== void 0 ? _a : []) : [], input.data.resolved); }, }; } case InteractionType.ApplicationCommandAutocomplete: { return { ...input, ...methods, getOption: (n) => getOption(n, false, input.data.options, input.data.resolved), }; } case InteractionType.MessageComponent: { return { ...input, ...methods, getValues: () => { var _a, _b, _c; if (!isMessageComponentSelectMenuInteraction(input)) { throw new Error("The function getValues may only be used on select menus"); } const resolved = "resolved" in input.data ? input.data.resolved : {}; const returnValue = (resolvedKey) => { if (resolvedKey) { if (!(resolved === null || resolved === void 0 ? void 0 : resolved[resolvedKey])) { throw new Error(`No ${resolvedKey} found`); } const resolveds = []; for (const value of input.data.values) { resolveds.push(resolved[resolvedKey][value]); } return resolveds; } return input.data.values; }; switch (input.data.component_type) { case ComponentType.StringSelect: return returnValue(); case ComponentType.UserSelect: return returnValue("users"); case ComponentType.RoleSelect: return returnValue("roles"); case ComponentType.MentionableSelect: { if (!(resolved === null || resolved === void 0 ? void 0 : resolved.users) && !(resolved === null || resolved === void 0 ? void 0 : resolved.roles)) { throw new Error(`No mentionables found`); } const mentionables = []; for (const value of input.data.values) { mentionables.push((_b = (_a = resolved.users) === null || _a === void 0 ? void 0 : _a[value]) !== null && _b !== void 0 ? _b : (_c = resolved.roles) === null || _c === void 0 ? void 0 : _c[value]); } return mentionables; } case ComponentType.ChannelSelect: return returnValue("channels"); } }, }; } case InteractionType.ModalSubmit: { const components = []; for (const component of input.data.components) { switch (component.type) { case ComponentType.ActionRow: components.push(...component.components); break; case ComponentType.Label: components.push(component.component); break; case ComponentType.TextDisplay: continue; } } return { ...input, ...methods, getField: (c, r) => getField(c, r !== null && r !== void 0 ? r : false, components, input.data.resolved), }; } default: { return null; } } } //# sourceMappingURL=interaction.js.map