From 99f5421736c15cec02c77fc36e1fd4e8450126ef Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Sun, 12 Oct 2025 12:54:23 -0500 Subject: [PATCH] Changed auto deletions to a background task running in the bot itself to hopefully make them reliable --- KfChatDotNetBot/ChatBot.cs | 67 +++++++++++++++++++++-------- KfChatDotNetBot/Settings/BuiltIn.cs | 9 ++++ 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/KfChatDotNetBot/ChatBot.cs b/KfChatDotNetBot/ChatBot.cs index 03f14eb..1f2eea4 100644 --- a/KfChatDotNetBot/ChatBot.cs +++ b/KfChatDotNetBot/ChatBot.cs @@ -34,6 +34,8 @@ public class ChatBot private int _joinFailures = 0; private Task _kfDeadBotDetection; private DateTime _lastReconnectAttempt = DateTime.UtcNow; + private List _scheduledDeletions; + private Task _scheduledAutoDeleteTask; public ChatBot() { @@ -87,6 +89,8 @@ public class ChatBot _logger.Debug("Creating ping task"); _kfChatPing = KfPingTask(); + _logger.Debug("Creating scheduled auto deletion task"); + _scheduledAutoDeleteTask = ScheduledDeletionTask(); _logger.Debug("Blocking the main thread"); var exitEvent = new ManualResetEvent(false); @@ -175,6 +179,40 @@ public class ChatBot } } + private async Task ScheduledDeletionTask() + { + var interval = (await SettingsProvider.GetValueAsync(BuiltIn.Keys.BotScheduledDeletionInterval)).ToType(); + using var timer = new PeriodicTimer(TimeSpan.FromMilliseconds(interval)); + while (await timer.WaitForNextTickAsync(_cancellationToken)) + { + if (!KfClient.IsConnected()) + { + _logger.Info("Not cleaning scheduled deletions up as we're disconnected"); + continue; + } + + var now = DateTimeOffset.UtcNow; + var removals = new List(); + foreach (var deletion in _scheduledDeletions) + { + if (deletion.Message.ChatMessageId == null) + { + _logger.Error($"Can't clean up {deletion.Message.Reference} as it doesn't have a chat message ID"); + continue; + } + + if (deletion.DeleteAt < now) continue; + await KfClient.DeleteMessageAsync(deletion.Message.ChatMessageId.Value); + removals.Add(deletion); + } + _logger.Info($"Cleaned up {removals.Count} messages"); + foreach (var removal in removals) + { + _scheduledDeletions.Remove(removal); + } + } + } + private async Task RefreshXfToken() { try @@ -416,7 +454,7 @@ public class ChatBot await KfClient.SendMessageInstantAsync(messageTracker.Message); if (autoDeleteAfter != null) { - _ = SendChatMessageAsyncAutoDeleteTask(messageTracker, autoDeleteAfter.Value); + ScheduleMessageAutoDelete(messageTracker, autoDeleteAfter.Value); } return messageTracker; } @@ -429,24 +467,11 @@ public class ChatBot /// When you want it deleted public void ScheduleMessageAutoDelete(SentMessageTrackerModel message, TimeSpan deleteAfter) { - _ = Task.Run(() => SendChatMessageAsyncAutoDeleteTask(message, deleteAfter), _cancellationToken); - } - - private async Task SendChatMessageAsyncAutoDeleteTask(SentMessageTrackerModel message, TimeSpan deleteAfter) - { - var i = 0; - while (message.ChatMessageId == null) + _scheduledDeletions.Add(new ScheduledAutoDeleteModel { - i++; - await Task.Delay(100, _cancellationToken); - if (message.Status is SentMessageTrackerStatus.NotSending or SentMessageTrackerStatus.Lost) return; - if (i <= 120) continue; - _logger.Error($"Gave up waiting for message with content '{message.Message}'"); - return; - } - - await Task.Delay(deleteAfter, _cancellationToken); - await KfClient.DeleteMessageAsync(message.ChatMessageId.Value); + Message = message, + DeleteAt = DateTimeOffset.UtcNow.Add(deleteAfter) + }); } /// @@ -610,4 +635,10 @@ public class ChatBot // Try to send the message anyway, even though Sneedchat will just silently eat it DoNothing } + + private class ScheduledAutoDeleteModel + { + public required SentMessageTrackerModel Message { get; set; } + public required DateTimeOffset DeleteAt { get; set; } + } } \ No newline at end of file diff --git a/KfChatDotNetBot/Settings/BuiltIn.cs b/KfChatDotNetBot/Settings/BuiltIn.cs index 6cd2678..6fe8247 100644 --- a/KfChatDotNetBot/Settings/BuiltIn.cs +++ b/KfChatDotNetBot/Settings/BuiltIn.cs @@ -1115,6 +1115,14 @@ public static class BuiltIn Default = "60000", ValueType = SettingValueType.Text, Regex = WholeNumberRegex + }, + new BuiltInSettingsModel + { + Key = Keys.BotScheduledDeletionInterval, + Description = "Delay in milliseconds between each check to see whether there's messages to be deleted", + Default = "1000", + ValueType = SettingValueType.Text, + Regex = WholeNumberRegex } ]; @@ -1244,5 +1252,6 @@ public static class BuiltIn public static string KasinoGuessWhatNumberCleanupDelay = "Kasino.GuessWhatNumber.CleanupDelay"; public static string KasinoKenoCleanupDelay = "Kasino.Keno.CleanupDelay"; public static string KasinoPlanesCleanupDelay = "Kasino.Planes.CleanupDelay"; + public static string BotScheduledDeletionInterval = "Bot.ScheduledDeletionInterval"; } } \ No newline at end of file