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";
|
|
|
|
import { IZeppelinGuildConfig } from "./types";
|
2020-07-28 23:28:26 +03:00
|
|
|
import { configUtils, PluginOptions } from "knub";
|
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
|
|
|
}
|
|
|
|
|
|
|
|
const guildConfigRootSchema = t.type({
|
|
|
|
prefix: t.string,
|
|
|
|
levels: t.record(t.string, t.number),
|
2020-01-11 00:39:32 +11:00
|
|
|
success_emoji: t.string,
|
2019-07-11 12:23:57 +03:00
|
|
|
plugins: t.record(t.string, t.unknown),
|
|
|
|
});
|
2019-07-22 13:09:05 +03:00
|
|
|
const partialGuildConfigRootSchema = t.partial(guildConfigRootSchema.props);
|
2019-07-11 12:23:57 +03:00
|
|
|
|
|
|
|
const globalConfigRootSchema = t.type({
|
|
|
|
url: t.string,
|
|
|
|
owners: t.array(t.string),
|
|
|
|
plugins: t.record(t.string, t.unknown),
|
|
|
|
});
|
|
|
|
|
|
|
|
const partialMegaTest = t.partial({ name: t.string });
|
|
|
|
|
2020-07-28 23:28:26 +03:00
|
|
|
export async function validateGuildConfig(config: any): Promise<string[] | null> {
|
2019-08-04 15:44:41 +03:00
|
|
|
const validationResult = decodeAndValidateStrict(partialGuildConfigRootSchema, config);
|
|
|
|
if (validationResult instanceof StrictValidationError) return validationResult.getErrors();
|
2019-07-11 12:23:57 +03:00
|
|
|
|
2020-07-06 02:08:11 +03:00
|
|
|
const guildConfig = config as IZeppelinGuildConfig;
|
|
|
|
|
|
|
|
if (guildConfig.plugins) {
|
|
|
|
for (const [pluginName, pluginOptions] of Object.entries(guildConfig.plugins)) {
|
|
|
|
if (!pluginNameToPlugin.has(pluginName)) {
|
2019-07-11 12:23:57 +03:00
|
|
|
return [`Unknown plugin: ${pluginName}`];
|
|
|
|
}
|
|
|
|
|
2020-07-06 02:08:11 +03: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);
|
|
|
|
await plugin.configPreprocessor(mergedOptions as PluginOptions<any>);
|
2020-07-22 22:56:21 +03:00
|
|
|
} catch (err) {
|
|
|
|
if (err instanceof StrictValidationError) {
|
|
|
|
return err.getErrors().map(err => {
|
|
|
|
return `${pluginName}: ${err.toString()}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
throw err;
|
2019-07-11 12:23:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|