From d53d2f1def9eaf27b3f8708a4804553c37a442d0 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Sat, 18 Oct 2025 19:30:49 -0500 Subject: [PATCH] Remove persistently failing mesasges from the deletion scheduled task if they get forever lost for some reason. Also moved the check for null until after it has checked the deadline so it only cares if it's due to be deleted. --- KfChatDotNetBot/ChatBot.cs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/KfChatDotNetBot/ChatBot.cs b/KfChatDotNetBot/ChatBot.cs index 94d8fe8..c4a16c8 100644 --- a/KfChatDotNetBot/ChatBot.cs +++ b/KfChatDotNetBot/ChatBot.cs @@ -183,6 +183,7 @@ public class ChatBot { var interval = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.BotScheduledDeletionInterval)).ToType(); using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(interval)); + var failures = new Dictionary(); while (await timer.WaitForNextTickAsync(_cancellationToken)) { if (!KfClient.IsConnected()) @@ -195,13 +196,27 @@ public class ChatBot var removals = new List(); foreach (var deletion in _scheduledDeletions) { + if (deletion.DeleteAt > now) continue; if (deletion.Message.ChatMessageId == null) { _logger.Error($"Can't clean up {deletion.Message.Reference} as it doesn't have a chat message ID"); + if (failures.TryGetValue(deletion.Message.Reference, out var failure)) + { + if (failure > 20) + { + removals.Add(deletion); + _logger.Error($"Giving up on {deletion.Message.Reference} and removing it from the deletion queue"); + continue; + } + + failures[deletion.Message.Reference] += 1; + } + else + { + failures[deletion.Message.Reference] = 1; + } continue; } - - if (deletion.DeleteAt > now) continue; await KfClient.DeleteMessageAsync(deletion.Message.ChatMessageId.Value); removals.Add(deletion); }