3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-15 05:41:51 +00:00

configPreprocessor => configParser

Signed-off-by: GitHub <noreply@github.com>
This commit is contained in:
metal 2023-03-12 00:37:07 +00:00 committed by GitHub
parent cd68a6083c
commit ddec78b929
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 25 additions and 26 deletions

View file

@ -36,7 +36,7 @@ export async function validateGuildConfig(config: any): Promise<string | null> {
const plugin = pluginNameToPlugin.get(pluginName)!;
try {
const mergedOptions = configUtils.mergeConfig(plugin.defaultOptions || {}, pluginOptions);
await plugin.configPreprocessor?.(mergedOptions as unknown as PluginOptions<any>, true);
await plugin.configParser?.(mergedOptions as unknown as PluginOptions<any>, true);
} catch (err) {
if (err instanceof ConfigValidationError || err instanceof StrictValidationError) {
return `${pluginName}: ${err.message}`;

View file

@ -91,10 +91,7 @@ export function strictValidationErrorToConfigValidationError(err: StrictValidati
);
}
export function getPluginConfigPreprocessor(
blueprint: ZeppelinPlugin,
customPreprocessor?: ZeppelinPlugin["configPreprocessor"],
) {
export function getPluginConfigParser(blueprint: ZeppelinPlugin, customParser?: ZeppelinPlugin["configParser"]) {
return async (options: PluginOptions<any>, strict?: boolean) => {
// 1. Validate the basic structure of plugin config
const basicOptionsValidation = validate(BasicPluginStructureType, options);
@ -146,8 +143,8 @@ export function getPluginConfigPreprocessor(
}
// 3. Run custom preprocessor, if any
if (customPreprocessor) {
options = await customPreprocessor(options);
if (customParser) {
options = await customParser(options);
}
// 4. Merge with default options and validate/decode the entire config

View file

@ -1,5 +1,5 @@
import { configUtils, CooldownManager } from "knub";
import { ConfigPreprocessorFn } from "knub/dist/config/configTypes";
import { ConfigParserFn } from "knub/dist/config/configTypes";
import { GuildAntiraidLevels } from "../../data/GuildAntiraidLevels";
import { GuildArchives } from "../../data/GuildArchives";
import { GuildLogs } from "../../data/GuildLogs";
@ -64,7 +64,8 @@ const defaultOptions = {
/**
* Config preprocessor to set default values for triggers and perform extra validation
*/
const configPreprocessor: ConfigPreprocessorFn<AutomodPluginType> = (options) => {
// TODO: Fix `any` typing
const configParser: ConfigParserFn<AutomodPluginType> = (options: any) => {
if (options.config?.rules) {
// Loop through each rule
for (const [name, rule] of Object.entries(options.config.rules)) {
@ -199,7 +200,7 @@ export const AutomodPlugin = zeppelinGuildPlugin<AutomodPluginType>()({
configSchema: ConfigSchema,
defaultOptions,
configPreprocessor,
configParser,
customOverrideCriteriaFunctions: {
antiraid_level: (pluginData, matchParams, value) => {

View file

@ -1,6 +1,6 @@
import { EventEmitter } from "events";
import { PluginOptions } from "knub";
import { ConfigPreprocessorFn } from "knub/dist/config/configTypes";
import { ConfigParserFn } from "knub/dist/config/configTypes";
import {
buildCounterConditionString,
CounterTrigger,
@ -55,7 +55,8 @@ const defaultOptions: PluginOptions<CountersPluginType> = {
],
};
const configPreprocessor: ConfigPreprocessorFn<CountersPluginType> = (options) => {
// TODO: Fix `any` typing
const configParser: ConfigParserFn<CountersPluginType> = (options: any) => {
for (const [counterName, counter] of Object.entries(options.config?.counters || {})) {
counter.name = counterName;
counter.per_user = counter.per_user ?? false;
@ -117,7 +118,7 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()({
configSchema: ConfigSchema,
defaultOptions,
configPreprocessor,
configParser,
public: {
counterExists: mapToPublicFn(counterExists),

View file

@ -32,7 +32,7 @@ export const RoleButtonsPlugin = zeppelinGuildPlugin<RoleButtonsPluginType>()({
],
},
configPreprocessor(options) {
configParser(options) {
// Auto-fill "name" property for buttons based on the object key
const buttonsArray = Array.isArray(options.config?.buttons) ? options.config.buttons : [];
const seenMessages = new Set();

View file

@ -70,7 +70,7 @@ export const SelfGrantableRolesPlugin = zeppelinGuildPlugin<SelfGrantableRolesPl
`),
},
configPreprocessor: (options) => {
configParser: (options) => {
const config = options.config;
for (const [key, entry] of Object.entries(config.entries)) {
// Apply default entry config

View file

@ -124,7 +124,7 @@ export const StarboardPlugin = zeppelinGuildPlugin<StarboardPluginType>()({
`),
},
configPreprocessor(options) {
configParser(options) {
if (options.config?.boards) {
for (const [name, opts] of Object.entries(options.config.boards)) {
options.config.boards[name] = Object.assign({}, defaultStarboardOpts, options.config.boards[name]);

View file

@ -96,7 +96,7 @@ export const TagsPlugin = zeppelinGuildPlugin<TagsPluginType>()({
findTagByName: mapToPublicFn(findTagByName),
},
configPreprocessor(options) {
configParser(options) {
if (options.config.delete_with_command && options.config.auto_delete_command) {
throw new StrictValidationError([
`Cannot have both (global) delete_with_command and global_delete_invoke enabled`,

View file

@ -10,7 +10,7 @@ import {
} from "knub";
import { PluginOptions } from "knub/dist/config/configTypes";
import { Awaitable } from "knub/dist/utils";
import { getPluginConfigPreprocessor } from "../pluginUtils";
import { getPluginConfigParser } from "../pluginUtils";
import { TMarkdown } from "../types";
/**
@ -30,7 +30,7 @@ export interface ZeppelinGuildPluginBlueprint<TPluginData extends GuildPluginDat
legacy?: boolean | string;
};
configPreprocessor?: (
configParser?: (
options: PluginOptions<TPluginData["_pluginType"]>,
strict?: boolean,
) => Awaitable<PluginOptions<TPluginData["_pluginType"]>>;
@ -38,14 +38,14 @@ export interface ZeppelinGuildPluginBlueprint<TPluginData extends GuildPluginDat
export function zeppelinGuildPlugin<TBlueprint extends ZeppelinGuildPluginBlueprint>(
blueprint: TBlueprint,
): TBlueprint & { configPreprocessor: ZeppelinGuildPluginBlueprint["configPreprocessor"] };
): TBlueprint & { configParser: ZeppelinGuildPluginBlueprint["configParser"] };
export function zeppelinGuildPlugin<TPluginType extends BasePluginType>(): <
TBlueprint extends ZeppelinGuildPluginBlueprint<GuildPluginData<TPluginType>>,
>(
blueprint: TBlueprint,
) => TBlueprint & {
configPreprocessor: ZeppelinGuildPluginBlueprint<GuildPluginData<TPluginType>>["configPreprocessor"];
configParser: ZeppelinGuildPluginBlueprint<GuildPluginData<TPluginType>>["configParser"];
};
export function zeppelinGuildPlugin(...args) {
@ -53,7 +53,7 @@ export function zeppelinGuildPlugin(...args) {
const blueprint = guildPlugin(
...(args as Parameters<typeof guildPlugin>),
) as unknown as ZeppelinGuildPluginBlueprint;
blueprint.configPreprocessor = getPluginConfigPreprocessor(blueprint, blueprint.configPreprocessor);
blueprint.configParser = getPluginConfigParser(blueprint, blueprint.configParser);
return blueprint;
} else {
return zeppelinGuildPlugin as (name, blueprint) => ZeppelinGuildPluginBlueprint;
@ -67,19 +67,19 @@ export function zeppelinGuildPlugin(...args) {
export interface ZeppelinGlobalPluginBlueprint<TPluginType extends BasePluginType = BasePluginType>
extends GlobalPluginBlueprint<GlobalPluginData<TPluginType>> {
configSchema: t.TypeC<any>;
configPreprocessor?: (options: PluginOptions<TPluginType>, strict?: boolean) => Awaitable<PluginOptions<TPluginType>>;
configParser?: (options: PluginOptions<TPluginType>, strict?: boolean) => Awaitable<PluginOptions<TPluginType>>;
}
export function zeppelinGlobalPlugin<TBlueprint extends ZeppelinGlobalPluginBlueprint>(
blueprint: TBlueprint,
): TBlueprint & { configPreprocessor: ZeppelinGlobalPluginBlueprint["configPreprocessor"] };
): TBlueprint & { configParser: ZeppelinGlobalPluginBlueprint["configParser"] };
export function zeppelinGlobalPlugin<TPluginType extends BasePluginType>(): <
TBlueprint extends ZeppelinGlobalPluginBlueprint<TPluginType>,
>(
blueprint: TBlueprint,
) => TBlueprint & {
configPreprocessor: ZeppelinGlobalPluginBlueprint<TPluginType>["configPreprocessor"];
configParser: ZeppelinGlobalPluginBlueprint<TPluginType>["configParser"];
};
export function zeppelinGlobalPlugin(...args) {
@ -88,7 +88,7 @@ export function zeppelinGlobalPlugin(...args) {
...(args as Parameters<typeof globalPlugin>),
) as unknown as ZeppelinGlobalPluginBlueprint;
// @ts-ignore FIXME: Check the types here
blueprint.configPreprocessor = getPluginConfigPreprocessor(blueprint, blueprint.configPreprocessor);
blueprint.configParser = getPluginConfigParser(blueprint, blueprint.configParser);
return blueprint;
} else {
return zeppelinGlobalPlugin as (name, blueprint) => ZeppelinGlobalPluginBlueprint;