2019-07-11 12:23:57 +03:00
|
|
|
import * as t from "io-ts";
|
2020-07-06 01:53:58 +03:00
|
|
|
import { guildPlugins } from "./plugins/availablePlugins";
|
2019-08-04 15:44:41 +03:00
|
|
|
import { decodeAndValidateStrict, StrictValidationError } from "./validatorUtils";
|
2020-07-06 02:08:11 +03:00
|
|
|
import { ZeppelinPlugin } from "./plugins/ZeppelinPlugin";
|
2020-08-10 00:24:06 +03:00
|
|
|
import { PartialZeppelinGuildConfigSchema, ZeppelinGuildConfig } from "./types";
|
2020-07-30 20:40:00 +03:00
|
|
|
import { configUtils, ConfigValidationError, PluginOptions } from "knub";
|
2020-08-10 00:24:06 +03:00
|
|
|
import moment from "moment-timezone";
|
2020-07-06 02:08:11 +03:00
|
|
|
|
|
|
|
const pluginNameToPlugin = new Map<string, ZeppelinPlugin>();
|
|
|
|
for (const plugin of guildPlugins) {
|
2020-07-22 22:56:21 +03:00
|
|
|
pluginNameToPlugin.set(plugin.name, plugin);
|
2019-07-11 12:23:57 +03:00
|
|
|
}
|
|
|
|
|
2020-07-30 20:40:00 +03:00
|
|
|
export async function validateGuildConfig(config: any): Promise<string | null> {
|
2020-08-10 00:24:06 +03:00
|
|
|
const validationResult = decodeAndValidateStrict(PartialZeppelinGuildConfigSchema, config);
|
2019-08-04 15:44:41 +03:00
|
|
|
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
2019-07-11 12:23:57 +03:00
|
|
|
|
2020-08-10 00:24:06 +03:00
|
|
|
const guildConfig = config as ZeppelinGuildConfig;
|
|
|
|
|
|
|
|
if (guildConfig.timezone) {
|
|
|
|
const validTimezones = moment.tz.names();
|
|
|
|
if (!validTimezones.includes(guildConfig.timezone)) {
|
|
|
|
return `Invalid timezone: ${guildConfig.timezone}`;
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 02:08:11 +03:00
|
|
|
|
|
|
|
if (guildConfig.plugins) {
|
|
|
|
for (const [pluginName, pluginOptions] of Object.entries(guildConfig.plugins)) {
|
|
|
|
if (!pluginNameToPlugin.has(pluginName)) {
|
2020-07-30 20:40:00 +03:00
|
|
|
return `Unknown plugin: ${pluginName}`;
|
2019-07-11 12:23:57 +03:00
|
|
|
}
|
|
|
|
|
2020-08-06 01:21:03 +03:00
|
|
|
if (typeof pluginOptions !== "object" || pluginOptions == null) {
|
|
|
|
return `Invalid options specified for plugin ${pluginName}`;
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const plugin = pluginNameToPlugin.get(pluginName)!;
|
2020-07-22 22:56:21 +03:00
|
|
|
try {
|
2020-07-28 23:28:26 +03:00
|
|
|
const mergedOptions = configUtils.mergeConfig(plugin.defaultOptions || {}, pluginOptions);
|
2021-05-23 18:10:23 +03:00
|
|
|
await plugin.configPreprocessor?.((mergedOptions as unknown) as PluginOptions<any>, true);
|
2020-07-22 22:56:21 +03:00
|
|
|
} catch (err) {
|
2020-08-10 02:22:56 +03:00
|
|
|
if (err instanceof ConfigValidationError || err instanceof StrictValidationError) {
|
|
|
|
return `${pluginName}: ${err.message}`;
|
2020-07-22 22:56:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
2019-07-11 12:23:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|