zappyzep/backend/src/plugins/Automod/actions/clean.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-07-27 20:42:10 +03:00
import * as t from "io-ts";
import { automodAction } from "../helpers";
import { LogType } from "../../../data/LogType";
import { noop } from "../../../utils";
2020-07-27 20:42:10 +03:00
export const CleanAction = automodAction({
configType: t.boolean,
defaultConfig: false,
2020-07-27 20:42:10 +03:00
2020-07-30 20:15:35 +03:00
async apply({ pluginData, contexts, ruleName }) {
2020-07-27 20:42:10 +03:00
const messageIdsToDeleteByChannelId: Map<string, string[]> = new Map();
for (const context of contexts) {
if (context.message) {
if (!messageIdsToDeleteByChannelId.has(context.message.channel_id)) {
messageIdsToDeleteByChannelId.set(context.message.channel_id, []);
}
2020-07-30 20:15:35 +03:00
if (messageIdsToDeleteByChannelId.get(context.message.channel_id).includes(context.message.id)) {
console.warn(`Message ID to delete was already present: ${pluginData.guild.name}, rule ${ruleName}`);
continue;
}
2020-07-27 20:42:10 +03:00
messageIdsToDeleteByChannelId.get(context.message.channel_id).push(context.message.id);
}
}
for (const [channelId, messageIds] of messageIdsToDeleteByChannelId.entries()) {
for (const id of messageIds) {
pluginData.state.logs.ignoreLog(LogType.MESSAGE_DELETE, id);
}
await pluginData.client.deleteMessages(channelId, messageIds).catch(noop);
2020-07-27 20:42:10 +03:00
}
},
});