From dd9c52bc43e50bc9c49a4a4b36b77ec9a147feec Mon Sep 17 00:00:00 2001 From: Nils <7890309+DarkView@users.noreply.github.com> Date: Wed, 28 Apr 2021 22:18:23 +0200 Subject: [PATCH] Add automod action for setting slowmode (#167) --- .../Automod/actions/availableActions.ts | 3 ++ .../plugins/Automod/actions/setSlowmode.ts | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 backend/src/plugins/Automod/actions/setSlowmode.ts diff --git a/backend/src/plugins/Automod/actions/availableActions.ts b/backend/src/plugins/Automod/actions/availableActions.ts index 7857db6f..54146de3 100644 --- a/backend/src/plugins/Automod/actions/availableActions.ts +++ b/backend/src/plugins/Automod/actions/availableActions.ts @@ -14,6 +14,7 @@ import { SetAntiraidLevelAction } from "./setAntiraidLevel"; import { ReplyAction } from "./reply"; import { AddToCounterAction } from "./addToCounter"; import { SetCounterAction } from "./setCounter"; +import { SetSlowmodeAction } from "./setSlowmode"; export const availableActions: Record> = { clean: CleanAction, @@ -30,6 +31,7 @@ export const availableActions: Record> = { reply: ReplyAction, add_to_counter: AddToCounterAction, set_counter: SetCounterAction, + set_slowmode: SetSlowmodeAction, }; export const AvailableActions = t.type({ @@ -47,4 +49,5 @@ export const AvailableActions = t.type({ reply: ReplyAction.configType, add_to_counter: AddToCounterAction.configType, set_counter: SetCounterAction.configType, + set_slowmode: SetSlowmodeAction.configType, }); diff --git a/backend/src/plugins/Automod/actions/setSlowmode.ts b/backend/src/plugins/Automod/actions/setSlowmode.ts new file mode 100644 index 00000000..6efc7b4f --- /dev/null +++ b/backend/src/plugins/Automod/actions/setSlowmode.ts @@ -0,0 +1,53 @@ +import * as t from "io-ts"; +import { automodAction } from "../helpers"; +import { convertDelayStringToMS, isDiscordRESTError, tDelayString, tNullable } from "../../../utils"; +import { LogType } from "../../../data/LogType"; +import { AnyGuildChannel } from "eris"; + +export const SetSlowmodeAction = automodAction({ + configType: t.type({ + channels: t.array(t.string), + duration: tNullable(tDelayString), + }), + + defaultConfig: { + duration: "10s", + }, + + async apply({ pluginData, actionConfig }) { + const duration = actionConfig.duration ? convertDelayStringToMS(actionConfig.duration)! : 0; + + for (const channelId of actionConfig.channels) { + const channel = pluginData.guild.channels.get(channelId); + // 0 = Guild Text, 4 = Guild Category - Both dont allow slowmode + if (!channel) continue; + if (!(channel.type === 0 || channel.type === 4)) continue; + + let channelsToSlowmode: AnyGuildChannel[] = []; + if (channel.type === 4) { + channelsToSlowmode = pluginData.guild.channels.filter(ch => ch.parentID === channel.id && ch.type === 0); + } else { + channelsToSlowmode.push(channel); + } + + try { + for (const chan of channelsToSlowmode) { + await chan.edit({ + rateLimitPerUser: duration / 1000, // ms -> seconds + }); + } + } catch (e) { + let errorMessage = e; + + // Check for invalid form body -> indicates duration was too large + if (isDiscordRESTError(e) && e.code === 50035) { + errorMessage = `Duration is greater than maximum native slowmode duration`; + } + + pluginData.state.logs.log(LogType.BOT_ALERT, { + body: `Unable to set slowmode for channel ${channel.id} to ${duration / 1000} seconds: ${errorMessage}`, + }); + } + } + }, +});