misc.color: use regex instead

This commit is contained in:
Cynthia Foxwell 2024-07-31 21:45:59 -06:00
parent 1bef3d11a9
commit f85e8c0215

View file

@ -630,14 +630,7 @@ searchInteraction.callback = async function (interaction) {
}; };
hf.registerCommand(searchInteraction); hf.registerCommand(searchInteraction);
const colornamesRaw = fs.readFileSync(resolve(__dirname, "../../data/colornames.csv"), "utf8").split("\n"); const colornamesRaw = fs.readFileSync(resolve(__dirname, "../../data/colornames.csv"), "utf8");
colornamesRaw.shift();
const colornamesMapped = colornamesRaw.map((line) => {
const [hex, name] = line.split(",");
return [hex, name];
});
const colornames = Object.fromEntries(colornamesMapped);
const color = new Command("color"); const color = new Command("color");
color.category = CATEGORY; color.category = CATEGORY;
@ -652,7 +645,7 @@ color.callback = async function (msg, line, args, {truerandom}) {
} }
if (!color.isValid) { if (!color.isValid) {
const colornamesHex = Object.entries(colornames).find(([hex, name]) => name.toLowerCase().localeCompare(line))[0]; const colornamesHex = colornamesRaw.match(new RegExp(`^([0-9a-f]{6}),${line},`, "i"))?.[1];
if (colornamesHex) { if (colornamesHex) {
color = tinycolor(colornamesHex); color = tinycolor(colornamesHex);
} else { } else {
@ -708,7 +701,7 @@ color.callback = async function (msg, line, args, {truerandom}) {
}); });
} }
const colornamesName = colornames[color.toHex()]; const colornamesName = colornamesRaw.match(new RegExp(`^${color.toHex()},([^,]+?),`, "i"))?.[1];
if (colornamesName) { if (colornamesName) {
fields.push({name: "\u200b", value: `**[colornames](https://colornames.org/) Name**\n${colornamesName}`}); fields.push({name: "\u200b", value: `**[colornames](https://colornames.org/) Name**\n${colornamesName}`});
} }