Add templateFormatter; migrate from formatTemplateString to templateFormatter

This commit is contained in:
Dragory 2019-03-16 15:42:55 +02:00
parent 5a29068bf1
commit ba3af1cb63
6 changed files with 435 additions and 32 deletions

View file

@ -8,7 +8,6 @@ import {
disableCodeBlocks,
disableLinkPreviews,
findRelevantAuditLogEntry,
formatTemplateString,
noop,
stripObjectToScalars,
useMediaUrls,
@ -23,6 +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";
interface ILogChannel {
include?: string[];
@ -58,7 +58,11 @@ interface ILogsPluginConfig {
};
}
export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig> {
interface ILogsPluginPermissions {
pinged: boolean;
}
export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig, ILogsPluginPermissions> {
public static pluginName = "logs";
protected guildLogs: GuildLogs;
@ -74,7 +78,7 @@ export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig> {
private onMessageDeleteBulkFn;
private onMessageUpdateFn;
getDefaultOptions(): IPluginOptions<ILogsPluginConfig> {
getDefaultOptions(): IPluginOptions<ILogsPluginConfig, ILogsPluginPermissions> {
return {
config: {
channels: {},
@ -84,7 +88,18 @@ export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig> {
},
},
permissions: {},
permissions: {
pinged: true,
},
overrides: [
{
level: ">=50",
permissions: {
pinged: false,
},
},
],
};
}
@ -126,7 +141,7 @@ export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig> {
if (!channel || !(channel instanceof TextChannel)) continue;
if ((opts.include && opts.include.includes(typeStr)) || (opts.exclude && !opts.exclude.includes(typeStr))) {
const message = this.getLogMessage(type, data);
const message = await this.getLogMessage(type, data);
if (message) {
if (opts.batched) {
// If we're batching log messages, gather all log messages within the set batch_time into a single message
@ -149,12 +164,12 @@ export class LogsPlugin extends ZeppelinPlugin<ILogsPluginConfig> {
}
}
getLogMessage(type, data): string {
async getLogMessage(type, data): Promise<string> {
const config = this.getConfig();
const format = config.format[LogType[type]] || "";
if (format === "") return;
const formatted = formatTemplateString(format, data);
const formatted = await renderTemplate(format, data);
const timestampFormat = config.format.timestamp;
if (timestampFormat) {

View file

@ -7,7 +7,6 @@ import {
createChunkedMessage,
errorMessage,
findRelevantAuditLogEntry,
formatTemplateString,
asSingleLine,
stripObjectToScalars,
successMessage,
@ -17,11 +16,11 @@ import { GuildMutes } from "../data/GuildMutes";
import { CaseTypes } from "../data/CaseTypes";
import { GuildLogs } from "../data/GuildLogs";
import { LogType } from "../data/LogType";
import Timer = NodeJS.Timer;
import { ZeppelinPlugin } from "./ZeppelinPlugin";
import { GuildActions } from "../data/GuildActions";
import { Case } from "../data/entities/Case";
import { Mute } from "../data/entities/Mute";
import { renderTemplate } from "../templateFormatter";
enum IgnoredEventType {
Ban = 1,
@ -510,7 +509,7 @@ export class ModActionsPlugin extends ZeppelinPlugin<IModActionsPluginConfig, IM
if (reason && !hasOldCase) {
const template = muteTime ? config.timed_mute_message : config.mute_message;
const muteMessage = formatTemplateString(template, {
const muteMessage = await renderTemplate(template, {
guildName: this.guild.name,
reason,
time: timeUntilUnmute,
@ -689,7 +688,7 @@ export class ModActionsPlugin extends ZeppelinPlugin<IModActionsPluginConfig, IM
// Attempt to message the user *before* kicking them, as doing it after may not be possible
let userMessageResult: IMessageResult = { status: MessageResultStatus.Ignored };
if (args.reason) {
const kickMessage = formatTemplateString(config.kick_message, {
const kickMessage = await renderTemplate(config.kick_message, {
guildName: this.guild.name,
reason,
});
@ -759,7 +758,7 @@ export class ModActionsPlugin extends ZeppelinPlugin<IModActionsPluginConfig, IM
// Attempt to message the user *before* banning them, as doing it after may not be possible
let userMessageResult: IMessageResult = { status: MessageResultStatus.Ignored };
if (reason) {
const banMessage = formatTemplateString(config.ban_message, {
const banMessage = await renderTemplate(config.ban_message, {
guildName: this.guild.name,
reason,
});