3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-21 16:55:03 +00:00

Add automod action for setting slowmode

This commit is contained in:
Dark 2021-03-18 15:57:45 +01:00
parent fdaf386193
commit db3d1d7c3b
No known key found for this signature in database
GPG key ID: 384C4B4F5B1E25A8
2 changed files with 45 additions and 0 deletions

View file

@ -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<string, AutomodActionBlueprint<any>> = {
clean: CleanAction,
@ -30,6 +31,7 @@ export const availableActions: Record<string, AutomodActionBlueprint<any>> = {
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,
});

View file

@ -0,0 +1,42 @@
import * as t from "io-ts";
import { automodAction } from "../helpers";
import { convertDelayStringToMS, isDiscordRESTError, tDelayString, tNullable } from "../../../utils";
import { LogType } from "../../../data/LogType";
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);
// 2 = Guild Voice, 5 = Guild News - Both dont allow slowmode
if (!channel || channel.type === 2 || channel.type === 5) continue;
try {
await channel.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}`,
});
}
}
},
});