3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-10 12:25:02 +00:00
zeppelin/backend/src/plugins/Automod/actions/setSlowmode.ts
metal 8347095fa0
channel typings
Signed-off-by: GitHub <noreply@github.com>
2023-03-11 12:43:04 +00:00

62 lines
2.2 KiB
TypeScript

import { ChannelType, Snowflake, TextChannel } from "discord.js";
import * as t from "io-ts";
import { LogType } from "../../../data/LogType";
import { convertDelayStringToMS, isDiscordAPIError, tDelayString, tNullable } from "../../../utils";
import { automodAction } from "../helpers";
import { LogsPlugin } from "../../Logs/LogsPlugin";
export const SetSlowmodeAction = automodAction({
configType: t.type({
channels: t.array(t.string),
duration: tNullable(tDelayString),
}),
defaultConfig: {
duration: "10s",
},
async apply({ pluginData, actionConfig }) {
const slowmodeMs = Math.max(actionConfig.duration ? convertDelayStringToMS(actionConfig.duration)! : 0, 0);
for (const channelId of actionConfig.channels) {
const channel = pluginData.guild.channels.cache.get(channelId as Snowflake);
// Only text channels and text channels within categories support slowmodes
if (!channel || !(channel.type === ChannelType.GuildText || ChannelType.GuildCategory)) {
continue;
}
const channelsToSlowmode: TextChannel[] = [];
if (channel.type === ChannelType.GuildCategory) {
// Find all text channels within the category
for (const ch of pluginData.guild.channels.cache.values()) {
if (ch.parentId === channel.id && ch.type === ChannelType.GuildText) {
channelsToSlowmode.push(ch as TextChannel);
}
}
} else {
channelsToSlowmode.push(channel as TextChannel);
}
const slowmodeSeconds = Math.ceil(slowmodeMs / 1000);
try {
for (const chan of channelsToSlowmode) {
await chan.edit({
rateLimitPerUser: slowmodeSeconds,
});
}
} catch (e) {
// Check for invalid form body -> indicates duration was too large
const errorMessage =
isDiscordAPIError(e) && e.code === 50035
? `Duration is greater than maximum native slowmode duration`
: e.message;
pluginData.getPlugin(LogsPlugin).logBotAlert({
body: `Unable to set slowmode for channel ${channel.id} to ${slowmodeSeconds} seconds: ${errorMessage}`,
});
}
}
},
});