From 9c2c4965ba86a275526b9b4160a2ab78ad079d5f Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Fri, 30 Aug 2024 04:47:58 +0100 Subject: [PATCH] have the bot cleanup chat history after 7 mins of not talking to the bot --- SecondBotEvents/Services/ChatGptService.cs | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/SecondBotEvents/Services/ChatGptService.cs b/SecondBotEvents/Services/ChatGptService.cs index e647978..d06f7fc 100644 --- a/SecondBotEvents/Services/ChatGptService.cs +++ b/SecondBotEvents/Services/ChatGptService.cs @@ -197,7 +197,34 @@ namespace SecondBotEvents.Services } lastUpkeep = SecondbotHelpers.UnixTimeNow(); // check for expired chat windows (longer than 2 mins from last message) - + lock (ChatHistoryLastAccessed) lock (chatHistoryAI) lock (ChatRateLimiter) + { + List needcleaning = new List(); + long now = SecondbotHelpers.UnixTimeNow(); + foreach (KeyValuePair entry in ChatHistoryLastAccessed) + { + dif = now - entry.Value; + if(dif > (7*60)) + { + needcleaning.Add(entry.Key); + } + } + foreach(UUID a in needcleaning) + { + if(chatHistoryAI.ContainsKey(a)) + { + chatHistoryAI.Remove(a); + } + if(ChatHistoryLastAccessed.ContainsKey(a)) + { + ChatHistoryLastAccessed.Remove(a); + } + if (ChatRateLimiter.ContainsKey(a)) + { + ChatRateLimiter.Remove(a); + } + } + } } protected async void GetAiReply(int ratelimiter, UUID replyTo, UUID user, string name, string message, bool avatarchat = false, bool groupchat = false)