From 0da204ed5a0fac04b3a99e884038e4a12c1f9f0c Mon Sep 17 00:00:00 2001 From: Tiago R Date: Sat, 1 Jul 2023 11:47:56 +0100 Subject: [PATCH] Fix: Utility-Clean command increased limit & chunk messages to be deleted (#393) Co-authored-by: Almeida --- backend/src/plugins/Utility/commands/CleanCmd.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/backend/src/plugins/Utility/commands/CleanCmd.ts b/backend/src/plugins/Utility/commands/CleanCmd.ts index d42cb5ad..11da9129 100644 --- a/backend/src/plugins/Utility/commands/CleanCmd.ts +++ b/backend/src/plugins/Utility/commands/CleanCmd.ts @@ -6,12 +6,15 @@ import { LogType } from "../../../data/LogType"; import { SavedMessage } from "../../../data/entities/SavedMessage"; import { humanizeDurationShort } from "../../../humanizeDurationShort"; import { getBaseUrl, sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils"; +import { allowTimeout } from "../../../RegExpRunner"; +import { chunkArray, DAYS, getInviteCodesInString, noop, SECONDS } from "../../../utils"; +import { utilityCmd, UtilityPluginType } from "../types"; import { ModActionsPlugin } from "../../../plugins/ModActions/ModActionsPlugin"; import { DAYS, SECONDS, getInviteCodesInString, noop } from "../../../utils"; import { LogsPlugin } from "../../Logs/LogsPlugin"; import { UtilityPluginType, utilityCmd } from "../types"; -const MAX_CLEAN_COUNT = 150; +const MAX_CLEAN_COUNT = 300; const MAX_CLEAN_TIME = 1 * DAYS; const MAX_CLEAN_API_REQUESTS = 20; const CLEAN_COMMAND_DELETE_DELAY = 10 * SECONDS; @@ -33,9 +36,14 @@ export async function cleanMessages( idsToDelete.forEach((id) => pluginData.state.logs.ignoreLog(LogType.MESSAGE_DELETE, id)); pluginData.state.logs.ignoreLog(LogType.MESSAGE_DELETE_BULK, idsToDelete[0]); - // Actually delete the messages - channel.bulkDelete(idsToDelete); - await pluginData.state.savedMessages.markBulkAsDeleted(idsToDelete); + // Actually delete the messages (in chunks of 100) + + const chunks = chunkArray(idsToDelete, 100); + await Promise.all( + chunks.map((chunk) => + Promise.all([channel.bulkDelete(chunk), pluginData.state.savedMessages.markBulkAsDeleted(chunk)]), + ), + ); // Create an archive const archiveId = await pluginData.state.archives.createFromSavedMessages(savedMessages, pluginData.guild);