Skip to content

Commit

Permalink
[Bot] Add exception reporting and resolve CA suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonic-Rainbow committed Feb 7, 2024
1 parent a4f1ca2 commit 0f38156
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 64 deletions.
1 change: 1 addition & 0 deletions Bot/.editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ dotnet_diagnostic.CA1852.severity = suggestion
# ReSharper
resharper_function_never_returns_highlighting = none
resharper_arrange_object_creation_when_type_not_evident_highlighting = none
resharper_unused_type_global_highlighting = none

# New line preferences
csharp_new_line_before_open_brace = all
Expand Down
9 changes: 8 additions & 1 deletion Bot/Clash/Coc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,14 @@ internal static async Task BotReadyAsync()
_ = Task.Run(Donate25.CheckAsync);
while (true)
{
await PollAsync();
try
{
await PollAsync();
}
catch (Exception ex)
{
await Dc.ExceptionAsync(ex);
}
await Task.Delay(20000);
}
}
Expand Down
125 changes: 63 additions & 62 deletions Bot/Clash/Donate25.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

namespace Hyperstellar.Clash;

internal sealed class Donate25
internal static class Donate25
{
private sealed class Node(long time)
{
internal long _checkTime = time;
internal ICollection<string> _ids = [];
internal readonly ICollection<string> _ids = [];
}

private const int TargetPerPerson = 25; // The donation target per week per person
Expand Down Expand Up @@ -46,41 +46,35 @@ internal static void Init()
internal static void AltAdded(string altId, string mainId)
{
Console.WriteLine($"[Donate25] Removing {altId} -> {mainId} (addalt)");
foreach (Node node in s_queue)
Node? node = s_queue.FirstOrDefault(n => n._ids.Remove(altId));
if (node != null)

Check notice on line 50 in Bot/Clash/Donate25.cs

View workflow job for this annotation

GitHub Actions / inspect

"[InvertIf] Invert 'if' statement to reduce nesting" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Donate25.cs(50,1782)
{
if (node._ids.Remove(altId))
{
Console.WriteLine($"[Donate25] Removed {altId} in {node._checkTime}");
node._ids.Add(mainId);
Donation altDon = Db.GetDonation(altId)!;
Donation mainDon = Db.GetDonation(mainId)!;
altDon.Delete();
mainDon.Donated += altDon.Donated;
mainDon.Update();
Console.WriteLine($"[Donate25] Added {mainId} because it replaced {altId} as main");
break;
}
Console.WriteLine($"[Donate25] Removed {altId} in {node._checkTime}");
node._ids.Add(mainId);
Donation altDon = Db.GetDonation(altId)!;
Donation mainDon = Db.GetDonation(mainId)!;
altDon.Delete();
mainDon.Donated += altDon.Donated;
mainDon.Update();
Console.WriteLine($"[Donate25] Added {mainId} because it replaced {altId} as main");
}
}

internal static void MemberRemoved(string id, string? newMainId)
{
Console.WriteLine($"[Donate25] Removing {id} -> {newMainId}");
foreach (Node node in s_queue)
Node? node = s_queue.FirstOrDefault(n => n._ids.Remove(id));
if (node != null)

Check notice on line 67 in Bot/Clash/Donate25.cs

View workflow job for this annotation

GitHub Actions / inspect

"[InvertIf] Invert 'if' statement to reduce nesting" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Donate25.cs(67,2481)
{
if (node._ids.Remove(id))
Console.WriteLine($"[Donate25] Removed {id} in {node._checkTime}");
if (newMainId != null)

Check notice on line 70 in Bot/Clash/Donate25.cs

View workflow job for this annotation

GitHub Actions / inspect

"[InvertIf] Invert 'if' statement to reduce nesting" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Donate25.cs(70,2601)
{
Console.WriteLine($"[Donate25] Removed {id} in {node._checkTime}");
if (newMainId != null)
{
node._ids.Add(newMainId);
Donation donation = Db.GetDonation(id)!;
donation.Delete();
donation.MainId = newMainId;
donation.Insert();
Console.WriteLine($"[Donate25] Added {newMainId} because it replaced {id} as main");
}
break;
node._ids.Add(newMainId);
Donation donation = Db.GetDonation(id)!;
donation.Delete();
donation.MainId = newMainId;
donation.Insert();
Console.WriteLine($"[Donate25] Added {newMainId} because it replaced {id} as main");
}
}
}
Expand Down Expand Up @@ -110,50 +104,57 @@ internal static void MemberAdded(string id)

internal static async Task CheckAsync()
{
while (s_queue.Count > 0)
try
{
Node node = s_queue.First();
if (node._ids.Count == 0)
while (s_queue.Count > 0)
{
s_queue.Dequeue();
continue;
}
Node node = s_queue.First();
if (node._ids.Count == 0)
{
s_queue.Dequeue();
continue;
}

int waitDelay = (int)((node._checkTime * 1000) - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());
await Task.Delay(waitDelay);
int waitDelay = (int)((node._checkTime * 1000) - DateTimeOffset.UtcNow.ToUnixTimeMilliseconds());

Check notice on line 118 in Bot/Clash/Donate25.cs

View workflow job for this annotation

GitHub Actions / inspect

"[ArrangeRedundantParentheses] Redundant parentheses" on /home/runner/work/Hyperstellar/Hyperstellar/Bot/Clash/Donate25.cs(118,4282)
await Task.Delay(waitDelay);

node = s_queue.Dequeue();
node._checkTime += CheckPeriod;
List<string> violators = [];
foreach (string member in node._ids)
{
IEnumerable<Alt> alts = new Member(member).GetAltsByMain();
int altCount = alts.Count();
int donationTarget = TargetPerPerson * (altCount + 1);
Donation donation = Db.GetDonation(member)!;
if (donation.Donated >= donationTarget)
node = s_queue.Dequeue();
node._checkTime += CheckPeriod;
List<string> violators = [];
foreach (string member in node._ids)
{
Console.WriteLine($"[Donate25] {member} new cycle");
IEnumerable<Alt> alts = new Member(member).GetAltsByMain();
int altCount = alts.Count();
int donationTarget = TargetPerPerson * (altCount + 1);
Donation donation = Db.GetDonation(member)!;
if (donation.Donated >= donationTarget)
{
Console.WriteLine($"[Donate25] {member} new cycle");
}
else
{
violators.Add(member);
Console.WriteLine($"[Donate25] {member} violated");
}
donation.Donated = 0;
donation.Checked = node._checkTime;
donation.Update();
}
else

if (node._ids.Count > 0)
{
violators.Add(member);
Console.WriteLine($"[Donate25] {member} violated");
s_queue.Enqueue(node);
}
donation.Donated = 0;
donation.Checked = node._checkTime;
donation.Update();
}

if (node._ids.Count > 0)
{
s_queue.Enqueue(node);
}

if (violators.Count > 0)
{
await Dc.Donate25Async(violators);
if (violators.Count > 0)
{
await Dc.Donate25Async(violators);
}
}
}
catch (Exception ex)
{
await Dc.ExceptionAsync(ex);
}
}
}
24 changes: 23 additions & 1 deletion Bot/Discord/Dc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal sealed class Dc
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
private static SocketTextChannel s_botLog;
private static InteractionService s_interactionSvc;
private static IApplication s_botApp;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.

internal static readonly DiscordSocketClient s_bot = new();


private static Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
Expand Down Expand Up @@ -56,6 +56,7 @@ internal static async Task InitAsync()
s_interactionSvc.AddTypeConverter<Member>(new MemberConverter());
await s_interactionSvc.AddModulesAsync(Assembly.GetEntryAssembly(), null);
await s_bot.LoginAsync(TokenType.Bot, Secrets.s_discord);
s_botApp = await s_bot.GetApplicationInfoAsync();
await s_bot.StartAsync();
}

Expand Down Expand Up @@ -89,4 +90,25 @@ internal static async Task DonationsChangedAsync(Dictionary<string, DonationTupl
}

internal static async Task Donate25Async(List<string> violators) => await s_botLog.SendMessageAsync($"[Donate25] {string.Join(", ", violators)}");

internal static async Task ExceptionAsync(Exception ex)
{
EmbedBuilder emb = new()
{
Title = ex.Message
};
if (ex.StackTrace != null)
{
emb.Description = ex.StackTrace;
}
string? exName = ex.GetType().FullName;
if (exName != null)
{
emb.Author = new EmbedAuthorBuilder
{
Name = exName
};
}
await s_botLog.SendMessageAsync(s_botApp.Owner.Mention, embed: emb.Build());
}
}

0 comments on commit 0f38156

Please sign in to comment.