misc.color: add option to take first colornames color

This commit is contained in:
Cynthia Foxwell 2024-07-31 22:03:43 -06:00
parent 64a15df40c
commit c7c7cfa75c

View file

@ -635,7 +635,7 @@ const colornamesRaw = fs.readFileSync(resolve(__dirname, "../../data/colornames.
const color = new Command("color"); const color = new Command("color");
color.category = CATEGORY; color.category = CATEGORY;
color.helpText = "Show information on a color or get a random color"; color.helpText = "Show information on a color or get a random color";
color.callback = async function (msg, line, args, {truerandom}) { color.callback = async function (msg, line, args, {truerandom, first}) {
let color = tinycolor(line), let color = tinycolor(line),
random = false; random = false;
@ -646,10 +646,12 @@ color.callback = async function (msg, line, args, {truerandom}) {
if (!color.isValid) { if (!color.isValid) {
const search = `^([0-9a-f]{6}),${line},`; const search = `^([0-9a-f]{6}),${line},`;
const colornamesMatches = colornamesRaw.match(new RegExp(search, "img")); if (!first) {
if (colornamesMatches.length > 1) { const colornamesMatches = colornamesRaw.match(new RegExp(search, "img"));
const hexes = colornamesMatches.map((m) => m.split(",")[0]); if (colornamesMatches.length > 1) {
return `Got ${colornamesMatches.length} matches for \`${safeString(line)}\`\n- ${hexes.join("\n- ")}`; const hexes = colornamesMatches.map((m) => m.split(",")[0]);
return `Got ${colornamesMatches.length} matches for \`${safeString(line)}\`\n- ${hexes.join("\n- ")}`;
}
} }
const colornamesHex = colornamesRaw.match(new RegExp(search, "im"))?.[1]; const colornamesHex = colornamesRaw.match(new RegExp(search, "im"))?.[1];
@ -752,11 +754,19 @@ colorInteraction.options.truerandom = {
required: false, required: false,
default: false, default: false,
}; };
colorInteraction.options.first = {
name: "first",
type: ApplicationCommandOptionTypes.BOOLEAN,
description: "Take first match for a colornames color by name",
required: false,
default: false,
};
colorInteraction.callback = async function (interaction) { colorInteraction.callback = async function (interaction) {
const input = this.getOption(interaction, "input"); const input = this.getOption(interaction, "input");
const truerandom = this.getOption(interaction, "truerandom"); const truerandom = this.getOption(interaction, "truerandom");
const first = this.getOption(interaction, "first");
return color.callback(interaction, input, [input], {truerandom}); return color.callback(interaction, input, [input], {truerandom, first});
}; };
hf.registerCommand(colorInteraction); hf.registerCommand(colorInteraction);