private void SessionChanged(ITorchSession session, TorchSessionState state) { switch (state) { case TorchSessionState.Loaded: _multibase = Torch.CurrentSession.Managers.GetManager <IMultiplayerManagerBase>(); if (_multibase != null) { _multibase.PlayerJoined += _multibase_PlayerJoined; MyEntities.OnEntityAdd += MyEntities_OnEntityAdd; _multibase.PlayerLeft += _multibase_PlayerLeft; } else { Log.Warn("No join/leave manager loaded!"); } _chatmanager = Torch.CurrentSession.Managers.GetManager <ChatManagerServer>(); if (_chatmanager != null) { _chatmanager.MessageRecieved += MessageRecieved; } else { Log.Warn("No chat manager loaded!"); } Log.Warn("Starting Discord Bridge!"); DDBridge = new DiscordBridge(this); //send status if (Config.UseStatus) { StartTimer(); } break; case TorchSessionState.Unloading: if (DDBridge != null) { if (Config.Stopped.Length > 0) { DDBridge.SendStatusMessage(null, Config.Stopped); } DDBridge.Stopdiscord(); } Log.Warn("Discord Bridge Unloaded!"); Dispose(); break; default: // ignore break; } }
public void UnloadSEDB() { if (DDBridge != null) { Log.Info("Unloading Discord Bridge!"); DDBridge.Stopdiscord(); DDBridge = null; Log.Info("Discord Bridge Unloaded!"); } Dispose(); }
private void InitPost() { Log.Info("Starting Discord Bridge!"); if (DDBridge == null) { DDBridge = new DiscordBridge(this); } //send status if (Config.UseStatus) { StartTimer(); } }
public void UnloadSEDB() { try { if (DDBridge != null) { Log.Info("Unloading Discord Bridge!"); DDBridge.Stopdiscord(); DDBridge = null; Log.Info("Discord Bridge Unloaded!"); } Dispose(); } catch (Exception ex) { Log.Error(ex, "UnloadSEDB: " + ex.Message); } }
public void LoadSEDB() { if (DDBridge == null) { DDBridge = new DiscordBridge(this); } ReflectEssentials(); if (Config.BotToken.Length <= 0) { Log.Error("No BOT token set, plugin will not work at all! Add your bot TOKEN, save and restart torch."); return; } if (_sessionManager == null) { _sessionManager = Torch.Managers.GetManager <TorchSessionManager>(); if (_sessionManager == null) { Log.Warn("No session manager loaded!"); } else { _sessionManager.SessionStateChanged += SessionChanged; } } if (Torch.CurrentSession != null) { if (_multibase == null) { _multibase = Torch.CurrentSession.Managers.GetManager <IMultiplayerManagerBase>(); if (_multibase == null) { Log.Warn("No join/leave manager loaded!"); } else { _multibase.PlayerJoined += _multibase_PlayerJoined; _multibase.PlayerLeft += _multibase_PlayerLeft; MyEntities.OnEntityAdd += MyEntities_OnEntityAdd; } } if (_chatmanager == null) { _chatmanager = Torch.CurrentSession.Managers.GetManager <ChatManagerServer>(); if (_chatmanager == null) { Log.Warn("No chat manager loaded!"); } else { _chatmanager.MessageRecieved += MessageRecieved; } } InitPost(); } else if (Config.PreLoad) { InitPost(); } }
private void Timer_Elapsed(object sender, ElapsedEventArgs e) { if (!Config.Enabled || DDBridge == null) { return; } if (!DDBridge.Ready) { if (TickRetry >= 3 && DiscordBridge.Discord.CurrentUser == null) { DiscordBridge.Discord.ConnectAsync(); TickRetry = 0; } else { if (TickRetry > 10) { DiscordBridge.Discord.DisconnectAsync(); DiscordBridge.Discord.Dispose(); DDBridge = new DiscordBridge(this); TickRetry = 0; } } TickRetry++; } else if (DiscordBridge.Discord.CurrentUser == null || DiscordBridge.Discord.CurrentUser.Id == 0L || DiscordBridge.Discord.Presences == null || DiscordBridge.Discord.Presences.Count == 0) { DDBridge.Ready = false; DiscordBridge.Discord.DisconnectAsync(); DiscordBridge.Discord.Dispose(); DDBridge = new DiscordBridge(this); TickRetry = 0; } if (Torch.CurrentSession == null || torchServer.SimulationRatio <= 0f) { DDBridge.SendStatus(Config.StatusPre, UserStatus.DoNotDisturb); } else { if (timerStart.Ticks == 0) { timerStart = e.SignalTime; } string status = Config.Status; DateTime upTime = new DateTime(e.SignalTime.Subtract(timerStart).Ticks); Regex regex = new Regex(@"{uptime@(.*?)}"); if (regex.IsMatch(status)) { var match = regex.Match(status); string format = match.Groups[0].ToString().Replace("{uptime@", "").Replace("}", ""); status = Regex.Replace(status, "{uptime@(.*?)}", upTime.ToString(format)); } var playersCount = MySession.Static.Players.GetOnlinePlayers().Where(p => p.IsRealPlayer).Count(); var maxPlayers = MySession.Static.MaxPlayers; var simSpeed = torchServer.SimulationRatio.ToString("0.00"); DDBridge.SendStatus(status .Replace("{p}", playersCount.ToString()) .Replace("{mp}", maxPlayers.ToString()) .Replace("{mc}", MySession.Static.Mods.Count.ToString()) .Replace("{ss}", simSpeed), playersCount > 0? UserStatus.Online : UserStatus.Idle); if (Config.SimPing) { if (torchServer.SimulationRatio < Config.SimThresh) { //condition if (i == DiscordBridge.MinIncrement && DiscordBridge.Locked != 1 && playersCount > 0) { Task.Run(() => DDBridge.SendSimMessage(Config.SimMessage)); i = 0; DiscordBridge.Locked = 1; DiscordBridge.FirstWarning = 1; DiscordBridge.CooldownNeutral = 0; Log.Warn("Simulation warning sent!"); } if (DiscordBridge.FirstWarning == 1 && DiscordBridge.CooldownNeutral.ToString("00") == "60" && playersCount > 0) { Task.Run(() => DDBridge.SendSimMessage(Config.SimMessage)); Log.Warn("Simulation warning sent!"); DiscordBridge.CooldownNeutral = 0; i = 0; } DiscordBridge.CooldownNeutral += (60 / DiscordBridge.Factor); i++; } else { //reset counter whenever Sim speed warning threshold is not met meaning that sim speed has to stay below //the set threshold for a consecutive minuete to trigger warning i = 0; DiscordBridge.CooldownNeutral = 0; } } } }