-
Notifications
You must be signed in to change notification settings - Fork 127
Support ColorResolvable via "color" ArgumentType. #81
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
base: master
Are you sure you want to change the base?
Conversation
It's a little messy, so I welcome anyone to improve it, but this adjustment allows common names, hex values (with and without #), RANDOM, RGB array, and numbers to be used as values for the color ArgumentType. Note that when using an RGB array, either the array must not have spaces in it, or `quoted: true` must be applied to the argument. This method takes advantage of the resolveColor() method from discord.js. This theoretically means that anything that can be considered ColorResolvable should work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review
This change is great since it can now parse HEX values, RGB values, and CSS keywords.
I have no problem with the current way it parses but I feel spaces need to be replaced with underscores in order for the resolveColor
function to work properly if you supply it with a CSS keyword.
Regex Matching
If anyone feels this would perform better with regex, I've made one regex pattern that matches the three colour types.
/^(?:(?<hex>#[0-9A-F]{6})|\[(?<r>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]),(?<g>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5]),(?<b>[0-9]|[0-9]{2}|[0-1][0-9]{2}|2[0-4][0-9]|25[0-5])\]|(?<string>[A-Z_]{1,19}))$/i
Here's a link if you want to test it. Although it might need a small change for matching CSS keywords, since it currently matches the pattern strictly to have underscores instead of spaces, and have a maximum length of 19
, as the longest CSS keyword that discord.js can parse is LUMINOUS_VIVID_PINK
, which is 19 characters long.
It has named groups, so you know which type is detected.
hex
- for hexadecimal value matchesr
,g
,b
- for RGB value matches (those are 3 groups not one)string
- for CSS keyword matches.
Note:
string
currently matches any word, and is not limited to CSS keywords strictly.
To sum up, this change will open a lot of chances for matching colors as a built-in type. More helpful for programmers who have no idea of TypeResolver#addType. Looking forward to this PR getting merged!
I never knew CSS keywords were supported. If that's really true, it could certainly be accounted for. |
Yeah, check it out https://discord.js.org/#/docs/main/stable/typedef/ColorResolvable |
const { resolveColor } = require('discord.js');
console.log(resolveColor('DARK_RED')); // 10038562
console.log(resolveColor('DARK RED')); // 218 |
I think I understand now. I'll do some tinkering. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, that's good for now!
Very simple fix. Just had to add |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe destructuring from Util
is clearer.
Suggested by Papaia Co-authored-by: Papaia <43409674+Papaia@users.noreply.github.com>
Finally, jfc... |
It's a little messy, so I welcome anyone to improve it, but this adjustment allows common names, hex values (with and without #),
'RANDOM'
, RGB array, and numbers to be used as values for the'color'
ArgumentType
.Note that when using an RGB array, either the array must not have spaces in it, or
quoted: true
must be applied to the argument.This method takes advantage of the
resolveColor()
method fromdiscord.js
. This theoretically means that anything that can be consideredColorResolvable
should work. I've tested it with hex with and without #, RGB array with and without spaces/quotes, numbers, common names, andRANDOM
, as listed here in the Discord.js documentation.This method also will not break the existing method which is to simply use a hex code as the value of an argument with
type: 'color'
.