diff --git a/Bot/Discord/Dc.cs b/Bot/Discord/Dc.cs index e734e13..43df720 100644 --- a/Bot/Discord/Dc.cs +++ b/Bot/Discord/Dc.cs @@ -127,7 +127,7 @@ internal static async Task NewExceptionLogAsync(Exception ex) } // Always waits until the exception is actually sent - return await Program.TryForeverAsync( + return await Program.TryUntilAsync( async () => await s_botLog.SendMessageAsync(s_botApp.Owner.Mention, embed: emb.Build()) ); } diff --git a/Bot/Discord/ReadyHandler.cs b/Bot/Discord/ReadyHandler.cs index 5c38556..098cfa3 100644 --- a/Bot/Discord/ReadyHandler.cs +++ b/Bot/Discord/ReadyHandler.cs @@ -31,7 +31,7 @@ internal async Task RunAsync() else if (_repeatCount < 10) { _repeatCount++; - await Program.TryForeverAsync(async () => + await Program.TryUntilAsync(async () => { await _msg!.ModifyAsync(mp => mp.Content = _msg.Content + "\n" + DateTime.Now.ToString("HH:mm:ss")); }); diff --git a/Bot/Program.cs b/Bot/Program.cs index 5e20117..d7b4994 100644 --- a/Bot/Program.cs +++ b/Bot/Program.cs @@ -5,8 +5,11 @@ namespace Hyperstellar; public static class Program { - private static void DefaultTryForeverExceptionFunc(Exception ex) => - Console.Error.WriteLine(GetExceptionStackTraceString(ex)); + private static void DefaultTryForeverExceptionFunc(Exception ex) + { + ex.Demystify(); + Console.WriteLine(ex); + } internal static string GetExceptionStackTraceString(Exception ex) { @@ -45,7 +48,7 @@ internal static string GetExceptionStackTraceString(Exception ex) return msg; } - internal static async Task TryForeverAsync(Func> tryFunc, Func? repeatFunc = null) + internal static async Task TryUntilAsync(Func> tryFunc, Func? repeatFunc = null) { while (true) { @@ -67,14 +70,18 @@ internal static async Task TryForeverAsync(Func> tryFunc, Func tryFunc, Func? repeatFunc = null) + internal static async Task TryUntilAsync(Func tryFunc, Func? repeatFunc = null, + bool runForever = false) { while (true) { try { await tryFunc(); - return; + if (!runForever) + { + return; + } } catch (Exception ex) { diff --git a/Bot/Sql/DbObj.cs b/Bot/Sql/DbObj.cs index 01275dd..3dce9db 100644 --- a/Bot/Sql/DbObj.cs +++ b/Bot/Sql/DbObj.cs @@ -19,11 +19,11 @@ internal static void Commit() internal static async Task InitAsync() { s_db.BeginTransaction(); - await Program.TryForeverAsync(async () => + await Program.TryUntilAsync(async () => { await Task.Delay(5 * 60 * 1000); Commit(); - }); + }, runForever: true); } internal virtual int Insert() => s_db.Insert(this); diff --git a/Bot/Sql/Member.cs b/Bot/Sql/Member.cs index ff96332..a30e0cc 100644 --- a/Bot/Sql/Member.cs +++ b/Bot/Sql/Member.cs @@ -32,23 +32,23 @@ public void AddAlt(Member altMember) public bool IsAlt() { - TableQuery result = Db.s_db.Table().Where(a => a.AltId == CocId); + TableQuery result = s_db.Table().Where(a => a.AltId == CocId); return result.Any(); } public bool IsAltMain() { - TableQuery result = Db.s_db.Table().Where(a => a.MainId == CocId); + TableQuery result = s_db.Table().Where(a => a.MainId == CocId); return result.Any(); } - public Alt? TryToAlt() => Db.s_db.Table().FirstOrDefault(a => a.AltId == CocId); + public Alt? TryToAlt() => s_db.Table().FirstOrDefault(a => a.AltId == CocId); - public Main? TryToMain() => Db.s_db.Table
().FirstOrDefault(m => m.MainId == CocId); + public Main? TryToMain() => s_db.Table
().FirstOrDefault(m => m.MainId == CocId); - public Main ToMain() => Db.s_db.Table
().First(m => m.MainId == CocId); + public Main ToMain() => s_db.Table
().First(m => m.MainId == CocId); - public TableQuery GetAltsByMain() => Db.s_db.Table().Where(a => a.MainId == CocId); + public TableQuery GetAltsByMain() => s_db.Table().Where(a => a.MainId == CocId); public string GetName() => Coc.GetMember(CocId).Name; @@ -56,6 +56,6 @@ public Main GetEffectiveMain() { Alt? alt = TryToAlt(); string mainId = alt == null ? CocId : alt.MainId; - return Db.s_db.Table
().First(m => m.MainId == mainId); + return s_db.Table
().First(m => m.MainId == mainId); } }