Tags: switch from %arg syntax to regular template variable format with indexes, e.g. {args.0}

This commit is contained in:
Dragory 2019-03-16 16:18:28 +02:00
parent 11558df665
commit d914e8a717

View file

@ -7,6 +7,7 @@ import { SavedMessage } from "../data/entities/SavedMessage";
import moment from "moment-timezone"; import moment from "moment-timezone";
import humanizeDuration from "humanize-duration"; import humanizeDuration from "humanize-duration";
import { ZeppelinPlugin } from "./ZeppelinPlugin"; import { ZeppelinPlugin } from "./ZeppelinPlugin";
import { renderTemplate } from "../templateFormatter";
const TAG_FUNCTIONS = { const TAG_FUNCTIONS = {
countdown(toDate) { countdown(toDate) {
@ -140,20 +141,12 @@ export class TagsPlugin extends ZeppelinPlugin<ITagsPluginConfig, ITagsPluginPer
// Substitute variables (matched with Knub's argument parser -> supports quotes etc.) // Substitute variables (matched with Knub's argument parser -> supports quotes etc.)
const variableStr = msg.data.content.slice(prefix.length + tagName.length).trim(); const variableStr = msg.data.content.slice(prefix.length + tagName.length).trim();
const variableValues = this.commands.parseArguments(variableStr).map(v => v.value); const tagArgs = this.commands.parseArguments(variableStr).map(v => v.value);
let variableIndex = 0;
body = body.replace(/(?<!\\)%[a-zA-Z]+/g, () => variableValues[variableIndex++] || "");
// Run functions // Format the string
body = body.replace(/(?<!\\)\{([a-zA-Z]+)(?::?(.*?))?\}/, (_, fn, args) => { body = await renderTemplate(body, {
if (!TAG_FUNCTIONS[fn]) return ""; args: tagArgs,
const fnArgs = args ? args.split(/(?<!\\):/) : []; ...TAG_FUNCTIONS,
try {
return TAG_FUNCTIONS[fn].apply(null, fnArgs);
} catch (e) {
return "";
}
}); });
const channel = this.guild.channels.get(msg.channel_id) as TextChannel; const channel = this.guild.channels.get(msg.channel_id) as TextChannel;