mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-16 22:21:51 +00:00
Add !tag eval for testing tag formatting before creating a tag
This commit is contained in:
parent
e61fe722c5
commit
656fb1baf3
1 changed files with 36 additions and 25 deletions
|
@ -137,6 +137,13 @@ export class TagsPlugin extends ZeppelinPlugin<ITagsPluginConfig, ITagsPluginPer
|
||||||
msg.channel.createMessage(successMessage("Tag deleted!"));
|
msg.channel.createMessage(successMessage("Tag deleted!"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@d.command("tag eval", "<body:string$>")
|
||||||
|
@d.permission("create")
|
||||||
|
async evalTagCmd(msg: Message, args: { body: string }) {
|
||||||
|
const rendered = await this.renderTag(args.body);
|
||||||
|
msg.channel.createMessage(rendered);
|
||||||
|
}
|
||||||
|
|
||||||
@d.command("tag", "<tag:string> <body:string$>")
|
@d.command("tag", "<tag:string> <body:string$>")
|
||||||
@d.permission("create")
|
@d.permission("create")
|
||||||
async tagCmd(msg: Message, args: { tag: string; body: string }) {
|
async tagCmd(msg: Message, args: { tag: string; body: string }) {
|
||||||
|
@ -171,6 +178,34 @@ export class TagsPlugin extends ZeppelinPlugin<ITagsPluginConfig, ITagsPluginPer
|
||||||
msg.channel.createMessage(`Tag source:\n${url}`);
|
msg.channel.createMessage(`Tag source:\n${url}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async renderTag(body, args = []) {
|
||||||
|
const dynamicVars = {};
|
||||||
|
const maxTagFnCalls = 25;
|
||||||
|
let tagFnCalls = 0;
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
args,
|
||||||
|
...this.tagFunctions,
|
||||||
|
set(name, val) {
|
||||||
|
if (typeof name !== "string") return;
|
||||||
|
dynamicVars[name] = val;
|
||||||
|
},
|
||||||
|
get(name) {
|
||||||
|
return dynamicVars[name] == null ? "" : dynamicVars[name];
|
||||||
|
},
|
||||||
|
tag: async (name, ...subTagArgs) => {
|
||||||
|
if (tagFnCalls++ > maxTagFnCalls) return "\\_recursion\\_";
|
||||||
|
if (typeof name !== "string") return "";
|
||||||
|
if (name === "") return "";
|
||||||
|
const subTag = await this.tags.find(name);
|
||||||
|
if (!subTag) return "";
|
||||||
|
return renderTemplate(subTag.body, { ...data, args: subTagArgs });
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
return renderTemplate(body, data);
|
||||||
|
}
|
||||||
|
|
||||||
async onMessageCreate(msg: SavedMessage) {
|
async onMessageCreate(msg: SavedMessage) {
|
||||||
const member = this.guild.members.get(msg.user_id);
|
const member = this.guild.members.get(msg.user_id);
|
||||||
if (!this.hasPermission("use", { member, channelId: msg.channel_id })) return;
|
if (!this.hasPermission("use", { member, channelId: msg.channel_id })) return;
|
||||||
|
@ -196,31 +231,7 @@ export class TagsPlugin extends ZeppelinPlugin<ITagsPluginConfig, ITagsPluginPer
|
||||||
|
|
||||||
// Format the string
|
// Format the string
|
||||||
try {
|
try {
|
||||||
const dynamicVars = {};
|
body = await this.renderTag(body, tagArgs);
|
||||||
const maxTagFnCalls = 25;
|
|
||||||
let tagFnCalls = 0;
|
|
||||||
|
|
||||||
const data = {
|
|
||||||
args: tagArgs,
|
|
||||||
...this.tagFunctions,
|
|
||||||
set(name, val) {
|
|
||||||
if (typeof name !== "string") return;
|
|
||||||
dynamicVars[name] = val;
|
|
||||||
},
|
|
||||||
get(name) {
|
|
||||||
return dynamicVars[name] == null ? "" : dynamicVars[name];
|
|
||||||
},
|
|
||||||
tag: async (name, ...args) => {
|
|
||||||
if (tagFnCalls++ > maxTagFnCalls) return "\\_recursion\\_";
|
|
||||||
if (typeof name !== "string") return "";
|
|
||||||
if (name === "") return "";
|
|
||||||
const subTag = await this.tags.find(name);
|
|
||||||
if (!subTag) return "";
|
|
||||||
return renderTemplate(subTag.body, { ...data, args });
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
body = await renderTemplate(body, data);
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof TemplateParseError) {
|
if (e instanceof TemplateParseError) {
|
||||||
logger.warn(`Invalid tag format!\nError: ${e.message}\nFormat: ${tag.body}`);
|
logger.warn(`Invalid tag format!\nError: ${e.message}\nFormat: ${tag.body}`);
|
||||||
|
|
Loading…
Add table
Reference in a new issue