diff --git a/src/plugins/Tags.ts b/src/plugins/Tags.ts index 9f5b6340..71dc9511 100644 --- a/src/plugins/Tags.ts +++ b/src/plugins/Tags.ts @@ -1,4 +1,4 @@ -import { Plugin, decorators as d, IPluginOptions } from "knub"; +import { decorators as d, IPluginOptions } from "knub"; import { Message, TextChannel } from "eris"; import { errorMessage, successMessage } from "../utils"; import { GuildTags } from "../data/GuildTags"; @@ -7,20 +7,9 @@ import { SavedMessage } from "../data/entities/SavedMessage"; import moment from "moment-timezone"; import humanizeDuration from "humanize-duration"; import { ZeppelinPlugin } from "./ZeppelinPlugin"; -import { renderTemplate } from "../templateFormatter"; -import { escapeBacktickString } from "jest-snapshot/build/utils"; +import { parseTemplate, renderTemplate, TemplateParseError } from "../templateFormatter"; import { GuildArchives } from "../data/GuildArchives"; -const TAG_FUNCTIONS = { - countdown(toDate) { - const now = moment(); - const target = moment(toDate, "YYYY-MM-DD HH:mm:ss"); - const diff = target.diff(now); - const result = humanizeDuration(diff, { largest: 2, round: true }); - return diff >= 0 ? result : `${result} ago`; - }, -}; - interface ITagsPluginConfig { prefix: string; delete_with_command: boolean; @@ -42,6 +31,8 @@ export class TagsPlugin extends ZeppelinPlugin { return { config: { @@ -77,6 +68,37 @@ export class TagsPlugin extends ZeppelinPlugin= 0 ? result : `${result} ago`; + }, + + mention: input => { + if (typeof input !== "string") return ""; + if (input.match(/^<(@#)(!&)\d+>$/)) { + return input; + } + + if (this.guild.members.has(input) || this.bot.users.has(input)) { + return `<@!${input}>`; + } + + if (this.guild.channels.has(input) || this.bot.channelGuildMap[input]) { + return `<#${input}>`; + } + + return input; + }, + }; } onUnload() { @@ -118,6 +140,17 @@ export class TagsPlugin extends ZeppelinPlugin ") @d.permission("create") async tagCmd(msg: Message, args: { tag: string; body: string }) { + try { + parseTemplate(args.body); + } catch (e) { + if (e instanceof TemplateParseError) { + msg.channel.createMessage(errorMessage(`Invalid tag syntax: ${e.message}`)); + return; + } else { + throw e; + } + } + await this.tags.createOrUpdate(args.tag, args.body, msg.author.id); const prefix = this.getConfig().prefix; @@ -164,7 +197,7 @@ export class TagsPlugin extends ZeppelinPlugin