From c7c7cfa75cc8e95726f2d9ce53a8cf6d3ba26d59 Mon Sep 17 00:00:00 2001 From: Cynthia Foxwell Date: Wed, 31 Jul 2024 22:03:43 -0600 Subject: [PATCH] misc.color: add option to take first colornames color --- src/modules/misc.js | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/modules/misc.js b/src/modules/misc.js index e55554f..f4b08d3 100644 --- a/src/modules/misc.js +++ b/src/modules/misc.js @@ -635,7 +635,7 @@ const colornamesRaw = fs.readFileSync(resolve(__dirname, "../../data/colornames. const color = new Command("color"); color.category = CATEGORY; 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), random = false; @@ -646,10 +646,12 @@ color.callback = async function (msg, line, args, {truerandom}) { if (!color.isValid) { const search = `^([0-9a-f]{6}),${line},`; - const colornamesMatches = colornamesRaw.match(new RegExp(search, "img")); - if (colornamesMatches.length > 1) { - const hexes = colornamesMatches.map((m) => m.split(",")[0]); - return `Got ${colornamesMatches.length} matches for \`${safeString(line)}\`\n- ${hexes.join("\n- ")}`; + if (!first) { + const colornamesMatches = colornamesRaw.match(new RegExp(search, "img")); + if (colornamesMatches.length > 1) { + 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]; @@ -752,11 +754,19 @@ colorInteraction.options.truerandom = { required: 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) { const input = this.getOption(interaction, "input"); 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);