3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-14 22:05:01 +00:00

refactor: replace io-ts with zod

This commit is contained in:
Dragory 2024-01-14 14:25:42 +00:00
parent fafaefa1fb
commit 28692962bc
No known key found for this signature in database
161 changed files with 1450 additions and 2105 deletions

View file

@ -6,7 +6,7 @@ import { GuildLogs } from "../../data/GuildLogs";
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
import { LogType } from "../../data/LogType";
import { logger } from "../../logger";
import { makeIoTsConfigParser, mapToPublicFn } from "../../pluginUtils";
import { mapToPublicFn } from "../../pluginUtils";
import { discardRegExpRunner, getRegExpRunner } from "../../regExpRunners";
import { TypedTemplateSafeValueContainer, createTypedTemplateSafeValueContainer } from "../../templateFormatter";
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
@ -31,7 +31,7 @@ import {
import { LogsThreadCreateEvt, LogsThreadDeleteEvt, LogsThreadUpdateEvt } from "./events/LogsThreadModifyEvts";
import { LogsGuildMemberUpdateEvt } from "./events/LogsUserUpdateEvts";
import { LogsVoiceStateUpdateEvt } from "./events/LogsVoiceChannelEvts";
import { ConfigSchema, FORMAT_NO_TIMESTAMP, ILogTypeData, LogsPluginType, TLogChannel } from "./types";
import { FORMAT_NO_TIMESTAMP, ILogTypeData, LogsPluginType, TLogChannel, zLogsConfig } from "./types";
import { getLogMessage } from "./util/getLogMessage";
import { log } from "./util/log";
import { onMessageDelete } from "./util/onMessageDelete";
@ -110,7 +110,6 @@ import { logVoiceChannelForceMove } from "./logFunctions/logVoiceChannelForceMov
import { logVoiceChannelJoin } from "./logFunctions/logVoiceChannelJoin";
import { logVoiceChannelLeave } from "./logFunctions/logVoiceChannelLeave";
import { logVoiceChannelMove } from "./logFunctions/logVoiceChannelMove";
import { asBoundedString } from "../../utils/iotsUtils";
// The `any` cast here is to prevent TypeScript from locking up from the circular dependency
function getCasesPlugin(): Promise<any> {
@ -121,12 +120,12 @@ const defaultOptions: PluginOptions<LogsPluginType> = {
config: {
channels: {},
format: {
timestamp: asBoundedString(FORMAT_NO_TIMESTAMP), // Legacy/deprecated, use timestamp_format below instead
timestamp: FORMAT_NO_TIMESTAMP,
...DefaultLogMessages,
},
ping_user: true, // Legacy/deprecated, if below is false mentions wont actually ping. In case you really want the old behavior, set below to true
ping_user: true,
allow_user_mentions: false,
timestamp_format: asBoundedString("[<t:]X[>]"),
timestamp_format: "[<t:]X[>]",
include_embed_timestamp: true,
},
@ -134,7 +133,8 @@ const defaultOptions: PluginOptions<LogsPluginType> = {
{
level: ">=50",
config: {
ping_user: false, // Legacy/deprecated, read comment on global ping_user option
// Legacy/deprecated, read comment on global ping_user option
ping_user: false,
},
},
],
@ -145,11 +145,11 @@ export const LogsPlugin = zeppelinGuildPlugin<LogsPluginType>()({
showInDocs: true,
info: {
prettyName: "Logs",
configSchema: ConfigSchema,
configSchema: zLogsConfig,
},
dependencies: async () => [TimeAndDatePlugin, InternalPosterPlugin, (await getCasesPlugin()).CasesPlugin],
configParser: makeIoTsConfigParser(ConfigSchema),
configParser: (input) => zLogsConfig.parse(input),
defaultOptions,
events: [

View file

@ -32,7 +32,7 @@ export function logMessageDelete(pluginData: GuildPluginData<LogsPluginType>, da
// See comment on FORMAT_NO_TIMESTAMP in types.ts
const config = pluginData.config.get();
const timestampFormat =
(config.format.timestamp !== FORMAT_NO_TIMESTAMP ? config.format.timestamp : null) ?? config.timestamp_format;
(config.format.timestamp !== FORMAT_NO_TIMESTAMP ? config.format.timestamp : null) ?? config.timestamp_format ?? undefined;
return log(
pluginData,

View file

@ -1,4 +1,3 @@
import * as t from "io-ts";
import { BasePluginType, CooldownManager, guildPluginEventListener } from "knub";
import { z } from "zod";
import { RegExpRunner } from "../../RegExpRunner";
@ -7,7 +6,7 @@ import { GuildCases } from "../../data/GuildCases";
import { GuildLogs } from "../../data/GuildLogs";
import { GuildSavedMessages } from "../../data/GuildSavedMessages";
import { LogType } from "../../data/LogType";
import { tMessageContent, tNullable } from "../../utils";
import { zBoundedCharacters, zMessageContent, zRegex, zSnowflake } from "../../utils";
import { MessageBuffer } from "../../utils/MessageBuffer";
import {
TemplateSafeCase,
@ -22,55 +21,56 @@ import {
TemplateSafeUnknownUser,
TemplateSafeUser,
} from "../../utils/templateSafeObjects";
import { TRegex } from "../../validatorUtils";
import { tBoundedString } from "../../utils/iotsUtils";
export const tLogFormats = t.record(t.string, t.union([t.string, tMessageContent]));
export type TLogFormats = t.TypeOf<typeof tLogFormats>;
const DEFAULT_BATCH_TIME = 1000;
const MIN_BATCH_TIME = 250;
const MAX_BATCH_TIME = 5000;
const LogChannel = t.partial({
include: t.array(t.string),
exclude: t.array(t.string),
batched: t.boolean,
batch_time: t.number,
excluded_users: t.array(t.string),
excluded_message_regexes: t.array(TRegex),
excluded_channels: t.array(t.string),
excluded_categories: t.array(t.string),
excluded_threads: t.array(t.string),
exclude_bots: t.boolean,
excluded_roles: t.array(t.string),
format: tNullable(tLogFormats),
timestamp_format: t.string,
include_embed_timestamp: t.boolean,
export const zLogFormats = z.record(
zBoundedCharacters(1, 255),
zMessageContent,
);
export type TLogFormats = z.infer<typeof zLogFormats>;
const zLogChannel = z.strictObject({
include: z.array(zBoundedCharacters(1, 255)).default([]),
exclude: z.array(zBoundedCharacters(1, 255)).default([]),
batched: z.boolean().default(true),
batch_time: z.number().min(MIN_BATCH_TIME).max(MAX_BATCH_TIME).default(DEFAULT_BATCH_TIME),
excluded_users: z.array(zSnowflake).nullable().default(null),
excluded_message_regexes: z.array(zRegex(z.string())).nullable().default(null),
excluded_channels: z.array(zSnowflake).nullable().default(null),
excluded_categories: z.array(zSnowflake).nullable().default(null),
excluded_threads: z.array(zSnowflake).nullable().default(null),
exclude_bots: z.boolean().default(false),
excluded_roles: z.array(zSnowflake).nullable().default(null),
format: zLogFormats.default({}),
timestamp_format: z.string().nullable().default(null),
include_embed_timestamp: z.boolean().nullable().default(null),
});
export type TLogChannel = t.TypeOf<typeof LogChannel>;
export type TLogChannel = z.infer<typeof zLogChannel>;
const LogChannelMap = t.record(t.string, LogChannel);
export type TLogChannelMap = t.TypeOf<typeof LogChannelMap>;
const zLogChannelMap = z.record(zSnowflake, zLogChannel);
export type TLogChannelMap = z.infer<typeof zLogChannelMap>;
export const ConfigSchema = t.type({
channels: LogChannelMap,
format: t.intersection([
tLogFormats,
t.type({
timestamp: tBoundedString(0, 64), // Legacy/deprecated
}),
]),
ping_user: t.boolean, // Legacy/deprecated, if below is false mentions wont actually ping
allow_user_mentions: t.boolean,
timestamp_format: tBoundedString(0, 64),
include_embed_timestamp: t.boolean,
export const zLogsConfig = z.strictObject({
channels: zLogChannelMap,
format: z.intersection(zLogFormats, z.strictObject({
// Legacy/deprecated, use timestamp_format below instead
timestamp: zBoundedCharacters(0, 64).nullable(),
})),
// Legacy/deprecated, if below is false mentions wont actually ping. In case you really want the old behavior, set below to true
ping_user: z.boolean(),
allow_user_mentions: z.boolean(),
timestamp_format: z.string().nullable(),
include_embed_timestamp: z.boolean(),
});
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.
// Hacky way of allowing a """null""" default value for config.format.timestamp due to legacy io-ts reasons
export const FORMAT_NO_TIMESTAMP = "__NO_TIMESTAMP__";
export interface LogsPluginType extends BasePluginType {
config: TConfigSchema;
config: z.infer<typeof zLogsConfig>;
state: {
guildLogs: GuildLogs;
savedMessages: GuildSavedMessages;