logs: add timestamps to embeds, add setting to turn off embed timestamps, move timestamp format setting to root/channel level
This commit is contained in:
parent
b158817bff
commit
400f1f0807
4 changed files with 42 additions and 16 deletions
|
@ -1,6 +1,6 @@
|
||||||
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
|
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
|
||||||
import { PluginOptions } from "knub";
|
import { PluginOptions } from "knub";
|
||||||
import { ConfigSchema, LogsPluginType } from "./types";
|
import { ConfigSchema, FORMAT_NO_TIMESTAMP, LogsPluginType } from "./types";
|
||||||
import DefaultLogMessages from "../../data/DefaultLogMessages.json";
|
import DefaultLogMessages from "../../data/DefaultLogMessages.json";
|
||||||
import { GuildLogs } from "../../data/GuildLogs";
|
import { GuildLogs } from "../../data/GuildLogs";
|
||||||
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
|
||||||
|
@ -28,10 +28,12 @@ const defaultOptions: PluginOptions<LogsPluginType> = {
|
||||||
config: {
|
config: {
|
||||||
channels: {},
|
channels: {},
|
||||||
format: {
|
format: {
|
||||||
timestamp: "YYYY-MM-DD HH:mm:ss z",
|
timestamp: FORMAT_NO_TIMESTAMP, // Legacy/deprecated, use timestamp_format below instead
|
||||||
...DefaultLogMessages,
|
...DefaultLogMessages,
|
||||||
},
|
},
|
||||||
ping_user: true,
|
ping_user: true,
|
||||||
|
timestamp_format: "YYYY-MM-DD HH:mm:ss z",
|
||||||
|
include_embed_timestamp: true,
|
||||||
},
|
},
|
||||||
|
|
||||||
overrides: [
|
overrides: [
|
||||||
|
|
|
@ -20,6 +20,8 @@ const LogChannel = t.partial({
|
||||||
excluded_message_regexes: t.array(TRegex),
|
excluded_message_regexes: t.array(TRegex),
|
||||||
excluded_channels: t.array(t.string),
|
excluded_channels: t.array(t.string),
|
||||||
format: tNullable(tLogFormats),
|
format: tNullable(tLogFormats),
|
||||||
|
timestamp_format: t.string,
|
||||||
|
include_embed_timestamp: t.boolean,
|
||||||
});
|
});
|
||||||
export type TLogChannel = t.TypeOf<typeof LogChannel>;
|
export type TLogChannel = t.TypeOf<typeof LogChannel>;
|
||||||
|
|
||||||
|
@ -31,13 +33,20 @@ export const ConfigSchema = t.type({
|
||||||
format: t.intersection([
|
format: t.intersection([
|
||||||
tLogFormats,
|
tLogFormats,
|
||||||
t.type({
|
t.type({
|
||||||
timestamp: t.string,
|
timestamp: t.string, // Legacy/deprecated
|
||||||
}),
|
}),
|
||||||
]),
|
]),
|
||||||
ping_user: t.boolean,
|
ping_user: t.boolean,
|
||||||
|
timestamp_format: t.string,
|
||||||
|
include_embed_timestamp: t.boolean,
|
||||||
});
|
});
|
||||||
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
|
||||||
|
|
||||||
|
// Hacky way of allowing a """null""" default value for config.format.timestamp
|
||||||
|
// The type cannot be made nullable properly because io-ts's intersection type still considers
|
||||||
|
// that it has to match the record type of tLogFormats, which includes string.
|
||||||
|
export const FORMAT_NO_TIMESTAMP = "__NO_TIMESTAMP__";
|
||||||
|
|
||||||
export interface LogsPluginType extends BasePluginType {
|
export interface LogsPluginType extends BasePluginType {
|
||||||
config: TConfigSchema;
|
config: TConfigSchema;
|
||||||
state: {
|
state: {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import { GuildPluginData } from "knub";
|
import { GuildPluginData } from "knub";
|
||||||
import { LogsPluginType, TLogFormats } from "../types";
|
import { FORMAT_NO_TIMESTAMP, LogsPluginType, TLogChannel, TLogFormats } from "../types";
|
||||||
import { LogType } from "../../../data/LogType";
|
import { LogType } from "../../../data/LogType";
|
||||||
import {
|
import {
|
||||||
verboseUserMention,
|
verboseUserMention,
|
||||||
|
@ -19,14 +19,27 @@ export async function getLogMessage(
|
||||||
pluginData: GuildPluginData<LogsPluginType>,
|
pluginData: GuildPluginData<LogsPluginType>,
|
||||||
type: LogType,
|
type: LogType,
|
||||||
data: any,
|
data: any,
|
||||||
formats?: TLogFormats,
|
opts?: Pick<TLogChannel, "format" | "timestamp_format" | "include_embed_timestamp">,
|
||||||
): Promise<string> {
|
): Promise<string> {
|
||||||
const config = pluginData.config.get();
|
const config = pluginData.config.get();
|
||||||
const format = (formats && formats[LogType[type]]) || config.format[LogType[type]] || "";
|
const format = opts?.format?.[LogType[type]] || config.format[LogType[type]] || "";
|
||||||
if (format === "") return;
|
if (format === "" || format == null) return;
|
||||||
|
|
||||||
|
// See comment on FORMAT_NO_TIMESTAMP in types.ts
|
||||||
|
const timestampFormat =
|
||||||
|
opts?.timestamp_format ??
|
||||||
|
(config.format.timestamp !== FORMAT_NO_TIMESTAMP ? config.format.timestamp : null) ??
|
||||||
|
config.timestamp_format;
|
||||||
|
|
||||||
|
const includeEmbedTimestamp = opts?.include_embed_timestamp ?? config.include_embed_timestamp;
|
||||||
|
|
||||||
|
const time = pluginData.getPlugin(TimeAndDatePlugin).inGuildTz();
|
||||||
|
const isoTimestamp = time.toISOString();
|
||||||
|
const timestamp = timestampFormat ? time.format(timestampFormat) : "";
|
||||||
|
|
||||||
const values = {
|
const values = {
|
||||||
...data,
|
...data,
|
||||||
|
timestamp,
|
||||||
userMention: async inputUserOrMember => {
|
userMention: async inputUserOrMember => {
|
||||||
if (!inputUserOrMember) return "";
|
if (!inputUserOrMember) return "";
|
||||||
|
|
||||||
|
@ -73,7 +86,8 @@ export async function getLogMessage(
|
||||||
|
|
||||||
let formatted;
|
let formatted;
|
||||||
try {
|
try {
|
||||||
formatted = typeof format === "string" ? await renderLogString(format) : renderRecursively(format, renderLogString);
|
formatted =
|
||||||
|
typeof format === "string" ? await renderLogString(format) : await renderRecursively(format, renderLogString);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof TemplateParseError) {
|
if (e instanceof TemplateParseError) {
|
||||||
logger.error(`Error when parsing template:\nError: ${e.message}\nTemplate: ${format}`);
|
logger.error(`Error when parsing template:\nError: ${e.message}\nTemplate: ${format}`);
|
||||||
|
@ -85,15 +99,11 @@ export async function getLogMessage(
|
||||||
|
|
||||||
if (typeof formatted === "string") {
|
if (typeof formatted === "string") {
|
||||||
formatted = formatted.trim();
|
formatted = formatted.trim();
|
||||||
|
if (timestamp) {
|
||||||
const timestampFormat = config.format.timestamp;
|
|
||||||
if (timestampFormat) {
|
|
||||||
const timestamp = pluginData
|
|
||||||
.getPlugin(TimeAndDatePlugin)
|
|
||||||
.inGuildTz()
|
|
||||||
.format(timestampFormat);
|
|
||||||
formatted = `\`[${timestamp}]\` ${formatted}`;
|
formatted = `\`[${timestamp}]\` ${formatted}`;
|
||||||
}
|
}
|
||||||
|
} else if (formatted != null && formatted.embed && includeEmbedTimestamp) {
|
||||||
|
formatted.embed.timestamp = isoTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
return formatted;
|
return formatted;
|
||||||
|
|
|
@ -62,7 +62,12 @@ export async function log(pluginData: GuildPluginData<LogsPluginType>, type: Log
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const message = await getLogMessage(pluginData, type, data, opts.format);
|
const message = await getLogMessage(pluginData, type, data, {
|
||||||
|
format: opts.format,
|
||||||
|
include_embed_timestamp: opts.include_embed_timestamp,
|
||||||
|
timestamp_format: opts.timestamp_format,
|
||||||
|
});
|
||||||
|
|
||||||
if (message) {
|
if (message) {
|
||||||
// For non-string log messages (i.e. embeds) batching or chunking is not possible, so send them immediately
|
// For non-string log messages (i.e. embeds) batching or chunking is not possible, so send them immediately
|
||||||
if (typeof message !== "string") {
|
if (typeof message !== "string") {
|
||||||
|
|
Loading…
Add table
Reference in a new issue