2020-07-28 21:34:01 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { LogType } from "../../../data/LogType";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
|
2021-06-08 02:23:30 +02:00
|
|
|
import { convertDelayStringToMS, nonNullish, tDelayString, tNullable, unique } from "../../../utils";
|
2021-04-02 19:36:40 +03:00
|
|
|
import { CaseArgs } from "../../Cases/types";
|
2021-06-06 23:51:32 +02:00
|
|
|
import { LogsPlugin } from "../../Logs/LogsPlugin";
|
|
|
|
import { MutesPlugin } from "../../Mutes/MutesPlugin";
|
|
|
|
import { resolveActionContactMethods } from "../functions/resolveActionContactMethods";
|
|
|
|
import { automodAction } from "../helpers";
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
export const MuteAction = automodAction({
|
|
|
|
configType: t.type({
|
|
|
|
reason: tNullable(t.string),
|
|
|
|
duration: tNullable(tDelayString),
|
|
|
|
notify: tNullable(t.string),
|
|
|
|
notifyChannel: tNullable(t.string),
|
2021-02-13 19:04:40 +01:00
|
|
|
remove_roles_on_mute: tNullable(t.union([t.boolean, t.array(t.string)])),
|
|
|
|
restore_roles_on_mute: tNullable(t.union([t.boolean, t.array(t.string)])),
|
2021-04-28 22:39:49 +03:00
|
|
|
postInCaseLog: tNullable(t.boolean),
|
2021-05-03 19:40:59 +03:00
|
|
|
hide_case: tNullable(t.boolean),
|
2020-07-28 21:34:01 +03:00
|
|
|
}),
|
|
|
|
|
2020-07-30 01:45:14 +03:00
|
|
|
defaultConfig: {
|
|
|
|
notify: null, // Use defaults from ModActions
|
2021-05-03 18:49:52 +03:00
|
|
|
hide_case: false,
|
2020-07-30 01:45:14 +03:00
|
|
|
},
|
|
|
|
|
2020-09-13 22:45:02 +03:00
|
|
|
async apply({ pluginData, contexts, actionConfig, ruleName, matchResult }) {
|
2020-11-09 20:03:57 +02:00
|
|
|
const duration = actionConfig.duration ? convertDelayStringToMS(actionConfig.duration)! : undefined;
|
2020-07-28 21:34:01 +03:00
|
|
|
const reason = actionConfig.reason || "Muted automatically";
|
2020-12-03 16:47:11 +02:00
|
|
|
const contactMethods = actionConfig.notify ? resolveActionContactMethods(pluginData, actionConfig) : undefined;
|
2021-02-13 19:04:40 +01:00
|
|
|
const rolesToRemove = actionConfig.remove_roles_on_mute;
|
|
|
|
const rolesToRestore = actionConfig.restore_roles_on_mute;
|
2020-07-28 21:34:01 +03:00
|
|
|
|
2021-04-02 19:36:40 +03:00
|
|
|
const caseArgs: Partial<CaseArgs> = {
|
2021-06-01 02:05:55 +02:00
|
|
|
modId: pluginData.client.user!.id,
|
2020-11-09 20:03:57 +02:00
|
|
|
extraNotes: matchResult.fullSummary ? [matchResult.fullSummary] : [],
|
2021-04-02 19:36:40 +03:00
|
|
|
automatic: true,
|
2021-04-28 22:39:49 +03:00
|
|
|
postInCaseLogOverride: actionConfig.postInCaseLog ?? undefined,
|
2021-05-03 19:40:59 +03:00
|
|
|
hide: Boolean(actionConfig.hide_case),
|
2020-07-28 21:34:01 +03:00
|
|
|
};
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
const userIdsToMute = unique(contexts.map(c => c.user?.id).filter(nonNullish));
|
2020-07-28 21:34:01 +03:00
|
|
|
|
|
|
|
const mutes = pluginData.getPlugin(MutesPlugin);
|
|
|
|
for (const userId of userIdsToMute) {
|
2020-09-13 22:45:02 +03:00
|
|
|
try {
|
2021-04-28 21:06:33 +02:00
|
|
|
await mutes.muteUser(
|
|
|
|
userId,
|
|
|
|
duration,
|
|
|
|
reason,
|
|
|
|
{ contactMethods, caseArgs, isAutomodAction: true },
|
|
|
|
rolesToRemove,
|
|
|
|
rolesToRestore,
|
|
|
|
);
|
2020-09-13 22:45:02 +03:00
|
|
|
} catch (e) {
|
|
|
|
if (e instanceof RecoverablePluginError && e.code === ERRORS.NO_MUTE_ROLE_IN_CONFIG) {
|
|
|
|
pluginData.getPlugin(LogsPlugin).log(LogType.BOT_ALERT, {
|
|
|
|
body: `Failed to mute <@!${userId}> in Automod rule \`${ruleName}\` because a mute role has not been specified in server config`,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 21:34:01 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|