Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(Messages): Improve ColorConvert error #10108

Merged
merged 2 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/discord.js/src/errors/Messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
17 changes: 12 additions & 5 deletions packages/discord.js/src/util/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Loading