From 0afffb9adc67f2bd5d2b5a062c25829d7ba0e3f5 Mon Sep 17 00:00:00 2001 From: Madpeter <57852827+Madpeterz@users.noreply.github.com> Date: Thu, 29 Aug 2024 10:12:17 +0100 Subject: [PATCH] tweak discord reconnect to reattach to the SL client if discord dies but SL lives --- SecondBotEvents/Services/BotClientService.cs | 10 ++++++++ SecondBotEvents/Services/DiscordService.cs | 24 +++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/SecondBotEvents/Services/BotClientService.cs b/SecondBotEvents/Services/BotClientService.cs index 30a60e0..1049584 100644 --- a/SecondBotEvents/Services/BotClientService.cs +++ b/SecondBotEvents/Services/BotClientService.cs @@ -19,6 +19,16 @@ namespace SecondBotEvents.Services LogoutExpected = true; } + public bool IsConnected() + { + if (client == null) { return false; } + else if (client.Network == null) { return false; } + else if (client.Network.Connected == false) { return false; } + else if (client.Network.CurrentSim == null) { return false; } + else if (client.Network.CurrentSim.Connected == false) { return false; } + return true; + } + protected bool ExitBot = false; protected Timer AutoRestartLoginTimer; diff --git a/SecondBotEvents/Services/DiscordService.cs b/SecondBotEvents/Services/DiscordService.cs index 76f10af..f7213cf 100644 --- a/SecondBotEvents/Services/DiscordService.cs +++ b/SecondBotEvents/Services/DiscordService.cs @@ -561,6 +561,11 @@ namespace SecondBotEvents.Services LogFormater.Info("Discord service [Starting]"); master.SystemStatusMessagesEvent += SystemStatusEvent; master.BotClientNoticeEvent += BotClientRestart; + if(master.BotClient.IsConnected() == true) + { + BotLoggedIn(this, new SimConnectedEventArgs(GetClient().Network.CurrentSim)); + } + var config = new DiscordSocketConfig() { GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildIntegrations | GatewayIntents.MessageContent @@ -595,11 +600,11 @@ namespace SecondBotEvents.Services LogFormater.Info("Discord service [Stopping]"); } running = false; - master.BotClientNoticeEvent -= BotClientRestart; master.SystemStatusMessagesEvent -= SystemStatusEvent; AcceptEventsFromSL = false; - if(DiscordClient != null) + BotLoggedOut(this, new LoggedOutEventArgs(new List()); + if (DiscordClient != null) { DiscordClient.Dispose(); DiscordClient = null; @@ -631,7 +636,7 @@ namespace SecondBotEvents.Services { if (e.isRestart == false) { - LogFormater.Info("Discord service [Avi link]"); + LogFormater.Info("Discord service [Avi link - waiting]"); AcceptEventsFromSL = false; LoginEventsAttached = false; GetClient().Network.LoggedOut += BotLoggedOut; @@ -646,12 +651,15 @@ namespace SecondBotEvents.Services protected void BotLoggedOut(object o, LoggedOutEventArgs e) { LoginEventsAttached = false; - GetClient().Network.SimConnected += BotLoggedIn; - GetClient().Self.ChatFromSimulator -= LocalChat; - GetClient().Self.IM -= BotImMessage; - GetClient().Groups.CurrentGroups -= GroupCurrent; + if (GetClient() != null) + { + GetClient().Network.SimConnected += BotLoggedIn; + GetClient().Self.ChatFromSimulator -= LocalChat; + GetClient().Self.IM -= BotImMessage; + GetClient().Groups.CurrentGroups -= GroupCurrent; + } AcceptEventsFromSL = false; - LogFormater.Info("Discord service [Avi link - standby]"); + LogFormater.Info("Discord service [Avi link - lost connection to SL]"); } protected void BotImMessage(object o, InstantMessageEventArgs e)