//This is a loop that keeps running and checks if the bot has been disconnected public StartConnectionWatchdog(SCPDiscordPlugin plugin) { while (true) { Thread.Sleep(200); if (!plugin.clientSocket.Connected && plugin.hasConnectedOnce) { plugin.Info("Discord bot connection issue detected, attempting reconnect..."); try { plugin.Info("Your Bot IP: " + plugin.GetConfigString("discord_bot_ip") + ". Your Bot Port: " + plugin.GetConfigInt("discord_bot_port") + "."); plugin.clientSocket = new TcpClient(plugin.GetConfigString("discord_bot_ip"), plugin.GetConfigInt("discord_bot_port")); plugin.Info("Reconnected to Discord bot."); plugin.SendMessageToBot("default", "botmessages.reconnectedtobot"); } catch (SocketException e) { plugin.Info("Error occured while reconnecting to discord bot server."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } catch (ObjectDisposedException e) { plugin.Info("TCP client was unexpectedly closed."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { plugin.Info("Invalid port."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } catch (ArgumentNullException e) { plugin.Info("IP address is null."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } } } }
//This is ran once on the first time connecting to the bot public ConnectToBot(SCPDiscordPlugin plugin) { Thread.Sleep(2000); while (!plugin.clientSocket.Connected) { plugin.Info("Attempting Bot Connection..."); try { plugin.Info("Your Bot IP: " + plugin.GetConfigString("discord_bot_ip") + ". Your Bot Port: " + plugin.GetConfigInt("discord_bot_port") + "."); plugin.clientSocket.Connect(plugin.GetConfigString("discord_bot_ip"), plugin.GetConfigInt("discord_bot_port")); } catch (SocketException e) { plugin.Info("Error occured while connecting to discord bot server."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } catch (ObjectDisposedException e) { plugin.Info("TCP client was unexpectedly closed."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } catch (ArgumentOutOfRangeException e) { plugin.Info("Invalid port."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } catch (ArgumentNullException e) { plugin.Info("IP address is null."); plugin.Debug(e.ToString()); Thread.Sleep(5000); } } plugin.Info("Connected to Discord bot."); plugin.SendMessageToBot("default", "botmessages.connectedtobot"); plugin.hasConnectedOnce = true; }