UNPKG

dressed

Version:

A sleek, serverless-ready Discord bot framework.

73 lines 3.42 kB
import { ApplicationCommandOptionType, } from "discord-api-types/v10"; const blurbs = { 1: "a subcommand", 2: "a subcommand group", 3: "a string", 4: "an integer", 5: "a boolean", 6: "a user", 7: "a channel", 8: "a role", 9: "a mentionable", 10: "a number", 11: "an attachment", }; export function getOption(name, required, options, resolved) { const option = options.find((o) => o.name === name); if (!option) { if (required) throw new Error(`Required option "${name}" not found`); return undefined; } const returnOption = (type, resolvedKey) => () => { if (option.type !== type) { throw new Error(`The option ${name} is ${blurbs[option.type]}, not ${blurbs[type]}`); } if (resolvedKey) { if (!(resolved === null || resolved === void 0 ? void 0 : resolved[resolvedKey])) { throw new Error(`No ${resolvedKey} found for option ${option.name}`); } return resolved[resolvedKey][option.value]; } return option.value; }; return { subcommand() { if (option.type !== ApplicationCommandOptionType.Subcommand) { throw new Error(`The option ${name} is ${blurbs[option.type]}, not a subcommand`); } return { name, getOption: (n, r) => { var _a; return getOption(n, r !== null && r !== void 0 ? r : false, (_a = option.options) !== null && _a !== void 0 ? _a : [], resolved); }, }; }, subcommandGroup() { if (option.type !== ApplicationCommandOptionType.SubcommandGroup) { throw new Error(`The option ${option.name} is ${blurbs[option.type]}, not a subcommand group`); } return { name, getSubcommand: (n) => { var _a; return (_a = getOption(n, false, option.options, resolved)) === null || _a === void 0 ? void 0 : _a.subcommand(); }, }; }, string: returnOption(ApplicationCommandOptionType.String), integer: returnOption(ApplicationCommandOptionType.Integer), boolean: returnOption(ApplicationCommandOptionType.Boolean), user: returnOption(ApplicationCommandOptionType.User, "users"), channel: returnOption(ApplicationCommandOptionType.Channel, "channels"), role: returnOption(ApplicationCommandOptionType.Role, "roles"), mentionable() { var _a, _b, _c; if (option.type !== ApplicationCommandOptionType.Mentionable) { throw new Error(`The option ${option.name} is ${blurbs[option.type]}, not a mentionable`); } 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 option ${option.name}`); } return (_b = (_a = resolved.users) === null || _a === void 0 ? void 0 : _a[option.value]) !== null && _b !== void 0 ? _b : (_c = resolved.roles) === null || _c === void 0 ? void 0 : _c[option.value]; }, number: returnOption(ApplicationCommandOptionType.Number), attachment: returnOption(ApplicationCommandOptionType.Attachment, "attachments"), }; } //# sourceMappingURL=options.js.map