tweak discord reconnect to reattach to the SL client if discord dies but SL lives

This commit is contained in:
Madpeter 2024-08-29 10:12:17 +01:00
parent 7a31a40e92
commit 0afffb9adc
2 changed files with 26 additions and 8 deletions

View file

@ -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;

View file

@ -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<UUID>());
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)