From 450f26f76309f934db9109890f0c53091ceaf101 Mon Sep 17 00:00:00 2001 From: barelyprofessional <150058423+barelyprofessional@users.noreply.github.com> Date: Sat, 7 Sep 2024 17:06:39 +0800 Subject: [PATCH] Fixed an incredibly silly bug where the bot would attempt to update the session token when the client hadn't yet been initialized, which would cause it to catch a null and get caught in a loop. This error was silent when caused by a reconnect event :( --- KfChatDotNetBot/ChatBot.cs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/KfChatDotNetBot/ChatBot.cs b/KfChatDotNetBot/ChatBot.cs index 242d283..ba8246d 100644 --- a/KfChatDotNetBot/ChatBot.cs +++ b/KfChatDotNetBot/ChatBot.cs @@ -40,10 +40,6 @@ public class ChatBot _kfTokenService = new KfTokenService(settings[BuiltIn.Keys.KiwiFarmsDomain].Value!, settings[BuiltIn.Keys.Proxy].Value, _cancellationToken); - if (_kfTokenService.GetXfSessionCookie() == null) - { - RefreshXfToken().Wait(_cancellationToken); - } KfClient = new ChatClient(new ChatClientConfigModel { @@ -53,6 +49,11 @@ public class ChatBot Proxy = settings[BuiltIn.Keys.Proxy].Value, ReconnectTimeout = settings[BuiltIn.Keys.KiwiFarmsWsReconnectTimeout].ToType() }); + + if (_kfTokenService.GetXfSessionCookie() == null) + { + RefreshXfToken().Wait(_cancellationToken); + } _logger.Debug("Creating bot command instance"); _botCommands = new BotCommands(this, _cancellationToken); @@ -127,7 +128,15 @@ public class ChatBot await Helpers.GetMultipleValues([BuiltIn.Keys.KiwiFarmsUsername, BuiltIn.Keys.KiwiFarmsPassword]); await _kfTokenService.PerformLogin(settings[BuiltIn.Keys.KiwiFarmsUsername].Value!, settings[BuiltIn.Keys.KiwiFarmsPassword].Value!); - KfClient.UpdateToken(_kfTokenService.GetXfSessionCookie()!); + var newCookie = _kfTokenService.GetXfSessionCookie(); + _logger.Debug($"GetXfSessionCookie returned => {newCookie}"); + if (newCookie == null) + { + // The bot will re-run this method continually in the event of a login issue + _logger.Error("Failed to retrieve new session cookie"); + return; + } + KfClient.UpdateToken(newCookie); await _kfTokenService.SaveCookies(); _logger.Info("Successfully logged in"); }