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

@ -2,7 +2,6 @@ import { GuildChannel, GuildMember, User } from "discord.js";
import { guildPluginMessageCommand, parseSignature } from "knub";
import { TSignature } from "knub-command-manager";
import { commandTypes } from "../../commandTypes";
import { makeIoTsConfigParser } from "../../pluginUtils";
import { TemplateSafeValueContainer, createTypedTemplateSafeValueContainer } from "../../templateFormatter";
import { UnknownUser } from "../../utils";
import { isScalar } from "../../utils/isScalar";
@ -14,7 +13,7 @@ import {
} from "../../utils/templateSafeObjects";
import { zeppelinGuildPlugin } from "../ZeppelinPluginBlueprint";
import { runEvent } from "./functions/runEvent";
import { ConfigSchema, CustomEventsPluginType } from "./types";
import { CustomEventsPluginType, zCustomEventsConfig } from "./types";
const defaultOptions = {
config: {
@ -26,7 +25,7 @@ export const CustomEventsPlugin = zeppelinGuildPlugin<CustomEventsPluginType>()(
name: "custom_events",
showInDocs: false,
configParser: makeIoTsConfigParser(ConfigSchema),
configParser: (input) => zCustomEventsConfig.parse(input),
defaultOptions,
afterLoad(pluginData) {

View file

@ -1,17 +1,17 @@
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { canActOn } from "../../../pluginUtils";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { resolveMember } from "../../../utils";
import { resolveMember, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const AddRoleAction = t.type({
type: t.literal("add_role"),
target: t.string,
role: t.union([t.string, t.array(t.string)]),
export const zAddRoleAction = z.strictObject({
type: z.literal("add_role"),
target: zSnowflake,
role: z.union([zSnowflake, z.array(zSnowflake)]),
});
export type TAddRoleAction = t.TypeOf<typeof AddRoleAction>;
export type TAddRoleAction = z.infer<typeof zAddRoleAction>;
export async function addRoleAction(
pluginData: GuildPluginData<CustomEventsPluginType>,

View file

@ -1,19 +1,20 @@
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { CaseTypes } from "../../../data/CaseTypes";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
import { zBoundedCharacters, zSnowflake } from "../../../utils";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const CreateCaseAction = t.type({
type: t.literal("create_case"),
case_type: t.string,
mod: t.string,
target: t.string,
reason: t.string,
export const zCreateCaseAction = z.strictObject({
type: z.literal("create_case"),
case_type: zBoundedCharacters(0, 32),
mod: zSnowflake,
target: zSnowflake,
reason: zBoundedCharacters(0, 4000),
});
export type TCreateCaseAction = t.TypeOf<typeof CreateCaseAction>;
export type TCreateCaseAction = z.infer<typeof zCreateCaseAction>;
export async function createCaseAction(
pluginData: GuildPluginData<CustomEventsPluginType>,
@ -32,7 +33,7 @@ export async function createCaseAction(
}
const casesPlugin = pluginData.getPlugin(CasesPlugin);
await casesPlugin.createCase({
await casesPlugin!.createCase({
userId: targetId,
modId,
type: CaseTypes[action.case_type],

View file

@ -1,17 +1,17 @@
import { Snowflake } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
import { convertDelayStringToMS, noop, tDelayString } from "../../../utils";
import { convertDelayStringToMS, noop, zDelayString, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const MakeRoleMentionableAction = t.type({
type: t.literal("make_role_mentionable"),
role: t.string,
timeout: tDelayString,
export const zMakeRoleMentionableAction = z.strictObject({
type: z.literal("make_role_mentionable"),
role: zSnowflake,
timeout: zDelayString,
});
export type TMakeRoleMentionableAction = t.TypeOf<typeof MakeRoleMentionableAction>;
export type TMakeRoleMentionableAction = z.infer<typeof zMakeRoleMentionableAction>;
export async function makeRoleMentionableAction(
pluginData: GuildPluginData<CustomEventsPluginType>,

View file

@ -1,15 +1,16 @@
import { Snowflake } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
import { zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const MakeRoleUnmentionableAction = t.type({
type: t.literal("make_role_unmentionable"),
role: t.string,
export const zMakeRoleUnmentionableAction = z.strictObject({
type: z.literal("make_role_unmentionable"),
role: zSnowflake,
});
export type TMakeRoleUnmentionableAction = t.TypeOf<typeof MakeRoleUnmentionableAction>;
export type TMakeRoleUnmentionableAction = z.infer<typeof zMakeRoleUnmentionableAction>;
export async function makeRoleUnmentionableAction(
pluginData: GuildPluginData<CustomEventsPluginType>,

View file

@ -1,16 +1,17 @@
import { Snowflake, TextChannel } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
import { zBoundedCharacters, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType } from "../types";
export const MessageAction = t.type({
type: t.literal("message"),
channel: t.string,
content: t.string,
export const zMessageAction = z.strictObject({
type: z.literal("message"),
channel: zSnowflake,
content: zBoundedCharacters(0, 4000),
});
export type TMessageAction = t.TypeOf<typeof MessageAction>;
export type TMessageAction = z.infer<typeof zMessageAction>;
export async function messageAction(
pluginData: GuildPluginData<CustomEventsPluginType>,

View file

@ -1,18 +1,18 @@
import { Snowflake, VoiceChannel } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { canActOn } from "../../../pluginUtils";
import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFormatter";
import { resolveMember } from "../../../utils";
import { resolveMember, zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const MoveToVoiceChannelAction = t.type({
type: t.literal("move_to_vc"),
target: t.string,
channel: t.string,
export const zMoveToVoiceChannelAction = z.strictObject({
type: z.literal("move_to_vc"),
target: zSnowflake,
channel: zSnowflake,
});
export type TMoveToVoiceChannelAction = t.TypeOf<typeof MoveToVoiceChannelAction>;
export type TMoveToVoiceChannelAction = z.infer<typeof zMoveToVoiceChannelAction>;
export async function moveToVoiceChannelAction(
pluginData: GuildPluginData<CustomEventsPluginType>,

View file

@ -1,23 +1,24 @@
import { PermissionsBitField, PermissionsString, Snowflake } from "discord.js";
import * as t from "io-ts";
import { GuildPluginData } from "knub";
import z from "zod";
import { TemplateSafeValueContainer } from "../../../templateFormatter";
import { zSnowflake } from "../../../utils";
import { ActionError } from "../ActionError";
import { CustomEventsPluginType, TCustomEvent } from "../types";
export const SetChannelPermissionOverridesAction = t.type({
type: t.literal("set_channel_permission_overrides"),
channel: t.string,
overrides: t.array(
t.type({
type: t.union([t.literal("member"), t.literal("role")]),
id: t.string,
allow: t.number,
deny: t.number,
export const zSetChannelPermissionOverridesAction = z.strictObject({
type: z.literal("set_channel_permission_overrides"),
channel: zSnowflake,
overrides: z.array(
z.strictObject({
type: z.union([z.literal("member"), z.literal("role")]),
id: zSnowflake,
allow: z.number(),
deny: z.number(),
}),
),
).max(15),
});
export type TSetChannelPermissionOverridesAction = t.TypeOf<typeof SetChannelPermissionOverridesAction>;
export type TSetChannelPermissionOverridesAction = z.infer<typeof zSetChannelPermissionOverridesAction>;
export async function setChannelPermissionOverridesAction(
pluginData: GuildPluginData<CustomEventsPluginType>,

View file

@ -1,47 +1,50 @@
import * as t from "io-ts";
import { BasePluginType } from "knub";
import { AddRoleAction } from "./actions/addRoleAction";
import { CreateCaseAction } from "./actions/createCaseAction";
import { MakeRoleMentionableAction } from "./actions/makeRoleMentionableAction";
import { MakeRoleUnmentionableAction } from "./actions/makeRoleUnmentionableAction";
import { MessageAction } from "./actions/messageAction";
import { MoveToVoiceChannelAction } from "./actions/moveToVoiceChannelAction";
import { SetChannelPermissionOverridesAction } from "./actions/setChannelPermissionOverrides";
import z from "zod";
import { zBoundedCharacters, zBoundedRecord } from "../../utils";
import { zAddRoleAction } from "./actions/addRoleAction";
import { zCreateCaseAction } from "./actions/createCaseAction";
import { zMakeRoleMentionableAction } from "./actions/makeRoleMentionableAction";
import { zMakeRoleUnmentionableAction } from "./actions/makeRoleUnmentionableAction";
import { zMessageAction } from "./actions/messageAction";
import { zMoveToVoiceChannelAction } from "./actions/moveToVoiceChannelAction";
import { zSetChannelPermissionOverridesAction } from "./actions/setChannelPermissionOverrides";
// Triggers
const CommandTrigger = t.type({
type: t.literal("command"),
name: t.string,
params: t.string,
can_use: t.boolean,
const zCommandTrigger = z.strictObject({
type: z.literal("command"),
name: zBoundedCharacters(0, 100),
params: zBoundedCharacters(0, 255),
can_use: z.boolean(),
});
const AnyTrigger = CommandTrigger; // TODO: Make into a union once we have more triggers
const zAnyTrigger = zCommandTrigger; // TODO: Make into a union once we have more triggers
const AnyAction = t.union([
AddRoleAction,
CreateCaseAction,
MoveToVoiceChannelAction,
MessageAction,
MakeRoleMentionableAction,
MakeRoleUnmentionableAction,
SetChannelPermissionOverridesAction,
const zAnyAction = z.union([
zAddRoleAction,
zCreateCaseAction,
zMoveToVoiceChannelAction,
zMessageAction,
zMakeRoleMentionableAction,
zMakeRoleUnmentionableAction,
zSetChannelPermissionOverridesAction,
]);
export const CustomEvent = t.type({
name: t.string,
trigger: AnyTrigger,
actions: t.array(AnyAction),
export const zCustomEvent = z.strictObject({
name: zBoundedCharacters(0, 100),
trigger: zAnyTrigger,
actions: z.array(zAnyAction).max(10),
});
export type TCustomEvent = t.TypeOf<typeof CustomEvent>;
export type TCustomEvent = z.infer<typeof zCustomEvent>;
export const ConfigSchema = t.type({
events: t.record(t.string, CustomEvent),
export const zCustomEventsConfig = z.strictObject({
events: zBoundedRecord(
z.record(zBoundedCharacters(0, 100), zCustomEvent),
0,
100,
),
});
export type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
export interface CustomEventsPluginType extends BasePluginType {
config: TConfigSchema;
config: z.infer<typeof zCustomEventsConfig>;
state: {
clearTriggers: () => void;
};