3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-07 19:17:19 +00:00

refactor: don't create 'name' property in config dynamically

This commit is contained in:
Dragory 2024-11-10 13:47:40 +02:00
parent 0810dd3f0e
commit 395a750e9d
No known key found for this signature in database
19 changed files with 86 additions and 154 deletions

View file

@ -2,8 +2,8 @@ import { GuildPluginData } from "knub";
import { convertDelayStringToMS } from "../../../utils.js";
import { AutomodContext, AutomodPluginType, TRule } from "../types.js";
export function applyCooldown(pluginData: GuildPluginData<AutomodPluginType>, rule: TRule, context: AutomodContext) {
const cooldownKey = `${rule.name}-${context.user?.id}`;
export function applyCooldown(pluginData: GuildPluginData<AutomodPluginType>, rule: TRule, ruleName: string, context: AutomodContext) {
const cooldownKey = `${ruleName}-${context.user?.id}`;
const cooldownTime = convertDelayStringToMS(rule.cooldown, "s");
if (cooldownTime) pluginData.state.cooldownManager.setCooldown(cooldownKey, cooldownTime);

View file

@ -1,8 +1,8 @@
import { GuildPluginData } from "knub";
import { AutomodContext, AutomodPluginType, TRule } from "../types.js";
export function checkCooldown(pluginData: GuildPluginData<AutomodPluginType>, rule: TRule, context: AutomodContext) {
const cooldownKey = `${rule.name}-${context.user?.id}`;
export function checkCooldown(pluginData: GuildPluginData<AutomodPluginType>, rule: TRule, ruleName: string, context: AutomodContext) {
const cooldownKey = `${ruleName}-${context.user?.id}`;
return pluginData.state.cooldownManager.isOnCooldown(cooldownKey);
}

View file

@ -49,7 +49,7 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
}
if (!rule.affects_self && userId && userId === pluginData.client.user?.id) continue;
if (rule.cooldown && checkCooldown(pluginData, rule, context)) {
if (rule.cooldown && checkCooldown(pluginData, rule, ruleName, context)) {
continue;
}
@ -87,7 +87,7 @@ export async function runAutomod(pluginData: GuildPluginData<AutomodPluginType>,
}
if (matchResult) {
if (rule.cooldown) applyCooldown(pluginData, rule, context);
if (rule.cooldown) applyCooldown(pluginData, rule, ruleName, context);
contexts = [context, ...(matchResult.extraContexts || [])];

View file

@ -9,30 +9,23 @@ interface MatchResultType {
mode: "blacklist" | "whitelist";
}
const configSchema = z
.strictObject({
filetype_blacklist: z.array(z.string().max(32)).max(255).default([]),
blacklist_enabled: z.boolean().default(false),
filetype_whitelist: z.array(z.string().max(32)).max(255).default([]),
whitelist_enabled: z.boolean().default(false),
})
.transform((parsed, ctx) => {
if (parsed.blacklist_enabled && parsed.whitelist_enabled) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Cannot have both blacklist and whitelist enabled",
});
return z.NEVER;
}
if (!parsed.blacklist_enabled && !parsed.whitelist_enabled) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Must have either blacklist or whitelist enabled",
});
return z.NEVER;
}
return parsed;
});
const baseConfig = z.strictObject({
filetype_blacklist: z.array(z.string().max(32)).max(255).default([]),
filetype_whitelist: z.array(z.string().max(32)).max(255).default([]),
});
const configWithWhitelist = baseConfig.merge(z.strictObject({
whitelist_enabled: z.literal(true),
blacklist_enabled: z.literal(false).default(false),
}));
const configWithBlacklist = baseConfig.merge(z.strictObject({
blacklist_enabled: z.literal(true),
whitelist_enabled: z.literal(false).default(false),
}));
const configSchema = z.union([
configWithWhitelist,
configWithBlacklist,
]);
export const MatchAttachmentTypeTrigger = automodTrigger<MatchResultType>()({
configSchema,

View file

@ -8,30 +8,23 @@ interface MatchResultType {
mode: "blacklist" | "whitelist";
}
const configSchema = z
.strictObject({
mime_type_blacklist: z.array(z.string().max(255)).max(255).default([]),
blacklist_enabled: z.boolean().default(false),
mime_type_whitelist: z.array(z.string().max(255)).max(255).default([]),
whitelist_enabled: z.boolean().default(false),
})
.transform((parsed, ctx) => {
if (parsed.blacklist_enabled && parsed.whitelist_enabled) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Cannot have both blacklist and whitelist enabled",
});
return z.NEVER;
}
if (!parsed.blacklist_enabled && !parsed.whitelist_enabled) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Must have either blacklist or whitelist enabled",
});
return z.NEVER;
}
return parsed;
});
const baseConfig = z.strictObject({
mime_type_blacklist: z.array(z.string().max(32)).max(255).default([]),
mime_type_whitelist: z.array(z.string().max(32)).max(255).default([]),
});
const configWithWhitelist = baseConfig.merge(z.strictObject({
whitelist_enabled: z.literal(true),
blacklist_enabled: z.literal(false).default(false),
}));
const configWithBlacklist = baseConfig.merge(z.strictObject({
blacklist_enabled: z.literal(true),
whitelist_enabled: z.literal(false).default(false),
}));
const configSchema = z.union([
configWithWhitelist,
configWithBlacklist,
]);
export const MatchMimeTypeTrigger = automodTrigger<MatchResultType>()({
configSchema,

View file

@ -46,22 +46,6 @@ const zActionsMap = z
const zRule = z.strictObject({
enabled: z.boolean().default(true),
// Typed as "never" because you are not expected to supply this directly.
// The transform instead picks it up from the property key and the output type is a string.
name: z
.never()
.optional()
.transform((_, ctx) => {
const ruleName = String(ctx.path[ctx.path.length - 2]).trim();
if (!ruleName) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "Automod rules must have names",
});
return z.NEVER;
}
return ruleName;
}),
pretty_name: z.string().optional(),
presets: z.array(z.string().max(100)).max(25).default([]),
affects_bots: z.boolean().default(false),
@ -69,9 +53,7 @@ const zRule = z.strictObject({
cooldown: zDelayString.nullable().default(null),
allow_further_rules: z.boolean().default(false),
triggers: z.array(zTriggersMap),
actions: zActionsMap.refine((v) => !(v.clean && v.start_thread), {
message: "Cannot have both clean and start_thread active at the same time",
}),
actions: zActionsMap,
});
export type TRule = z.infer<typeof zRule>;