Logs: add permission for whether a user is pinged in logs or not (mainly to not ping mods); templateFormatter: fix error when following a plain value variable with a function variable

This commit is contained in:
Dragory 2019-03-16 16:10:30 +02:00
parent d5a902bcdd
commit 11558df665
5 changed files with 75 additions and 44 deletions

View file

@ -1,52 +1,52 @@
{
"MEMBER_WARN": "⚠️ <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was warned by {mod.username}#{mod.discriminator}",
"MEMBER_MUTE": "🔇 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was muted indefinitely by {mod.username}#{mod.discriminator}",
"MEMBER_TIMED_MUTE": "🔇 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was muted for **{time}** by {mod.username}#{mod.discriminator}",
"MEMBER_UNMUTE": "🔊 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was unmuted by {mod.username}#{mod.discriminator}",
"MEMBER_TIMED_UNMUTE": "🔊 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was scheduled to be unmuted in **{time}** by {mod.username}#{mod.discriminator}",
"MEMBER_MUTE_EXPIRED": "🔊 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`)'s mute expired",
"MEMBER_KICK": "👢 **{user.username}#{user.discriminator}** (`{user.id}`) was kicked by {mod.username}#{mod.discriminator}",
"MEMBER_BAN": "🔨 **{user.username}#{user.discriminator}** (`{user.id}`) was banned by {mod.username}#{mod.discriminator}",
"MEMBER_UNBAN": "🔓 User (`{userId}`) was unbanned by {mod.username}#{mod.discriminator}",
"MEMBER_FORCEBAN": "🔨 User (`{userId}`) was forcebanned by {mod.username}#{mod.discriminator}",
"MEMBER_SOFTBAN": "🔨 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was softbanned by {mod.username}#{mod.discriminator}",
"MEMBER_JOIN": "📥 **{member.user.username}#{member.user.discriminator}** (`{member.id}`, <@!{member.id}>) joined{new} (created {account_age} ago)",
"MEMBER_LEAVE": "📤 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) left the server",
"MEMBER_ROLE_ADD": "🔑 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`): role(s) **{roles}** added by {mod.username}#{mod.discriminator}",
"MEMBER_ROLE_REMOVE": "🔑 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`): role(s) **{roles}** removed by {mod.username}#{mod.discriminator}",
"MEMBER_ROLE_CHANGES": "🔑 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`): roles changed: added **{addedRoles}**, removed **{removedRoles}** by {mod.username}#{mod.discriminator}",
"MEMBER_NICK_CHANGE": "✏ <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`): nickname changed from **{oldNick}** to **{newNick}**",
"MEMBER_USERNAME_CHANGE": "✏ <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`): username changed from **{oldName}** to **{newName}**",
"MEMBER_RESTORE": "💿 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was restored",
"MEMBER_WARN": "⚠️ {userMention(member)} was warned by {userMention(mod)}",
"MEMBER_MUTE": "🔇 {userMention(member)} was muted indefinitely by {userMention(mod)}",
"MEMBER_TIMED_MUTE": "🔇 {userMention(member)} was muted for **{time}** by {userMention(mod)}",
"MEMBER_UNMUTE": "🔊 {userMention(member)} was unmuted by {userMention(mod)}",
"MEMBER_TIMED_UNMUTE": "🔊 {userMention(member)} was scheduled to be unmuted in **{time}** by {userMention(mod)}",
"MEMBER_MUTE_EXPIRED": "🔊 {userMention(member)}'s mute expired",
"MEMBER_KICK": "👢 {userMention(member)} was kicked by {userMention(mod)}",
"MEMBER_BAN": "🔨 {userMention(member)} was banned by {userMention(mod)}",
"MEMBER_UNBAN": "🔓 User (`{userId}`) was unbanned by {userMention(mod)}",
"MEMBER_FORCEBAN": "🔨 User (`{userId}`) was forcebanned by {userMention(mod)}",
"MEMBER_SOFTBAN": "🔨 {userMention(member)} was softbanned by {userMention(mod)}",
"MEMBER_JOIN": "📥 {userMention(member)} joined{new} (created {account_age} ago)",
"MEMBER_LEAVE": "📤 {userMention(member)} left the server",
"MEMBER_ROLE_ADD": "🔑 {userMention(member)}: role(s) **{roles}** added by {userMention(mod)}",
"MEMBER_ROLE_REMOVE": "🔑 {userMention(member)}: role(s) **{roles}** removed by {userMention(mod)}",
"MEMBER_ROLE_CHANGES": "🔑 {userMention(member)}: roles changed: added **{addedRoles}**, removed **{removedRoles}** by {userMention(mod)}",
"MEMBER_NICK_CHANGE": "✏ {userMention(member)}: nickname changed from **{oldNick}** to **{newNick}**",
"MEMBER_USERNAME_CHANGE": "✏ {userMention(member)}: username changed from **{oldName}** to **{newName}**",
"MEMBER_RESTORE": "💿 {userMention(member)} was restored",
"CHANNEL_CREATE": "🖊 Channel <#{channel.id}> (**#{channel.name}**, `{channel.id}`) was created",
"CHANNEL_DELETE": "🗑 Channel <#{channel.id}> (**#{channel.name}**, `{channel.id}`) was deleted",
"CHANNEL_EDIT": "✏ Channel <#{channel.id}> (**#{channel.name}**, `{channel.id}`) was edited",
"CHANNEL_CREATE": "🖊 Channel {channelMention(channel)} was created",
"CHANNEL_DELETE": "🗑 Channel {channelMention(channel)} was deleted",
"CHANNEL_EDIT": "✏ Channel {channelMention(channel)} was edited",
"ROLE_CREATE": "🖊 Role **{role.name}** (`{role.id}`) was created",
"ROLE_DELETE": "🖊 Role **{role.name}** (`{role.id}`) was deleted",
"ROLE_EDIT": "🖊 Role **{role.name}** (`{role.id}`) was edited",
"MESSAGE_EDIT": "✏ <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) edited their message in <#{channel.id}> (**#{channel.name}**, `{channel.id}`):\nBefore:```{before}```After:```{after}```",
"MESSAGE_DELETE": "🗑 Message from <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) deleted in <#{channel.id}> (**#{channel.name}**, `{channel.id}`) (originally posted at **{messageDate}**):\n```{messageText}```{attachments}",
"MESSAGE_DELETE_BULK": "🗑 **{count}** messages deleted in <#{channel.id}> (**#{channel.name}**, `{channel.id}`) ({archiveUrl})",
"MESSAGE_DELETE_BARE": "🗑 Message (`{messageId}`) deleted in <#{channel.id}> (**#{channel.name}**, `{channel.id}`) (no more info available)",
"MESSAGE_EDIT": "✏ {userMention(member)} edited their message in {channelMention(channel)}:\nBefore:```{before}```After:```{after}```",
"MESSAGE_DELETE": "🗑 Message from {userMention(member)} deleted in {channelMention(channel)} (originally posted at **{messageDate}**):\n```{messageText}```{attachments}",
"MESSAGE_DELETE_BULK": "🗑 **{count}** messages deleted in {channelMention(channel)} ({archiveUrl})",
"MESSAGE_DELETE_BARE": "🗑 Message (`{messageId}`) deleted in {channelMention(channel)} (no more info available)",
"VOICE_CHANNEL_JOIN": "🎙 🔵 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) (`{member.id}`) joined **{channel.name}**",
"VOICE_CHANNEL_MOVE": "🎙 ↔ <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) (`{member.id}`) moved from **{oldChannel.name}** to **{newChannel.name}**",
"VOICE_CHANNEL_LEAVE": "🎙 🔴 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) (`{member.id}`) left **{channel.name}**",
"VOICE_CHANNEL_FORCE_MOVE": "\uD83C\uDF99 ✍ <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) was moved from **{oldChannel.name}** to **{newChannel.name}** by {mod.username}#{mod.discriminator}",
"VOICE_CHANNEL_JOIN": "🎙 🔵 {userMention(member)} (`{member.id}`) joined **{channel.name}**",
"VOICE_CHANNEL_MOVE": "🎙 ↔ {userMention(member)} (`{member.id}`) moved from **{oldChannel.name}** to **{newChannel.name}**",
"VOICE_CHANNEL_LEAVE": "🎙 🔴 {userMention(member)} (`{member.id}`) left **{channel.name}**",
"VOICE_CHANNEL_FORCE_MOVE": "\uD83C\uDF99 ✍ {userMention(member)} was moved from **{oldChannel.name}** to **{newChannel.name}** by {userMention(mod)}",
"COMMAND": "🤖 **{member.user.username}#{member.user.discriminator}** (`{member.id}`) used command in <#{channel.id}> (**#{channel.name}**, `{channel.id}`):\n`{command}`",
"COMMAND": "🤖 {userMention(member)} used command in {channelMention(channel)}:\n`{command}`",
"MESSAGE_SPAM_DETECTED": "🛑 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) spam detected in <#{channel.id}> (**#{channel.name}**, `{channel.id}`): {description} (more than {limit} in {interval}s)\n{archiveUrl}",
"OTHER_SPAM_DETECTED": "🛑 <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) spam detected: {description} (more than {limit} in {interval}s)",
"CENSOR": "🛑 Censored message from <@!{member.id}> (**{member.user.username}#{member.user.discriminator}**, `{member.id}`) in <#{channel.id}> (**#{channel.name}**, `{channel.id}`): {reason}:\n```{messageText}```",
"CLEAN": "🚿 **{mod.username}#{mod.discriminator}** (`{mod.id}`) cleaned **{count}** message(s) in <#{channel.id}> (**#{channel.name}**, `{channel.id}`)\n{archiveUrl}",
"MESSAGE_SPAM_DETECTED": "🛑 {userMention(member)} spam detected in {channelMention(channel)}: {description} (more than {limit} in {interval}s)\n{archiveUrl}",
"OTHER_SPAM_DETECTED": "🛑 {userMention(member)} spam detected: {description} (more than {limit} in {interval}s)",
"CENSOR": "🛑 Censored message from {userMention(member)} in {channelMention(channel)}: {reason}:\n```{messageText}```",
"CLEAN": "🚿 {userMention(mod)} cleaned **{count}** message(s) in {channelMention(channel)}\n{archiveUrl}",
"CASE_CREATE": "✏ **{mod.username}#{mod.discriminator}** (`{mod.id}`) manually created new **{caseType}** case (#{caseNum})",
"CASE_CREATE": "✏ {userMention(mod)} manually created new **{caseType}** case (#{caseNum})",
"MASSBAN": "⚒ **{mod.username}#{mod.discriminator}** massbanned {count} users",
"MASSBAN": "⚒ {userMention(mod)} massbanned {count} users",
"MEMBER_JOIN_WITH_PRIOR_RECORDS": "⚠ **{member.user.username}#{member.user.discriminator}** (`{member.id}`) joined with prior records. Recent cases:\n{recentCaseSummary}"
"MEMBER_JOIN_WITH_PRIOR_RECORDS": "⚠ {userMention(member)} joined with prior records. Recent cases:\n{recentCaseSummary}"
}

View file

@ -1,4 +1,4 @@
import { decorators as d, IPluginOptions, Plugin } from "knub";
import { decorators as d, IPluginOptions, logger, Plugin } from "knub";
import { GuildLogs } from "../data/GuildLogs";
import { LogType } from "../data/LogType";
import { Channel, Constants as ErisConstants, Member, Message, TextChannel, User } from "eris";
@ -22,7 +22,7 @@ import { SavedMessage } from "../data/entities/SavedMessage";
import { GuildArchives } from "../data/GuildArchives";
import { GuildCases } from "../data/GuildCases";
import { ZeppelinPlugin } from "./ZeppelinPlugin";
import { renderTemplate } from "../templateFormatter";
import { renderTemplate, TemplateParseError } from "../templateFormatter";
interface ILogChannel {
include?: string[];
@ -169,7 +169,34 @@ export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig, ILogsPluginPer
const format = config.format[LogType[type]] || "";
if (format === "") return;
const formatted = await renderTemplate(format, data);
let formatted;
try {
formatted = await renderTemplate(format, {
...data,
userMention: user => {
if (user.user) user = user.user;
const member = this.guild.members.get(user.id);
if (this.hasPermission("pinged", { member, userId: user.id })) {
// Ping/mention the user
return `<@!${user.id}> (**${user.username}#${user.discriminator}**, \`${user.id}\`)`;
} else {
// No ping/mention
return `**${user.username}#${user.discriminator}** (\`${user.id}\`)`;
}
},
channelMention: channel => {
return `<#${channel.id}> (**#${channel.name}**, \`${channel.id}\`)`;
},
});
} catch (e) {
if (e instanceof TemplateParseError) {
logger.error(`Error when parsing template:\nError: ${e.message}\nTemplate: ${format}`);
return;
} else {
throw e;
}
}
const timestampFormat = config.format.timestamp;
if (timestampFormat) {

View file

@ -726,7 +726,7 @@ export class ModActionsPlugin extends ZeppelinPlugin<IModActionsPluginConfig, IM
// Log the action
this.serverLogs.log(LogType.MEMBER_KICK, {
mod: stripObjectToScalars(mod.user),
user: stripObjectToScalars(args.member.user),
member: stripObjectToScalars(args.member, ["user"]),
});
}

View file

@ -94,3 +94,8 @@ test("Supports base values in renderTemplate", async () => {
const result = await renderTemplate('{if("", "+", "-")} {if(1, "+", "-")}');
expect(result).toBe("- +");
});
test("Edge case #1", async () => {
const result = await renderTemplate("{foo} {bar()}");
// No "Unclosed function" exception = success
});

View file

@ -3,7 +3,7 @@ import at from "lodash.at";
const TEMPLATE_CACHE_SIZE = 100;
const templateCache: Map<string, ParsedTemplate> = new Map();
class TemplateParseError extends Error {}
export class TemplateParseError extends Error {}
interface ITemplateVar {
identifier: string;
@ -196,7 +196,6 @@ export function parseTemplate(str: string): ParsedTemplate {
}
const newVar = newTemplateVar();
if (currentVar) newVar._parent = currentVar;
currentVar = newVar;
rootVar = newVar;
inVar = true;