3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

logs: add support for embeds in log formats

This commit is contained in:
Dragory 2020-07-30 02:21:31 +03:00
parent b6b4154b2d
commit 8b22ce267d
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
3 changed files with 75 additions and 54 deletions

View file

@ -5,6 +5,7 @@ import { GuildLogs } from "src/data/GuildLogs";
import { GuildSavedMessages } from "src/data/GuildSavedMessages";
import { GuildArchives } from "src/data/GuildArchives";
import { GuildCases } from "src/data/GuildCases";
import { tMessageContent } from "../../utils";
const LogChannel = t.partial({
include: t.array(t.string),
@ -20,14 +21,16 @@ export type TLogChannel = t.TypeOf<typeof LogChannel>;
const LogChannelMap = t.record(t.string, LogChannel);
export type TLogChannelMap = t.TypeOf<typeof LogChannelMap>;
export const ConfigSchema = t.type({
channels: LogChannelMap,
format: t.intersection([
t.record(t.string, t.string),
const tLogFormats = t.intersection([
t.record(t.string, t.union([t.string, tMessageContent])),
t.type({
timestamp: t.string,
}),
]),
]);
export const ConfigSchema = t.type({
channels: LogChannelMap,
format: tLogFormats,
ping_user: t.boolean,
});
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;

View file

@ -1,7 +1,14 @@
import { PluginData } from "knub";
import { LogsPluginType } from "../types";
import { LogType } from "src/data/LogType";
import { verboseUserMention, verboseUserName, verboseChannelMention, messageSummary, resolveMember } from "src/utils";
import {
verboseUserMention,
verboseUserName,
verboseChannelMention,
messageSummary,
resolveMember,
renderRecursively,
} from "src/utils";
import { SavedMessage } from "src/data/entities/SavedMessage";
import { renderTemplate, TemplateParseError } from "src/templateFormatter";
import { logger } from "src/logger";
@ -12,8 +19,6 @@ export async function getLogMessage(pluginData: PluginData<LogsPluginType>, type
const format = config.format[LogType[type]] || "";
if (format === "") return;
let formatted;
try {
const values = {
...data,
userMention: async inputUserOrMember => {
@ -58,7 +63,11 @@ export async function getLogMessage(pluginData: PluginData<LogsPluginType>, type
};
}
formatted = await renderTemplate(format, values);
const renderLogString = str => renderTemplate(str, values);
let formatted;
try {
formatted = typeof format === "string" ? await renderLogString(format) : renderRecursively(format, renderLogString);
} catch (e) {
if (e instanceof TemplateParseError) {
logger.error(`Error when parsing template:\nError: ${e.message}\nTemplate: ${format}`);
@ -68,13 +77,15 @@ export async function getLogMessage(pluginData: PluginData<LogsPluginType>, type
}
}
if (typeof formatted === "string") {
formatted = formatted.trim();
const timestampFormat = config.format.timestamp;
if (timestampFormat) {
const timestamp = moment().format(timestampFormat);
return `\`[${timestamp}]\` ${formatted}`;
} else {
formatted = `\`[${timestamp}]\` ${formatted}`;
}
}
return formatted;
}
}

View file

@ -59,7 +59,14 @@ export async function log(pluginData: PluginData<LogsPluginType>, type: LogType,
const message = await getLogMessage(pluginData, type, data);
if (message) {
const batched = opts.batched ?? true; // Default to batched unless explicitly disabled
// For non-string log messages (i.e. embeds) batching or chunking is not possible, so send them immediately
if (typeof message !== "string") {
await channel.createMessage(message).catch(noop);
return;
}
// Default to batched unless explicitly disabled
const batched = opts.batched ?? true;
const batchTime = opts.batch_time ?? 1000;
if (batched) {