Color Check for String

I was wanting to see how I would go about validating a string as a color. I am using a command to change the color of the member's role and if you input anything that isn't valid it kills the bot. I am mainly wanting something like new Color(colorString); and checking if that isn't null before passing it to the role.setColor() function
7 Replies
d.js toolkit
d.js toolkit10mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
Elrol
ElrolOP10mo ago
Discord.JS: 14.14.1 NodeJS: 20.11.0
mallusrgreat
mallusrgreat10mo ago
export const colorPredicate = s.number.int
.greaterThanOrEqual(0)
.lessThanOrEqual(0xffffff)
.or(s.tuple([RGBPredicate, RGBPredicate, RGBPredicate]))
.nullable.setValidationEnabled(isValidationEnabled);
export const colorPredicate = s.number.int
.greaterThanOrEqual(0)
.lessThanOrEqual(0xffffff)
.or(s.tuple([RGBPredicate, RGBPredicate, RGBPredicate]))
.nullable.setValidationEnabled(isValidationEnabled);
This is what is used inside @discordjs/builders using @sapphire/shapeshift
Elrol
ElrolOP10mo ago
That will help for the hex, I supposed I would also have to make an array of valid strings to check if it is one of those as well.
d.js docs
d.js docs10mo ago
:method: resolveColor Resolves a ColorResolvable into a color number.
Elrol
ElrolOP10mo ago
Going to give it a try lol I had this:
try{
role.setColor(color);
} catch(err) {
console.error(err);
}
interaction.reply("Your color has been changed");
try{
role.setColor(color);
} catch(err) {
console.error(err);
}
interaction.reply("Your color has been changed");
But it was printing the error and still crashed the bot I have it working now so its alright lol
Want results from more Discord servers?
Add your server