mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-18 15:00:00 +00:00
54 lines
1.9 KiB
TypeScript
54 lines
1.9 KiB
TypeScript
import { ConfigValidationError, PluginConfigManager } from "knub";
|
|
import moment from "moment-timezone";
|
|
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
|
import { guildPlugins } from "./plugins/availablePlugins";
|
|
import { PartialZeppelinGuildConfigSchema, ZeppelinGuildConfig } from "./types";
|
|
import { StrictValidationError, decodeAndValidateStrict } from "./validatorUtils";
|
|
|
|
const pluginNameToPlugin = new Map<string, ZeppelinPlugin>();
|
|
for (const plugin of guildPlugins) {
|
|
pluginNameToPlugin.set(plugin.name, plugin);
|
|
}
|
|
|
|
export async function validateGuildConfig(config: any): Promise<string | null> {
|
|
const validationResult = decodeAndValidateStrict(PartialZeppelinGuildConfigSchema, config);
|
|
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
|
|
|
const guildConfig = config as ZeppelinGuildConfig;
|
|
|
|
if (guildConfig.timezone) {
|
|
const validTimezones = moment.tz.names();
|
|
if (!validTimezones.includes(guildConfig.timezone)) {
|
|
return `Invalid timezone: ${guildConfig.timezone}`;
|
|
}
|
|
}
|
|
|
|
if (guildConfig.plugins) {
|
|
for (const [pluginName, pluginOptions] of Object.entries(guildConfig.plugins)) {
|
|
if (!pluginNameToPlugin.has(pluginName)) {
|
|
return `Unknown plugin: ${pluginName}`;
|
|
}
|
|
|
|
if (typeof pluginOptions !== "object" || pluginOptions == null) {
|
|
return `Invalid options specified for plugin ${pluginName}`;
|
|
}
|
|
|
|
const plugin = pluginNameToPlugin.get(pluginName)!;
|
|
const configManager = new PluginConfigManager(plugin.defaultOptions || { config: {} }, pluginOptions, {
|
|
levels: {},
|
|
parser: plugin.configParser,
|
|
});
|
|
try {
|
|
await configManager.init();
|
|
} catch (err) {
|
|
if (err instanceof ConfigValidationError || err instanceof StrictValidationError) {
|
|
return `${pluginName}: ${err.message}`;
|
|
}
|
|
|
|
throw err;
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|