Merge pull request #348 from DenverCoderOne/tag-list-search

feat: Tag list search and improved readability
This commit is contained in:
Miikka 2022-08-14 00:12:43 +03:00 committed by GitHub
commit e9fe0e03b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,3 +1,4 @@
import { commandTypeHelpers as ct } from "../../../commandTypes";
import { createChunkedMessage } from "../../../utils";
import { tagsCmd } from "../types";
@ -5,7 +6,11 @@ export const TagListCmd = tagsCmd({
trigger: ["tag list", "tags", "taglist"],
permission: "can_list",
async run({ message: msg, pluginData }) {
signature: {
search: ct.string({ required: false }),
},
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.`);
@ -15,6 +20,33 @@ export const TagListCmd = tagsCmd({
const prefix = (await pluginData.config.getForMessage(msg)).prefix;
const tagNames = tags.map((tag) => tag.tag).sort();
createChunkedMessage(msg.channel, `Available tags (use with ${prefix}tag): \`\`\`${tagNames.join(", ")}\`\`\``);
const filteredTags = args.search
? tagNames.filter((tag) =>
new RegExp(
args.search
.split("")
.map((char) => char.replace(/[.*+?^${}()|[\]\\]/, "\\$&"))
.join(".*")
).test(tag)
)
: tagNames;
const tagGroups = filteredTags.reduce((acc, tag) => {
const obj = { ...acc };
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");
createChunkedMessage(msg.channel, `Available tags (use with ${prefix}tag): \`\`\`${tagList}\`\`\``);
},
});