mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
refactor(Messages): Improve ColorConvert error (#10108)
* feat(Messages): improve `ColorConvert` error * style(Util): consistency
This commit is contained in:
@@ -39,7 +39,7 @@ const Messages = {
|
||||
`Calculated invalid shard ${shard} for guild ${guild} with ${count} shards.`,
|
||||
|
||||
[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]:
|
||||
'A valid guild channel must be provided when GuildScheduledEvent is EXTERNAL.',
|
||||
|
||||
@@ -278,19 +278,26 @@ function verifyString(
|
||||
* @returns {number} A color
|
||||
*/
|
||||
function resolveColor(color) {
|
||||
let resolvedColor;
|
||||
|
||||
if (typeof color === 'string') {
|
||||
if (color === 'Random') return Math.floor(Math.random() * (0xffffff + 1));
|
||||
if (color === 'Default') return 0;
|
||||
if (/^#?[\da-f]{6}$/i.test(color)) return parseInt(color.replace('#', ''), 16);
|
||||
color = Colors[color];
|
||||
resolvedColor = Colors[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 (typeof color !== 'number' || Number.isNaN(color)) throw new DiscordjsTypeError(ErrorCodes.ColorConvert);
|
||||
if (resolvedColor < 0 || resolvedColor > 0xffffff) {
|
||||
throw new DiscordjsRangeError(ErrorCodes.ColorRange);
|
||||
}
|
||||
|
||||
return color;
|
||||
if (typeof resolvedColor !== 'number' || Number.isNaN(resolvedColor)) {
|
||||
throw new DiscordjsTypeError(ErrorCodes.ColorConvert, color);
|
||||
}
|
||||
|
||||
return resolvedColor;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user