3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-05-21 08:45:03 +00:00
zeppelin/backend/src/plugins/Slowmode/util/clearExpiredSlowmodes.ts
Dark d24aea7c5c Start move to configAccessibleObjects, exclude perm overrides from logs
configAccessibleObjects are used to guarantee backwards compatibility and consistency.
Perm overrides from our own plugins are ignored as to not spam logs through bot managed slowmode or companion channels
2021-07-06 05:23:47 +02:00

34 lines
1.5 KiB
TypeScript

import { GuildChannel, Snowflake, TextChannel } from "discord.js";
import { GuildPluginData } from "knub";
import { channelToConfigAccessibleChannel, userToConfigAccessibleUser } from "src/utils/configAccessibleObjects";
import { LogType } from "../../../data/LogType";
import { logger } from "../../../logger";
import { stripObjectToScalars, UnknownUser } from "../../../utils";
import { SlowmodePluginType } from "../types";
import { clearBotSlowmodeFromUserId } from "./clearBotSlowmodeFromUserId";
export async function clearExpiredSlowmodes(pluginData: GuildPluginData<SlowmodePluginType>) {
const expiredSlowmodeUsers = await pluginData.state.slowmodes.getExpiredSlowmodeUsers();
for (const user of expiredSlowmodeUsers) {
const channel = pluginData.guild.channels.cache.get(user.channel_id as Snowflake);
if (!channel) {
await pluginData.state.slowmodes.clearSlowmodeUser(user.channel_id, user.user_id);
continue;
}
try {
await clearBotSlowmodeFromUserId(pluginData, channel as GuildChannel & TextChannel, user.user_id);
} catch (e) {
logger.error(e);
const realUser =
pluginData.client.users!.fetch(user.user_id as Snowflake) || new UnknownUser({ id: user.user_id });
pluginData.state.logs.log(LogType.BOT_ALERT, {
body: `Failed to clear slowmode permissions from {userMention(user)} in {channelMention(channel)}`,
user: userToConfigAccessibleUser(await realUser),
channel: channelToConfigAccessibleChannel(channel),
});
}
}
}