3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-03-16 14:11:50 +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)!; const plugin = pluginNameToPlugin.get(pluginName)!;
try { try {
const mergedOptions = configUtils.mergeConfig(plugin.defaultOptions || {}, pluginOptions); 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) { } catch (err) {
if (err instanceof ConfigValidationError || err instanceof StrictValidationError) { if (err instanceof ConfigValidationError || err instanceof StrictValidationError) {
return `${pluginName}: ${err.message}`; return `${pluginName}: ${err.message}`;

View file

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

View file

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

View file

@ -1,6 +1,6 @@
import { EventEmitter } from "events"; import { EventEmitter } from "events";
import { PluginOptions } from "knub"; import { PluginOptions } from "knub";
import { ConfigPreprocessorFn } from "knub/dist/config/configTypes"; import { ConfigParserFn } from "knub/dist/config/configTypes";
import { import {
buildCounterConditionString, buildCounterConditionString,
CounterTrigger, 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 || {})) { for (const [counterName, counter] of Object.entries(options.config?.counters || {})) {
counter.name = counterName; counter.name = counterName;
counter.per_user = counter.per_user ?? false; counter.per_user = counter.per_user ?? false;
@ -117,7 +118,7 @@ export const CountersPlugin = zeppelinGuildPlugin<CountersPluginType>()({
configSchema: ConfigSchema, configSchema: ConfigSchema,
defaultOptions, defaultOptions,
configPreprocessor, configParser,
public: { public: {
counterExists: mapToPublicFn(counterExists), 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 // Auto-fill "name" property for buttons based on the object key
const buttonsArray = Array.isArray(options.config?.buttons) ? options.config.buttons : []; const buttonsArray = Array.isArray(options.config?.buttons) ? options.config.buttons : [];
const seenMessages = new Set(); const seenMessages = new Set();

View file

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

View file

@ -124,7 +124,7 @@ export const StarboardPlugin = zeppelinGuildPlugin<StarboardPluginType>()({
`), `),
}, },
configPreprocessor(options) { configParser(options) {
if (options.config?.boards) { if (options.config?.boards) {
for (const [name, opts] of Object.entries(options.config.boards)) { for (const [name, opts] of Object.entries(options.config.boards)) {
options.config.boards[name] = Object.assign({}, defaultStarboardOpts, options.config.boards[name]); 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), findTagByName: mapToPublicFn(findTagByName),
}, },
configPreprocessor(options) { configParser(options) {
if (options.config.delete_with_command && options.config.auto_delete_command) { if (options.config.delete_with_command && options.config.auto_delete_command) {
throw new StrictValidationError([ throw new StrictValidationError([
`Cannot have both (global) delete_with_command and global_delete_invoke enabled`, `Cannot have both (global) delete_with_command and global_delete_invoke enabled`,

View file

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