automod: simplify preprocessStaticConfig

This commit is contained in:
Dragory 2019-11-28 18:34:48 +02:00
parent f8444c1a3d
commit b0df86692f

View file

@ -12,6 +12,7 @@ import {
noop, noop,
SECONDS, SECONDS,
stripObjectToScalars, stripObjectToScalars,
tDeepPartial,
tNullable, tNullable,
UnknownUser, UnknownUser,
verboseChannelMention, verboseChannelMention,
@ -297,6 +298,8 @@ const ConfigSchema = t.type({
}); });
type TConfigSchema = t.TypeOf<typeof ConfigSchema>; type TConfigSchema = t.TypeOf<typeof ConfigSchema>;
const PartialConfigSchema = tDeepPartial(ConfigSchema);
/** /**
* DEFAULTS * DEFAULTS
*/ */
@ -499,12 +502,10 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema> {
protected archives: GuildArchives; protected archives: GuildArchives;
protected guildLogs: GuildLogs; protected guildLogs: GuildLogs;
protected static preprocessStaticConfig(config) { protected static preprocessStaticConfig(config: t.TypeOf<typeof PartialConfigSchema>) {
if (config.rules && typeof config.rules === "object") { if (config.rules) {
// Loop through each rule // Loop through each rule
for (const [name, rule] of Object.entries(config.rules)) { for (const [name, rule] of Object.entries(config.rules)) {
if (rule == null || typeof rule !== "object") continue;
rule["name"] = name; rule["name"] = name;
// If the rule doesn't have an explicitly set "enabled" property, set it to true // If the rule doesn't have an explicitly set "enabled" property, set it to true
@ -513,12 +514,11 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema> {
} }
// Loop through the rule's triggers // Loop through the rule's triggers
if (rule["triggers"] != null && Array.isArray(rule["triggers"])) { if (rule["triggers"]) {
for (const trigger of rule["triggers"]) { for (const trigger of rule["triggers"]) {
if (trigger == null || typeof trigger !== "object") continue;
// Apply default config to the triggers used in this rule // Apply default config to the triggers used in this rule
for (const [defaultTriggerName, defaultTrigger] of Object.entries(defaultTriggers)) { for (const [defaultTriggerName, defaultTrigger] of Object.entries(defaultTriggers)) {
if (trigger[defaultTriggerName] && typeof trigger[defaultTriggerName] === "object") { if (trigger[defaultTriggerName]) {
trigger[defaultTriggerName] = configUtils.mergeConfig({}, defaultTrigger, trigger[defaultTriggerName]); trigger[defaultTriggerName] = configUtils.mergeConfig({}, defaultTrigger, trigger[defaultTriggerName]);
} }
} }
@ -526,7 +526,7 @@ export class AutomodPlugin extends ZeppelinPlugin<TConfigSchema> {
} }
// Enable logging of automod actions by default // Enable logging of automod actions by default
if (rule["actions"] && typeof rule["actions"] === "object") { if (rule["actions"]) {
if (rule["actions"]["log"] == null) { if (rule["actions"]["log"] == null) {
rule["actions"]["log"] = true; rule["actions"]["log"] = true;
} }