2020-07-27 20:42:10 +03:00
|
|
|
import * as t from "io-ts";
|
|
|
|
import { automodAction } from "../helpers";
|
|
|
|
import { LogType } from "../../../data/LogType";
|
2020-07-30 20:40:57 +03:00
|
|
|
import { noop } from "../../../utils";
|
2020-07-27 20:42:10 +03:00
|
|
|
|
|
|
|
export const CleanAction = automodAction({
|
|
|
|
configType: t.boolean,
|
2020-07-30 01:45:14 +03:00
|
|
|
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-11-09 20:03:57 +02:00
|
|
|
if (messageIdsToDeleteByChannelId.get(context.message.channel_id)!.includes(context.message.id)) {
|
2020-07-30 20:15:35 +03:00
|
|
|
console.warn(`Message ID to delete was already present: ${pluginData.guild.name}, rule ${ruleName}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-09 20:03:57 +02:00
|
|
|
messageIdsToDeleteByChannelId.get(context.message.channel_id)!.push(context.message.id);
|
2020-07-27 20:42:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const [channelId, messageIds] of messageIdsToDeleteByChannelId.entries()) {
|
|
|
|
for (const id of messageIds) {
|
|
|
|
pluginData.state.logs.ignoreLog(LogType.MESSAGE_DELETE, id);
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:40:57 +03:00
|
|
|
await pluginData.client.deleteMessages(channelId, messageIds).catch(noop);
|
2020-07-27 20:42:10 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|