UNPKG

dressed

Version:

A sleek, serverless-ready Discord bot framework.

63 lines 2.92 kB
import { ComponentType, } from "discord-api-types/v10"; const blurbs = { 3: "a string select", 4: "a text input", 5: "a user select", 6: "a role select", 7: "a mentionable select", 8: "a channel select", 19: "a file upload", }; export function getField(custom_id, required, components, resolved) { const component = components.find((c) => c.custom_id === custom_id); if (!component) { if (required) throw new Error(`Required field "${custom_id}" not found`); return undefined; } const returnValue = (type, resolvedKey) => () => { if (component.type !== type) { throw new Error(`The field ${custom_id} is ${blurbs[component.type]}, not ${blurbs[type]}`); } if (component.type === ComponentType.TextInput) { return component.value; } else { if (resolvedKey) { if (!(resolved === null || resolved === void 0 ? void 0 : resolved[resolvedKey])) { throw new Error(`No ${resolvedKey} found for field ${component.custom_id}`); } const resolveds = []; for (const value of component.values) { resolveds.push(resolved[resolvedKey][value]); } return resolveds; } return component.values; } }; // TODO Remove this assign and just return the getters before next major release return Object.assign(component.type === ComponentType.TextInput ? component.value : {}, { stringSelect: returnValue(ComponentType.StringSelect), textInput: returnValue(ComponentType.TextInput), userSelect: returnValue(ComponentType.UserSelect, "users"), roleSelect: returnValue(ComponentType.RoleSelect, "roles"), mentionableSelect() { var _a, _b, _c; if (component.type !== ComponentType.MentionableSelect) { throw new Error(`The field ${component.custom_id} is ${blurbs[component.type]}, not a mentionable select`); } 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 for field ${component.custom_id}`); } const mentionables = []; for (const value of component.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; }, channelSelect: returnValue(ComponentType.ChannelSelect, "channels"), fileUpload: returnValue(ComponentType.FileUpload, "attachments"), }); } //# sourceMappingURL=fields.js.map