Skip to content

Commit

Permalink
[Bot] Fix stacktrace not printing to console
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Sep 19, 2024
1 parent 0aaceb7 commit 4ba24d3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Bot/Discord/Dc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal static async Task<RestUserMessage> NewExceptionLogAsync(Exception ex)
}

// Always waits until the exception is actually sent
return await Program.TryForeverAsync<RestUserMessage>(
return await Program.TryUntilAsync<RestUserMessage>(
async () => await s_botLog.SendMessageAsync(s_botApp.Owner.Mention, embed: emb.Build())
);
}
Expand Down
2 changes: 1 addition & 1 deletion Bot/Discord/ReadyHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
});
Expand Down
17 changes: 12 additions & 5 deletions Bot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -45,7 +48,7 @@ internal static string GetExceptionStackTraceString(Exception ex)
return msg;
}

internal static async Task<T> TryForeverAsync<T>(Func<Task<T>> tryFunc, Func<Exception, Task>? repeatFunc = null)
internal static async Task<T> TryUntilAsync<T>(Func<Task<T>> tryFunc, Func<Exception, Task>? repeatFunc = null)
{
while (true)
{
Expand All @@ -67,14 +70,18 @@ internal static async Task<T> TryForeverAsync<T>(Func<Task<T>> tryFunc, Func<Exc
}
}

internal static async Task TryForeverAsync(Func<Task> tryFunc, Func<Exception, Task>? repeatFunc = null)
internal static async Task TryUntilAsync(Func<Task> tryFunc, Func<Exception, Task>? repeatFunc = null,
bool runForever = false)
{
while (true)
{
try
{
await tryFunc();
return;
if (!runForever)
{
return;
}
}
catch (Exception ex)
{
Expand Down
4 changes: 2 additions & 2 deletions Bot/Sql/DbObj.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ internal static void Commit()
internal static async Task InitAsync()
{
s_db.BeginTransaction();
await Program.TryForeverAsync(async () =>
await Program.TryUntilAsync(async () =>

Check warning on line 22 in Bot/Sql/DbObj.cs

View workflow job for this annotation

GitHub Actions / inspect

"[IDE0320] Anonymous function can be made static" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Sql/DbObj.cs(22,37)
{
await Task.Delay(5 * 60 * 1000);
Commit();
});
}, runForever: true);
}

internal virtual int Insert() => s_db.Insert(this);
Expand Down
14 changes: 7 additions & 7 deletions Bot/Sql/Member.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,30 @@ public void AddAlt(Member altMember)

public bool IsAlt()
{
TableQuery<Alt> result = Db.s_db.Table<Alt>().Where(a => a.AltId == CocId);
TableQuery<Alt> result = s_db.Table<Alt>().Where(a => a.AltId == CocId);
return result.Any();
}

public bool IsAltMain()
{
TableQuery<Alt> result = Db.s_db.Table<Alt>().Where(a => a.MainId == CocId);
TableQuery<Alt> result = s_db.Table<Alt>().Where(a => a.MainId == CocId);
return result.Any();
}

public Alt? TryToAlt() => Db.s_db.Table<Alt>().FirstOrDefault(a => a.AltId == CocId);
public Alt? TryToAlt() => s_db.Table<Alt>().FirstOrDefault(a => a.AltId == CocId);

public Main? TryToMain() => Db.s_db.Table<Main>().FirstOrDefault(m => m.MainId == CocId);
public Main? TryToMain() => s_db.Table<Main>().FirstOrDefault(m => m.MainId == CocId);

public Main ToMain() => Db.s_db.Table<Main>().First(m => m.MainId == CocId);
public Main ToMain() => s_db.Table<Main>().First(m => m.MainId == CocId);

public TableQuery<Alt> GetAltsByMain() => Db.s_db.Table<Alt>().Where(a => a.MainId == CocId);
public TableQuery<Alt> GetAltsByMain() => s_db.Table<Alt>().Where(a => a.MainId == CocId);

public string GetName() => Coc.GetMember(CocId).Name;

public Main GetEffectiveMain()
{
Alt? alt = TryToAlt();
string mainId = alt == null ? CocId : alt.MainId;
return Db.s_db.Table<Main>().First(m => m.MainId == mainId);
return s_db.Table<Main>().First(m => m.MainId == mainId);
}
}

0 comments on commit 4ba24d3

Please sign in to comment.