diff --git a/KfChatDotNetBot/ChatBot.cs b/KfChatDotNetBot/ChatBot.cs index ba8246d..e3c8a7e 100644 --- a/KfChatDotNetBot/ChatBot.cs +++ b/KfChatDotNetBot/ChatBot.cs @@ -95,15 +95,24 @@ public class ChatBot private async Task KfPingTask() { - using var timer = new PeriodicTimer(TimeSpan.FromSeconds(10)); + var interval = (await Helpers.GetValue(BuiltIn.Keys.KiwiFarmsPingInterval)).ToType(); + using var timer = new PeriodicTimer(TimeSpan.FromSeconds(interval)); while (await timer.WaitForNextTickAsync(_cancellationToken)) { _logger.Debug("Pinging KF"); - KfClient.SendMessage("/ping"); + if (KfClient.IsConnected()) + { + KfClient.SendMessage("/ping"); + } + else + { + _logger.Info("Not pinging the connection as we're currently disconnected"); + } if (InitialStartCooldown) InitialStartCooldown = false; var inactivityTime = DateTime.UtcNow - KfClient.LastPacketReceived; _logger.Debug($"Last KF event was {inactivityTime:g} ago"); - if (inactivityTime.TotalMinutes > 10) + var inactivityTimeout = (await Helpers.GetValue(BuiltIn.Keys.KiwiFarmsInactivityTimeout)).ToType(); + if (inactivityTime.TotalSeconds > inactivityTimeout) { // Yeah, super dodgy KfClient.LastPacketReceived = DateTime.UtcNow; diff --git a/KfChatDotNetBot/Settings/BuiltIn.cs b/KfChatDotNetBot/Settings/BuiltIn.cs index b957276..7db151d 100644 --- a/KfChatDotNetBot/Settings/BuiltIn.cs +++ b/KfChatDotNetBot/Settings/BuiltIn.cs @@ -458,6 +458,27 @@ public static class BuiltIn Default = "true", IsSecret = false, CacheDuration = TimeSpan.FromHours(1) + }, + new BuiltInSettingsModel + { + Key = Keys.KiwiFarmsInactivityTimeout, + Regex = @"\d+", + // You would think the WS library would trip up with the "NoMessageReceived" exception, but there's some bug + // where it'll occasionally fail to reconnect properly and sit there dead forever, hence the watchdog timer + Description = "Length of time the client can go without receiving ANY packets from Sneedchat before forcing a reconnect.", + Default = "300", + IsSecret = false, + CacheDuration = TimeSpan.FromHours(1) + }, + new BuiltInSettingsModel + { + Key = Keys.KiwiFarmsPingInterval, + Regex = @"\d+", + Description = "Interval in seconds to ping Sneedchat using the non-existent /ping command. " + + "Note this affects how often the bot will check inactivity of the connection.", + Default = "10", + IsSecret = false, + CacheDuration = TimeSpan.FromHours(1) } ]; @@ -501,5 +522,7 @@ public static class BuiltIn public static string KiwiFarmsCookies = "KiwiFarms.Cookies"; public static string BotGmKasinoImageRotation = "Bot.GmKasinoImageRotation"; public static string TwitchShillRestreamOnCommercial = "Twitch.ShillRestreamOnCommercial"; + public static string KiwiFarmsInactivityTimeout = "KiwiFarms.InactivityTimeout"; + public static string KiwiFarmsPingInterval = "KiwiFarms.PingInterval"; } } \ No newline at end of file