3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-14 21:31:50 +00:00

tags: support hardcoded tags with tag() tag function

This commit is contained in:
Dragory 2020-09-15 01:54:34 +03:00
parent f82cbb43e9
commit 99438441cb
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
4 changed files with 44 additions and 6 deletions

View file

@ -15,7 +15,7 @@ export const TagEvalCmd = tagsCmd({
async run({ message: msg, args, pluginData }) {
try {
const rendered = await renderTagBody(pluginData, args.body);
const rendered = await renderTagBody(pluginData, args.body, [], {}, { member: msg.member });
msg.channel.createMessage(rendered);
} catch (e) {
if (e instanceof TemplateParseError) {

View file

@ -0,0 +1,27 @@
import { PluginData } from "knub";
import { Tag, TagsPluginType } from "../types";
import { ExtendedMatchParams } from "knub/dist/config/PluginConfigManager";
import * as t from "io-ts";
export async function findTagByName(
pluginData: PluginData<TagsPluginType>,
name: string,
matchParams: ExtendedMatchParams = {},
): Promise<t.TypeOf<typeof Tag> | null> {
const config = pluginData.config.getMatchingConfig(matchParams);
// Tag from a hardcoded category
// Format: "category.tag"
const categorySeparatorIndex = name.indexOf(".");
if (categorySeparatorIndex > 0) {
const categoryName = name.slice(0, categorySeparatorIndex);
const tagName = name.slice(categorySeparatorIndex + 1);
return config.categories[categoryName]?.tags[tagName] ?? null;
}
// Dynamic tag
// Format: "tag"
const dynamicTag = await pluginData.state.tags.find(name);
return dynamicTag?.body ?? null;
}

View file

@ -3,12 +3,15 @@ import { PluginData, plugin } from "knub";
import { Tag, TagsPluginType } from "../types";
import { renderRecursively, StrictMessageContent } from "../../../utils";
import * as t from "io-ts";
import { findTagByName } from "./findTagByName";
import { ExtendedMatchParams } from "knub/dist/config/PluginConfigManager";
export async function renderTagBody(
pluginData: PluginData<TagsPluginType>,
body: t.TypeOf<typeof Tag>,
args = [],
extraData = {},
subTagPermissionMatchParams?: ExtendedMatchParams,
): Promise<StrictMessageContent> {
const dynamicVars = {};
const maxTagFnCalls = 25;
@ -30,10 +33,18 @@ export async function renderTagBody(
if (typeof name !== "string") return "";
if (name === "") return "";
// TODO: Incorporate tag categories here
const subTag = await pluginData.state.tags.find(name);
if (!subTag) return "";
return renderTemplate(subTag.body, { ...data, args: subTagArgs });
const subTagBody = await findTagByName(pluginData, name, subTagPermissionMatchParams);
if (!subTagBody) {
return "";
}
if (typeof subTagBody !== "string") {
return "<embed>";
}
const rendered = await renderTagBody(pluginData, subTagBody, subTagArgs, subTagPermissionMatchParams);
return rendered.content!;
},
};

View file

@ -23,7 +23,7 @@ export async function renderTagFromString(
// Format the string
try {
return renderTagBody(pluginData, tagBody, tagArgs);
return renderTagBody(pluginData, tagBody, tagArgs, {}, { member });
} catch (e) {
if (e instanceof TemplateParseError) {
const logs = pluginData.getPlugin(LogsPlugin);