|
| 1 | +import { |
| 2 | + ApplicationCommandOptionType, |
| 3 | + type APIApplicationCommandBasicOption, |
| 4 | + type APIApplicationCommandOption, |
| 5 | + type APIApplicationCommandStringOption |
| 6 | +} from 'discord-api-types/v10'; |
| 7 | +import { |
| 8 | + hasChoicesAndAutocompleteSupport, |
| 9 | + hasMinMaxLengthSupport, |
| 10 | + hasMinMaxValueSupport, |
| 11 | + optionTypeToPrettyName, |
| 12 | + subcommandTypes, |
| 13 | + type APIApplicationCommandChoosableAndAutocompletableTypes, |
| 14 | + type APIApplicationCommandNumericTypes, |
| 15 | + type APIApplicationCommandSubcommandTypes, |
| 16 | + type CommandDifference |
| 17 | +} from './_shared'; |
| 18 | +import { checkDescription } from './description'; |
| 19 | +import { checkLocalizations } from './localizations'; |
| 20 | +import { checkName } from './name'; |
| 21 | +import { handleAutocomplete } from './option/autocomplete'; |
| 22 | +import { handleMinMaxLengthOptions } from './option/minMaxLength'; |
| 23 | +import { handleMinMaxValueOptions } from './option/minMaxValue'; |
| 24 | +import { checkOptionRequired } from './option/required'; |
| 25 | +import { checkOptionType } from './option/type'; |
| 26 | + |
| 27 | +export function* reportOptionDifferences({ |
| 28 | + option, |
| 29 | + existingOption, |
| 30 | + currentIndex, |
| 31 | + keyPath = (index: number) => `options[${index}]` |
| 32 | +}: { |
| 33 | + option: APIApplicationCommandOption; |
| 34 | + currentIndex: number; |
| 35 | + existingOption?: APIApplicationCommandOption; |
| 36 | + keyPath?: (index: number) => string; |
| 37 | +}): Generator<CommandDifference> { |
| 38 | + // If current option doesn't exist, report and return |
| 39 | + if (!existingOption) { |
| 40 | + const expectedType = optionTypeToPrettyName.get(option.type) ?? `unknown (${option.type}); please contact Sapphire developers about this!`; |
| 41 | + |
| 42 | + yield { |
| 43 | + key: keyPath(currentIndex), |
| 44 | + expected: `${expectedType} with name ${option.name}`, |
| 45 | + original: 'no option present' |
| 46 | + }; |
| 47 | + |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Check type |
| 52 | + yield* checkOptionType({ |
| 53 | + key: `${keyPath(currentIndex)}.type`, |
| 54 | + originalType: existingOption.type, |
| 55 | + expectedType: option.type |
| 56 | + }); |
| 57 | + |
| 58 | + // Check name |
| 59 | + yield* checkName({ |
| 60 | + key: `${keyPath(currentIndex)}.name`, |
| 61 | + oldName: existingOption.name, |
| 62 | + newName: option.name |
| 63 | + }); |
| 64 | + |
| 65 | + // Check localized names |
| 66 | + const originalLocalizedNames = existingOption.name_localizations; |
| 67 | + const expectedLocalizedNames = option.name_localizations; |
| 68 | + |
| 69 | + yield* checkLocalizations({ |
| 70 | + localeMapName: `${keyPath(currentIndex)}.nameLocalizations`, |
| 71 | + localePresentMessage: 'localized names', |
| 72 | + localeMissingMessage: 'no localized names', |
| 73 | + originalLocalizedDescriptions: originalLocalizedNames, |
| 74 | + expectedLocalizedDescriptions: expectedLocalizedNames |
| 75 | + }); |
| 76 | + |
| 77 | + // Check description |
| 78 | + yield* checkDescription({ |
| 79 | + key: `${keyPath(currentIndex)}.description`, |
| 80 | + oldDescription: existingOption.description, |
| 81 | + newDescription: option.description |
| 82 | + }); |
| 83 | + |
| 84 | + // Check localized descriptions |
| 85 | + const originalLocalizedDescriptions = existingOption.description_localizations; |
| 86 | + const expectedLocalizedDescriptions = option.description_localizations; |
| 87 | + |
| 88 | + yield* checkLocalizations({ |
| 89 | + localeMapName: `${keyPath(currentIndex)}.descriptionLocalizations`, |
| 90 | + localePresentMessage: 'localized descriptions', |
| 91 | + localeMissingMessage: 'no localized descriptions', |
| 92 | + originalLocalizedDescriptions, |
| 93 | + expectedLocalizedDescriptions |
| 94 | + }); |
| 95 | + |
| 96 | + // Check required |
| 97 | + yield* checkOptionRequired({ |
| 98 | + key: `${keyPath(currentIndex)}.required`, |
| 99 | + oldRequired: existingOption.required, |
| 100 | + newRequired: option.required |
| 101 | + }); |
| 102 | + |
| 103 | + // Check for subcommands |
| 104 | + if (subcommandTypes.includes(existingOption.type) && subcommandTypes.includes(option.type)) { |
| 105 | + const castedExisting = existingOption as APIApplicationCommandSubcommandTypes; |
| 106 | + const castedExpected = option as APIApplicationCommandSubcommandTypes; |
| 107 | + |
| 108 | + if ( |
| 109 | + castedExisting.type === ApplicationCommandOptionType.SubcommandGroup && |
| 110 | + castedExpected.type === ApplicationCommandOptionType.SubcommandGroup |
| 111 | + ) { |
| 112 | + // We know we have options in this case, because they are both groups |
| 113 | + for (const [subcommandIndex, subcommandOption] of castedExpected.options!.entries()) { |
| 114 | + yield* reportOptionDifferences({ |
| 115 | + currentIndex: subcommandIndex, |
| 116 | + option: subcommandOption, |
| 117 | + existingOption: castedExisting.options?.[subcommandIndex], |
| 118 | + keyPath: (index) => `${keyPath(currentIndex)}.options[${index}]` |
| 119 | + }); |
| 120 | + } |
| 121 | + } else if ( |
| 122 | + castedExisting.type === ApplicationCommandOptionType.Subcommand && |
| 123 | + castedExpected.type === ApplicationCommandOptionType.Subcommand |
| 124 | + ) { |
| 125 | + yield* handleSubcommandOptions({ |
| 126 | + expectedOptions: castedExpected.options, |
| 127 | + existingOptions: castedExisting.options, |
| 128 | + currentIndex, |
| 129 | + keyPath |
| 130 | + }); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + if (hasMinMaxValueSupport(option)) { |
| 135 | + // Check min and max_value |
| 136 | + const existingCasted = existingOption as APIApplicationCommandNumericTypes; |
| 137 | + |
| 138 | + yield* handleMinMaxValueOptions({ |
| 139 | + currentIndex, |
| 140 | + existingOption: existingCasted, |
| 141 | + expectedOption: option, |
| 142 | + keyPath |
| 143 | + }); |
| 144 | + } |
| 145 | + |
| 146 | + if (hasChoicesAndAutocompleteSupport(option)) { |
| 147 | + const existingCasted = existingOption as APIApplicationCommandChoosableAndAutocompletableTypes; |
| 148 | + |
| 149 | + yield* handleAutocomplete({ |
| 150 | + expectedOption: option, |
| 151 | + existingOption: existingCasted, |
| 152 | + currentIndex, |
| 153 | + keyPath |
| 154 | + }); |
| 155 | + } |
| 156 | + |
| 157 | + if (hasMinMaxLengthSupport(option)) { |
| 158 | + // Check min and max_value |
| 159 | + const existingCasted = existingOption as APIApplicationCommandStringOption; |
| 160 | + |
| 161 | + yield* handleMinMaxLengthOptions({ |
| 162 | + currentIndex, |
| 163 | + existingOption: existingCasted, |
| 164 | + expectedOption: option, |
| 165 | + keyPath |
| 166 | + }); |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +function* handleSubcommandOptions({ |
| 171 | + expectedOptions, |
| 172 | + existingOptions, |
| 173 | + currentIndex, |
| 174 | + keyPath |
| 175 | +}: { |
| 176 | + expectedOptions?: APIApplicationCommandBasicOption[]; |
| 177 | + existingOptions?: APIApplicationCommandBasicOption[]; |
| 178 | + currentIndex: number; |
| 179 | + keyPath: (index: number) => string; |
| 180 | +}): Generator<CommandDifference> { |
| 181 | + // 0. No existing options and now we have options |
| 182 | + if (!existingOptions?.length && expectedOptions?.length) { |
| 183 | + yield { |
| 184 | + key: `${keyPath(currentIndex)}.options`, |
| 185 | + expected: 'options present', |
| 186 | + original: 'no options present' |
| 187 | + }; |
| 188 | + } |
| 189 | + |
| 190 | + // 1. Existing options and now we have no options |
| 191 | + else if (existingOptions?.length && !expectedOptions?.length) { |
| 192 | + yield { |
| 193 | + key: `${keyPath(currentIndex)}.options`, |
| 194 | + expected: 'no options present', |
| 195 | + original: 'options present' |
| 196 | + }; |
| 197 | + } |
| 198 | + |
| 199 | + // 2. Iterate over each option if we have any and see what's different |
| 200 | + else if (expectedOptions?.length) { |
| 201 | + let processedIndex = 0; |
| 202 | + for (const subcommandOption of expectedOptions) { |
| 203 | + const currentSubCommandOptionIndex = processedIndex++; |
| 204 | + const existingSubcommandOption = existingOptions![currentSubCommandOptionIndex]; |
| 205 | + |
| 206 | + yield* reportOptionDifferences({ |
| 207 | + currentIndex: currentSubCommandOptionIndex, |
| 208 | + option: subcommandOption, |
| 209 | + existingOption: existingSubcommandOption, |
| 210 | + keyPath: (index) => `${keyPath(currentIndex)}.options[${index}]` |
| 211 | + }); |
| 212 | + } |
| 213 | + |
| 214 | + // If we went through less options than we previously had, report that |
| 215 | + if (processedIndex < existingOptions!.length) { |
| 216 | + let option: APIApplicationCommandOption; |
| 217 | + while ((option = existingOptions![processedIndex]) !== undefined) { |
| 218 | + const expectedType = |
| 219 | + optionTypeToPrettyName.get(option.type) ?? `unknown (${option.type}); please contact Sapphire developers about this!`; |
| 220 | + |
| 221 | + yield { |
| 222 | + key: `existing command option at path ${keyPath(currentIndex)}.options[${processedIndex}]`, |
| 223 | + expected: 'no option present', |
| 224 | + original: `${expectedType} with name ${option.name}` |
| 225 | + }; |
| 226 | + |
| 227 | + processedIndex++; |
| 228 | + } |
| 229 | + } |
| 230 | + } |
| 231 | +} |
0 commit comments