Skip to content

Commit

Permalink
refactor(Messages): Improve ColorConvert error (#10108)
Browse files Browse the repository at this point in the history
* feat(Messages): improve `ColorConvert` error

* style(Util): consistency
  • Loading branch information
Jiralite authored Jan 30, 2024

Unverified

This user has not yet uploaded their public signing key.
1 parent bfc7bb5 commit fc1f8ae
Showing 2 changed files with 13 additions and 6 deletions.
2 changes: 1 addition & 1 deletion packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
@@ -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.',
17 changes: 12 additions & 5 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
@@ -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 (resolvedColor < 0 || resolvedColor > 0xffffff) {
throw new DiscordjsRangeError(ErrorCodes.ColorRange);
}

if (color < 0 || color > 0xffffff) throw new DiscordjsRangeError(ErrorCodes.ColorRange);
if (typeof color !== 'number' || Number.isNaN(color)) throw new DiscordjsTypeError(ErrorCodes.ColorConvert);
if (typeof resolvedColor !== 'number' || Number.isNaN(resolvedColor)) {
throw new DiscordjsTypeError(ErrorCodes.ColorConvert, color);
}

return color;
return resolvedColor;
}

/**

0 comments on commit fc1f8ae

Please sign in to comment.