Added a feature to reconnect if the bot somehow gets stuck (either not joined to a channel or stuck in a disconnected state)

This commit is contained in:
barelyprofessional
2024-06-27 11:30:41 +08:00
parent 4ece07d64d
commit 2e4616c736
2 changed files with 16 additions and 0 deletions

View File

@@ -31,6 +31,7 @@ public class KickBot
private bool _isBmjLive = false;
private bool _isBmjLiveSynced = false;
private Dictionary<string, int> _userIdMapping = new();
private DateTime _lastKfEvent = DateTime.Now;
public KickBot()
{
@@ -189,6 +190,13 @@ public class KickBot
_kfClient.SendMessage("/ping");
_kickClient.SendPusherPing();
if (_initialStartCooldown) _initialStartCooldown = false;
var inactivityTime = DateTime.Now - _lastKfEvent;
_logger.Debug($"Last KF event was {inactivityTime:g} ago");
if (inactivityTime.TotalMinutes > 10)
{
_logger.Error("Forcing reconnection as bot is completely dead");
_kfClient.Reconnect().Wait(_cancellationToken);
}
}
}
@@ -212,6 +220,7 @@ public class KickBot
private void OnKfChatMessage(object sender, List<MessageModel> messages, MessagesJsonModel jsonPayload)
{
_lastKfEvent = DateTime.Now;
_logger.Debug($"Received {messages.Count} message(s)");
foreach (var message in messages)
{
@@ -298,6 +307,7 @@ public class KickBot
private void OnUsersJoined(object sender, List<UserModel> users, UsersJsonModel jsonPayload)
{
_lastKfEvent = DateTime.Now;
_logger.Debug($"Received {users.Count} user join events");
foreach (var user in users)
{
@@ -316,6 +326,7 @@ public class KickBot
private void OnUsersParted(object sender, List<int> userIds)
{
_lastKfEvent = DateTime.Now;
if (userIds.Contains(_config.GambaSeshUserId) && _config.EnableGambaSeshDetect)
{
_logger.Info("GambaSesh is no longer present");

View File

@@ -51,6 +51,11 @@ public class ChatClient
_wsClient.Stop(WebSocketCloseStatus.NormalClosure, "Closing websocket").Wait();
}
public async Task Reconnect()
{
await _wsClient.Reconnect();
}
private async Task CreateWsClient()
{
var factory = new Func<ClientWebSocket>(() =>