UNPKG

@sapphire/framework

Version:

Discord bot framework built for advanced and amazing bots.

1 lines • 13.5 kB
{"version":3,"file":"options.cjs","names":["option: APIApplicationCommandOption","optionTypeToPrettyName","checkOptionType","checkName","checkLocalizations","checkDescription","checkOptionRequired","subcommandTypes","ApplicationCommandOptionType","hasMinMaxValueSupport","handleMinMaxValueOptions","hasChoicesAndAutocompleteSupport","handleAutocomplete","hasMinMaxLengthSupport","handleMinMaxLengthOptions","hasChannelTypesSupport","checkChannelTypes"],"sources":["../../../../../../src/lib/utils/application-commands/compute-differences/options.ts"],"sourcesContent":["import {\n\tApplicationCommandOptionType,\n\ttype APIApplicationCommandBasicOption,\n\ttype APIApplicationCommandChannelOption,\n\ttype APIApplicationCommandOption\n} from 'discord-api-types/v10';\nimport {\n\thasChannelTypesSupport,\n\thasChoicesAndAutocompleteSupport,\n\thasMinMaxLengthSupport,\n\thasMinMaxValueSupport,\n\toptionTypeToPrettyName,\n\tsubcommandTypes,\n\ttype APIApplicationCommandChoosableAndAutocompletableTypes,\n\ttype APIApplicationCommandMinAndMaxValueTypes,\n\ttype APIApplicationCommandMinMaxLengthTypes,\n\ttype APIApplicationCommandSubcommandTypes,\n\ttype CommandDifference\n} from './_shared';\nimport { checkDescription } from './description';\nimport { checkLocalizations } from './localizations';\nimport { checkName } from './name';\nimport { handleAutocomplete } from './option/autocomplete';\nimport { checkChannelTypes } from './option/channelTypes';\nimport { handleMinMaxLengthOptions } from './option/minMaxLength';\nimport { handleMinMaxValueOptions } from './option/minMaxValue';\nimport { checkOptionRequired } from './option/required';\nimport { checkOptionType } from './option/type';\n\nexport function* checkOptions(\n\texistingOptions?: APIApplicationCommandOption[],\n\tnewOptions?: APIApplicationCommandOption[]\n): Generator<CommandDifference> {\n\t// 0. No existing options and now we have options\n\tif (!existingOptions?.length && newOptions?.length) {\n\t\tyield {\n\t\t\tkey: 'options',\n\t\t\toriginal: 'no options present',\n\t\t\texpected: 'options present'\n\t\t};\n\t}\n\t// 1. Existing options and now we have no options\n\telse if (existingOptions?.length && !newOptions?.length) {\n\t\tyield {\n\t\t\tkey: 'options',\n\t\t\toriginal: 'options present',\n\t\t\texpected: 'no options present'\n\t\t};\n\t}\n\t// 2. Iterate over each option if we have any and see what's different\n\telse if (newOptions?.length) {\n\t\tlet index = 0;\n\t\tfor (const option of newOptions) {\n\t\t\tconst currentIndex = index++;\n\t\t\tconst existingOption = existingOptions![currentIndex];\n\t\t\tyield* reportOptionDifferences({ currentIndex, option, existingOption });\n\t\t}\n\n\t\t// If we went through less options than we previously had, report that\n\t\tif (index < existingOptions!.length) {\n\t\t\tlet option: APIApplicationCommandOption;\n\t\t\twhile ((option = existingOptions![index]) !== undefined) {\n\t\t\t\tconst expectedType =\n\t\t\t\t\toptionTypeToPrettyName.get(option.type) ?? `unknown (${option.type}); please contact Sapphire developers about this!`;\n\n\t\t\t\tyield {\n\t\t\t\t\tkey: `existing command option at index ${index}`,\n\t\t\t\t\texpected: 'no option present',\n\t\t\t\t\toriginal: `${expectedType} with name ${option.name}`\n\t\t\t\t};\n\n\t\t\t\tindex++;\n\t\t\t}\n\t\t}\n\t}\n}\n\nfunction* reportOptionDifferences({\n\toption,\n\texistingOption,\n\tcurrentIndex,\n\tkeyPath = (index: number) => `options[${index}]`\n}: {\n\toption: APIApplicationCommandOption;\n\tcurrentIndex: number;\n\texistingOption?: APIApplicationCommandOption;\n\tkeyPath?: (index: number) => string;\n}): Generator<CommandDifference> {\n\t// If current option doesn't exist, report and return\n\tif (!existingOption) {\n\t\tconst expectedType = optionTypeToPrettyName.get(option.type) ?? `unknown (${option.type}); please contact Sapphire developers about this!`;\n\n\t\tyield {\n\t\t\tkey: keyPath(currentIndex),\n\t\t\texpected: `${expectedType} with name ${option.name}`,\n\t\t\toriginal: 'no option present'\n\t\t};\n\n\t\treturn;\n\t}\n\n\t// Check type\n\tyield* checkOptionType({\n\t\tkey: `${keyPath(currentIndex)}.type`,\n\t\toriginalType: existingOption.type,\n\t\texpectedType: option.type\n\t});\n\n\t// Check name\n\tyield* checkName({\n\t\tkey: `${keyPath(currentIndex)}.name`,\n\t\toldName: existingOption.name,\n\t\tnewName: option.name\n\t});\n\n\t// Check localized names\n\tconst originalLocalizedNames = existingOption.name_localizations;\n\tconst expectedLocalizedNames = option.name_localizations;\n\n\tyield* checkLocalizations({\n\t\tlocaleMapName: `${keyPath(currentIndex)}.nameLocalizations`,\n\t\tlocalePresentMessage: 'localized names',\n\t\tlocaleMissingMessage: 'no localized names',\n\t\toriginalLocalizedDescriptions: originalLocalizedNames,\n\t\texpectedLocalizedDescriptions: expectedLocalizedNames\n\t});\n\n\t// Check description\n\tyield* checkDescription({\n\t\tkey: `${keyPath(currentIndex)}.description`,\n\t\toldDescription: existingOption.description,\n\t\tnewDescription: option.description\n\t});\n\n\t// Check localized descriptions\n\tconst originalLocalizedDescriptions = existingOption.description_localizations;\n\tconst expectedLocalizedDescriptions = option.description_localizations;\n\n\tyield* checkLocalizations({\n\t\tlocaleMapName: `${keyPath(currentIndex)}.descriptionLocalizations`,\n\t\tlocalePresentMessage: 'localized descriptions',\n\t\tlocaleMissingMessage: 'no localized descriptions',\n\t\toriginalLocalizedDescriptions,\n\t\texpectedLocalizedDescriptions\n\t});\n\n\t// Check required\n\tyield* checkOptionRequired({\n\t\tkey: `${keyPath(currentIndex)}.required`,\n\t\toldRequired: existingOption.required,\n\t\tnewRequired: option.required\n\t});\n\n\t// Check for subcommands\n\tif (subcommandTypes.includes(existingOption.type) && subcommandTypes.includes(option.type)) {\n\t\tconst castedExisting = existingOption as APIApplicationCommandSubcommandTypes;\n\t\tconst castedExpected = option as APIApplicationCommandSubcommandTypes;\n\n\t\tif (\n\t\t\tcastedExisting.type === ApplicationCommandOptionType.SubcommandGroup &&\n\t\t\tcastedExpected.type === ApplicationCommandOptionType.SubcommandGroup\n\t\t) {\n\t\t\t// We know we have options in this case, because they are both groups\n\t\t\tfor (const [subcommandIndex, subcommandOption] of castedExpected.options!.entries()) {\n\t\t\t\tyield* reportOptionDifferences({\n\t\t\t\t\tcurrentIndex: subcommandIndex,\n\t\t\t\t\toption: subcommandOption,\n\t\t\t\t\texistingOption: castedExisting.options?.[subcommandIndex],\n\t\t\t\t\tkeyPath: (index) => `${keyPath(currentIndex)}.options[${index}]`\n\t\t\t\t});\n\t\t\t}\n\t\t} else if (\n\t\t\tcastedExisting.type === ApplicationCommandOptionType.Subcommand &&\n\t\t\tcastedExpected.type === ApplicationCommandOptionType.Subcommand\n\t\t) {\n\t\t\tyield* handleSubcommandOptions({\n\t\t\t\texpectedOptions: castedExpected.options,\n\t\t\t\texistingOptions: castedExisting.options,\n\t\t\t\tcurrentIndex,\n\t\t\t\tkeyPath\n\t\t\t});\n\t\t}\n\t}\n\n\tif (hasMinMaxValueSupport(option)) {\n\t\t// Check min and max_value\n\t\tconst existingCasted = existingOption as APIApplicationCommandMinAndMaxValueTypes;\n\n\t\tyield* handleMinMaxValueOptions({\n\t\t\tcurrentIndex,\n\t\t\texistingOption: existingCasted,\n\t\t\texpectedOption: option,\n\t\t\tkeyPath\n\t\t});\n\t}\n\n\tif (hasChoicesAndAutocompleteSupport(option)) {\n\t\tconst existingCasted = existingOption as APIApplicationCommandChoosableAndAutocompletableTypes;\n\n\t\tyield* handleAutocomplete({\n\t\t\texpectedOption: option,\n\t\t\texistingOption: existingCasted,\n\t\t\tcurrentIndex,\n\t\t\tkeyPath\n\t\t});\n\t}\n\n\tif (hasMinMaxLengthSupport(option)) {\n\t\t// Check min and max_value\n\t\tconst existingCasted = existingOption as APIApplicationCommandMinMaxLengthTypes;\n\n\t\tyield* handleMinMaxLengthOptions({\n\t\t\tcurrentIndex,\n\t\t\texistingOption: existingCasted,\n\t\t\texpectedOption: option,\n\t\t\tkeyPath\n\t\t});\n\t}\n\n\tif (hasChannelTypesSupport(option)) {\n\t\t// Check channel_types\n\t\tconst existingCasted = existingOption as APIApplicationCommandChannelOption;\n\n\t\tyield* checkChannelTypes({\n\t\t\tcurrentIndex,\n\t\t\texistingChannelTypes: existingCasted.channel_types,\n\t\t\tkeyPath,\n\t\t\tnewChannelTypes: option.channel_types\n\t\t});\n\t}\n}\n\nfunction* handleSubcommandOptions({\n\texpectedOptions,\n\texistingOptions,\n\tcurrentIndex,\n\tkeyPath\n}: {\n\texpectedOptions?: APIApplicationCommandBasicOption[];\n\texistingOptions?: APIApplicationCommandBasicOption[];\n\tcurrentIndex: number;\n\tkeyPath: (index: number) => string;\n}): Generator<CommandDifference> {\n\t// 0. No existing options and now we have options\n\tif (!existingOptions?.length && expectedOptions?.length) {\n\t\tyield {\n\t\t\tkey: `${keyPath(currentIndex)}.options`,\n\t\t\texpected: 'options present',\n\t\t\toriginal: 'no options present'\n\t\t};\n\t}\n\n\t// 1. Existing options and now we have no options\n\telse if (existingOptions?.length && !expectedOptions?.length) {\n\t\tyield {\n\t\t\tkey: `${keyPath(currentIndex)}.options`,\n\t\t\texpected: 'no options present',\n\t\t\toriginal: 'options present'\n\t\t};\n\t}\n\n\t// 2. Iterate over each option if we have any and see what's different\n\telse if (expectedOptions?.length) {\n\t\tlet processedIndex = 0;\n\t\tfor (const subcommandOption of expectedOptions) {\n\t\t\tconst currentSubCommandOptionIndex = processedIndex++;\n\t\t\tconst existingSubcommandOption = existingOptions![currentSubCommandOptionIndex];\n\n\t\t\tyield* reportOptionDifferences({\n\t\t\t\tcurrentIndex: currentSubCommandOptionIndex,\n\t\t\t\toption: subcommandOption,\n\t\t\t\texistingOption: existingSubcommandOption,\n\t\t\t\tkeyPath: (index) => `${keyPath(currentIndex)}.options[${index}]`\n\t\t\t});\n\t\t}\n\n\t\t// If we went through less options than we previously had, report that\n\t\tif (processedIndex < existingOptions!.length) {\n\t\t\tlet option: APIApplicationCommandOption;\n\t\t\twhile ((option = existingOptions![processedIndex]) !== undefined) {\n\t\t\t\tconst expectedType =\n\t\t\t\t\toptionTypeToPrettyName.get(option.type) ?? `unknown (${option.type}); please contact Sapphire developers about this!`;\n\n\t\t\t\tyield {\n\t\t\t\t\tkey: `existing command option at path ${keyPath(currentIndex)}.options[${processedIndex}]`,\n\t\t\t\t\texpected: 'no option present',\n\t\t\t\t\toriginal: `${expectedType} with name ${option.name}`\n\t\t\t\t};\n\n\t\t\t\tprocessedIndex++;\n\t\t\t}\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;AA6BA,UAAiB,aAChB,iBACA,YAC+B;AAE/B,KAAI,CAAC,iBAAiB,UAAU,YAAY,OAC3C,OAAM;EACL,KAAK;EACL,UAAU;EACV,UAAU;EACV;UAGO,iBAAiB,UAAU,CAAC,YAAY,OAChD,OAAM;EACL,KAAK;EACL,UAAU;EACV,UAAU;EACV;UAGO,YAAY,QAAQ;EAC5B,IAAI,QAAQ;AACZ,OAAK,MAAM,UAAU,YAAY;GAChC,MAAM,eAAe;GACrB,MAAM,iBAAiB,gBAAiB;AACxC,UAAO,wBAAwB;IAAE;IAAc;IAAQ;IAAgB,CAAC;;AAIzE,MAAI,QAAQ,gBAAiB,QAAQ;GACpC,IAAIA;AACJ,WAAQ,SAAS,gBAAiB,YAAY,QAAW;IACxD,MAAM,eACLC,0FAAuB,IAAI,OAAO,KAAK,IAAI,YAAY,OAAO,KAAK;AAEpE,UAAM;KACL,KAAK,oCAAoC;KACzC,UAAU;KACV,UAAU,GAAG,aAAa,aAAa,OAAO;KAC9C;AAED;;;;;AAMJ,UAAU,wBAAwB,EACjC,QACA,gBACA,cACA,WAAW,UAAkB,WAAW,MAAM,MAMd;AAEhC,KAAI,CAAC,gBAAgB;EACpB,MAAM,eAAeA,0FAAuB,IAAI,OAAO,KAAK,IAAI,YAAY,OAAO,KAAK;AAExF,QAAM;GACL,KAAK,QAAQ,aAAa;GAC1B,UAAU,GAAG,aAAa,aAAa,OAAO;GAC9C,UAAU;GACV;AAED;;AAID,QAAOC,uFAAgB;EACtB,KAAK,GAAG,QAAQ,aAAa,CAAC;EAC9B,cAAc,eAAe;EAC7B,cAAc,OAAO;EACrB,CAAC;AAGF,QAAOC,0EAAU;EAChB,KAAK,GAA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