private async Task CheckStateAsync() { // Client reconnected, no need to reset if (_discord.ConnectionState == ConnectionState.Connected) { return; } if (_attemptReset) { await InfoAsync("Attempting to reset the client"); var timeout = Task.Delay(_timeout); lock (resetLock){ while ((DateTime.UtcNow - lastDisconnect).TotalSeconds <= 30 || StaticBase.GetMopsRAM() > 2200) { Task.Delay(30000).Wait(); } lastDisconnect = DateTime.UtcNow; } var connect = _discord.StartAsync(); var task = await Task.WhenAny(timeout, connect); if (task == timeout) { await CriticalAsync("Client reset timed out (task deadlocked?), killing process"); //FailFast(); } else if (connect.IsFaulted) { await CriticalAsync("Client reset faulted, killing process", connect.Exception); FailFast(); } else if (connect.IsCompletedSuccessfully) { await InfoAsync("Client reset succesfully!"); } return; } await CriticalAsync("Client did not reconnect in time, killing process"); FailFast(); }
private async Task Start() { Client = new DiscordShardedClient(new DiscordSocketConfig() { LogLevel = LogSeverity.Info, //TotalShards = 2, LargeThreshold = 50, MessageCacheSize = 0, AlwaysDownloadUsers = false, }); System.IO.Directory.CreateDirectory(".//mopsdata//Images"); using (StreamReader sr = new StreamReader(new FileStream("mopsdata//Config.json", FileMode.Open))) Config = JsonConvert.DeserializeObject <Dictionary <string, string> >(sr.ReadToEnd()); using (StreamReader sr = new StreamReader(new FileStream("mopsdata//TrackerLimits.json", FileMode.Open))) TrackerLimits = JsonConvert.DeserializeObject <Dictionary <string, Dictionary <string, int> > >(sr.ReadToEnd()); Client.Log += ClientLog; Client.ShardReady += onShardReady; Task.Run(() => { StaticBase.UpdateStatusAsync(); }); await Client.LoginAsync(TokenType.Bot, Config["DiscordToken"]); foreach (var shard in Client.Shards) { await shard.StartAsync(); do { await Task.Delay(30000); } while(StaticBase.GetMopsRAM() > 2200); } await Task.Delay(-1); }