From 1c0b41597c915c590301f8740494db2fd13cc668 Mon Sep 17 00:00:00 2001 From: Ruby Date: Fri, 25 Oct 2024 17:13:58 +0200 Subject: [PATCH] feat: include categorized tags in the tag list command --- .../src/plugins/Tags/commands/TagListCmd.ts | 65 +++++++++++-------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/backend/src/plugins/Tags/commands/TagListCmd.ts b/backend/src/plugins/Tags/commands/TagListCmd.ts index 02132f95a..57606f617 100644 --- a/backend/src/plugins/Tags/commands/TagListCmd.ts +++ b/backend/src/plugins/Tags/commands/TagListCmd.ts @@ -12,40 +12,53 @@ export const TagListCmd = tagsCmd({ }, async run({ message: msg, args, pluginData }) { - const tags = await pluginData.state.tags.all(); - if (tags.length === 0) { - msg.channel.send(`No tags created yet! Use \`tag create\` command to create one.`); - return; - } + const config = await pluginData.config.getForMessage(msg); - const prefix = (await pluginData.config.getForMessage(msg)).prefix; - const tagNames = tags.map((tag) => tag.tag).sort(); const searchRegex = args.search ? new RegExp([...args.search].map((s) => escapeStringRegexp(s)).join(".*"), "i") : null; - const filteredTags = args.search ? tagNames.filter((tag) => searchRegex!.test(tag)) : tagNames; - - if (filteredTags.length === 0) { - msg.channel.send("No tags matched the filter"); - return; - } + const messageBody = (prefix = config.prefix, tagNames: string[], category?): string => { + if (tagNames.length == 0) { + return ""; + } - const tagGroups = filteredTags.reduce((obj, tag) => { - const tagUpper = tag.toUpperCase(); - const key = /[A-Z]/.test(tagUpper[0]) ? tagUpper[0] : "#"; - if (!(key in obj)) { - obj[key] = []; + const filteredTags = args.search ? tagNames.filter((tag) => searchRegex!.test(tag)) : tagNames; + if (filteredTags.length === 0) { + return ""; } - obj[key].push(tag); - return obj; - }, {}); - const tagList = Object.keys(tagGroups) - .sort() - .map((key) => `[${key}] ${tagGroups[key].join(", ")}`) - .join("\n"); + const tagGroups = filteredTags.reduce((obj, tag) => { + const tagUpper = tag.toUpperCase(); + const key = /[A-Z]/.test(tagUpper[0]) ? tagUpper[0] : "#"; + if (!(key in obj)) { + obj[key] = []; + } + obj[key].push(tag); + return obj; + }, {}); + + const tagList = Object.keys(tagGroups) + .sort() + .map((key) => `[${key}] ${tagGroups[key].join(", ")}`) + .join("\n"); + + return `Available ${ + category ? `\`${category}\`` : "uncategorized" + } tags (use with ${prefix}tag): \`\`\`${tagList}\`\`\``; + }; + + const dynamicTags = (await pluginData.state.tags.all()).map((tag) => tag.tag).sort(); + const dynamicMessageBody = messageBody(config.prefix, dynamicTags); + + const messageBodies = [dynamicMessageBody]; + + const tagCategories = Object.entries(config.categories); + for (const [category, value] of tagCategories) { + const tags = Object.keys(value.tags); + messageBodies.push(messageBody(value.prefix ?? config.prefix, tags, category)); + } - createChunkedMessage(msg.channel, `Available tags (use with ${prefix}tag): \`\`\`${tagList}\`\`\``); + createChunkedMessage(msg.channel, messageBodies.filter((str) => str.length > 0).join("\n")); }, });