zappyzep/backend/src/plugins/Automod/helpers.ts

78 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as t from "io-ts";
import { GuildPluginData } from "knub";
2020-07-27 20:42:10 +03:00
import { Awaitable } from "knub/dist/utils";
import { AutomodContext, AutomodPluginType } from "./types";
interface BaseAutomodTriggerMatchResult {
2020-07-27 20:42:10 +03:00
extraContexts?: AutomodContext[];
2020-07-27 22:19:34 +03:00
silentClean?: boolean; // TODO: Maybe generalize to a "silent" value in general, which mutes alert/log
summary?: string;
fullSummary?: string;
2020-07-27 20:42:10 +03:00
}
export type AutomodTriggerMatchResult<TExtra extends any = unknown> = unknown extends TExtra
? BaseAutomodTriggerMatchResult
: BaseAutomodTriggerMatchResult & { extra: TExtra };
2020-07-27 20:42:10 +03:00
type AutomodTriggerMatchFn<TConfigType, TMatchResultExtra> = (meta: {
ruleName: string;
pluginData: GuildPluginData<AutomodPluginType>;
2020-07-27 20:42:10 +03:00
context: AutomodContext;
triggerConfig: TConfigType;
}) => Awaitable<null | undefined | AutomodTriggerMatchResult<TMatchResultExtra>>;
2020-07-27 20:42:10 +03:00
type AutomodTriggerRenderMatchInformationFn<TConfigType, TMatchResultExtra> = (meta: {
ruleName: string;
pluginData: GuildPluginData<AutomodPluginType>;
2020-07-27 20:42:10 +03:00
contexts: AutomodContext[];
triggerConfig: TConfigType;
matchResult: AutomodTriggerMatchResult<TMatchResultExtra>;
}) => Awaitable<string>;
2020-07-27 21:51:03 +03:00
export interface AutomodTriggerBlueprint<TConfigType extends t.Any, TMatchResultExtra> {
2020-07-27 20:42:10 +03:00
configType: TConfigType;
defaultConfig: Partial<t.TypeOf<TConfigType>>;
2020-07-27 21:51:03 +03:00
match: AutomodTriggerMatchFn<t.TypeOf<TConfigType>, TMatchResultExtra>;
renderMatchInformation: AutomodTriggerRenderMatchInformationFn<t.TypeOf<TConfigType>, TMatchResultExtra>;
2020-07-27 20:42:10 +03:00
}
2020-07-27 21:51:03 +03:00
export function automodTrigger<TMatchResultExtra>(): <TConfigType extends t.Any>(
2020-07-27 20:42:10 +03:00
blueprint: AutomodTriggerBlueprint<TConfigType, TMatchResultExtra>,
2020-07-27 21:51:03 +03:00
) => AutomodTriggerBlueprint<TConfigType, TMatchResultExtra>;
export function automodTrigger<TConfigType extends t.Any>(
blueprint: AutomodTriggerBlueprint<TConfigType, unknown>,
): AutomodTriggerBlueprint<TConfigType, unknown>;
export function automodTrigger(...args) {
if (args.length) {
return args[0];
} else {
return automodTrigger;
}
2020-07-27 20:42:10 +03:00
}
type AutomodActionApplyFn<TConfigType> = (meta: {
ruleName: string;
pluginData: GuildPluginData<AutomodPluginType>;
2020-07-27 20:42:10 +03:00
contexts: AutomodContext[];
actionConfig: TConfigType;
matchResult: AutomodTriggerMatchResult;
2020-07-27 20:42:10 +03:00
}) => Awaitable<void>;
export interface AutomodActionBlueprint<TConfigType extends t.Any> {
configType: TConfigType;
defaultConfig: Partial<t.TypeOf<TConfigType>>;
2020-07-27 20:42:10 +03:00
apply: AutomodActionApplyFn<t.TypeOf<TConfigType>>;
}
export function automodAction<TConfigType extends t.Any>(
blueprint: AutomodActionBlueprint<TConfigType>,
): AutomodActionBlueprint<TConfigType> {
return blueprint;
}