mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
tags: support hardcoded tags with tag() tag function
This commit is contained in:
parent
f82cbb43e9
commit
99438441cb
4 changed files with 44 additions and 6 deletions
|
@ -15,7 +15,7 @@ export const TagEvalCmd = tagsCmd({
|
||||||
|
|
||||||
async run({ message: msg, args, pluginData }) {
|
async run({ message: msg, args, pluginData }) {
|
||||||
try {
|
try {
|
||||||
const rendered = await renderTagBody(pluginData, args.body);
|
const rendered = await renderTagBody(pluginData, args.body, [], {}, { member: msg.member });
|
||||||
msg.channel.createMessage(rendered);
|
msg.channel.createMessage(rendered);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof TemplateParseError) {
|
if (e instanceof TemplateParseError) {
|
||||||
|
|
27
backend/src/plugins/Tags/util/findTagByName.ts
Normal file
27
backend/src/plugins/Tags/util/findTagByName.ts
Normal 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;
|
||||||
|
}
|
|
@ -3,12 +3,15 @@ import { PluginData, plugin } from "knub";
|
||||||
import { Tag, TagsPluginType } from "../types";
|
import { Tag, TagsPluginType } from "../types";
|
||||||
import { renderRecursively, StrictMessageContent } from "../../../utils";
|
import { renderRecursively, StrictMessageContent } from "../../../utils";
|
||||||
import * as t from "io-ts";
|
import * as t from "io-ts";
|
||||||
|
import { findTagByName } from "./findTagByName";
|
||||||
|
import { ExtendedMatchParams } from "knub/dist/config/PluginConfigManager";
|
||||||
|
|
||||||
export async function renderTagBody(
|
export async function renderTagBody(
|
||||||
pluginData: PluginData<TagsPluginType>,
|
pluginData: PluginData<TagsPluginType>,
|
||||||
body: t.TypeOf<typeof Tag>,
|
body: t.TypeOf<typeof Tag>,
|
||||||
args = [],
|
args = [],
|
||||||
extraData = {},
|
extraData = {},
|
||||||
|
subTagPermissionMatchParams?: ExtendedMatchParams,
|
||||||
): Promise<StrictMessageContent> {
|
): Promise<StrictMessageContent> {
|
||||||
const dynamicVars = {};
|
const dynamicVars = {};
|
||||||
const maxTagFnCalls = 25;
|
const maxTagFnCalls = 25;
|
||||||
|
@ -30,10 +33,18 @@ export async function renderTagBody(
|
||||||
if (typeof name !== "string") return "";
|
if (typeof name !== "string") return "";
|
||||||
if (name === "") return "";
|
if (name === "") return "";
|
||||||
|
|
||||||
// TODO: Incorporate tag categories here
|
const subTagBody = await findTagByName(pluginData, name, subTagPermissionMatchParams);
|
||||||
const subTag = await pluginData.state.tags.find(name);
|
|
||||||
if (!subTag) return "";
|
if (!subTagBody) {
|
||||||
return renderTemplate(subTag.body, { ...data, args: subTagArgs });
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (typeof subTagBody !== "string") {
|
||||||
|
return "<embed>";
|
||||||
|
}
|
||||||
|
|
||||||
|
const rendered = await renderTagBody(pluginData, subTagBody, subTagArgs, subTagPermissionMatchParams);
|
||||||
|
return rendered.content!;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ export async function renderTagFromString(
|
||||||
|
|
||||||
// Format the string
|
// Format the string
|
||||||
try {
|
try {
|
||||||
return renderTagBody(pluginData, tagBody, tagArgs);
|
return renderTagBody(pluginData, tagBody, tagArgs, {}, { member });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof TemplateParseError) {
|
if (e instanceof TemplateParseError) {
|
||||||
const logs = pluginData.getPlugin(LogsPlugin);
|
const logs = pluginData.getPlugin(LogsPlugin);
|
||||||
|
|
Loading…
Add table
Reference in a new issue