refactor(Messages): Improve ColorConvert error (#10108)

* feat(Messages): improve `ColorConvert` error

* style(Util): consistency
This commit is contained in:
Jiralite
2024-01-30 07:17:45 +00:00
committed by GitHub
parent bfc7bb5564
commit fc1f8ae374
2 changed files with 13 additions and 6 deletions

View File

@@ -39,7 +39,7 @@ const Messages = {
`Calculated invalid shard ${shard} for guild ${guild} with ${count} shards.`, `Calculated invalid shard ${shard} for guild ${guild} with ${count} shards.`,
[DjsErrorCodes.ColorRange]: 'Color must be within the range 0 - 16777215 (0xFFFFFF).', [DjsErrorCodes.ColorRange]: 'Color must be within the range 0 - 16777215 (0xFFFFFF).',
[DjsErrorCodes.ColorConvert]: 'Unable to convert color to a number.', [DjsErrorCodes.ColorConvert]: color => `Unable to convert "${color}" to a number.`,
[DjsErrorCodes.InviteOptionsMissingChannel]: [DjsErrorCodes.InviteOptionsMissingChannel]:
'A valid guild channel must be provided when GuildScheduledEvent is EXTERNAL.', 'A valid guild channel must be provided when GuildScheduledEvent is EXTERNAL.',

View File

@@ -278,19 +278,26 @@ function verifyString(
* @returns {number} A color * @returns {number} A color
*/ */
function resolveColor(color) { function resolveColor(color) {
let resolvedColor;
if (typeof color === 'string') { if (typeof color === 'string') {
if (color === 'Random') return Math.floor(Math.random() * (0xffffff + 1)); if (color === 'Random') return Math.floor(Math.random() * (0xffffff + 1));
if (color === 'Default') return 0; if (color === 'Default') return 0;
if (/^#?[\da-f]{6}$/i.test(color)) return parseInt(color.replace('#', ''), 16); if (/^#?[\da-f]{6}$/i.test(color)) return parseInt(color.replace('#', ''), 16);
color = Colors[color]; resolvedColor = Colors[color];
} else if (Array.isArray(color)) { } else if (Array.isArray(color)) {
color = (color[0] << 16) + (color[1] << 8) + color[2]; resolvedColor = (color[0] << 16) + (color[1] << 8) + color[2];
} }
if (color < 0 || color > 0xffffff) throw new DiscordjsRangeError(ErrorCodes.ColorRange); if (resolvedColor < 0 || resolvedColor > 0xffffff) {
if (typeof color !== 'number' || Number.isNaN(color)) throw new DiscordjsTypeError(ErrorCodes.ColorConvert); throw new DiscordjsRangeError(ErrorCodes.ColorRange);
}
return color; if (typeof resolvedColor !== 'number' || Number.isNaN(resolvedColor)) {
throw new DiscordjsTypeError(ErrorCodes.ColorConvert, color);
}
return resolvedColor;
} }
/** /**