Skip to content

Commit

Permalink
feat: include categorized tags in the tag list command
Browse files Browse the repository at this point in the history
  • Loading branch information
rubyowo committed Oct 25, 2024
1 parent eb5fda8 commit 1c0b415
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions backend/src/plugins/Tags/commands/TagListCmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
},
});

0 comments on commit 1c0b415

Please sign in to comment.