Skip to content

Commit

Permalink
Better logging for DiscordClient#ClientErrored and DiscordClient#Sock…
Browse files Browse the repository at this point in the history
…etErrored
  • Loading branch information
Plerx2493 committed May 6, 2024
1 parent 16608a8 commit d066ba4
Showing 1 changed file with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ internal static async Task OnClientErrored(DiscordClient sender, ClientErrorEven
.WithTimestamp(DateTime.UtcNow)
.WithTitle($"Eventhandler exception: {e.EventName} - {e.Exception.GetType()}")
.WithDescription(e.Exception.Message);

if (!string.IsNullOrWhiteSpace(e.Exception.StackTrace))
{
if (e.Exception.StackTrace.Length > 1013)
{
string stacktrace = e.Exception.StackTrace[..1013] + "...";
exceptionEmbed.AddField("Stacktrace", Formatter.BlockCode(stacktrace));
}
else
{
exceptionEmbed.AddField("Stacktrace", Formatter.BlockCode(e.Exception.StackTrace));
}
}

DiscordWebhookBuilder webhookBuilder = new DiscordWebhookBuilder()
.WithUsername("Mads-Debug")
Expand All @@ -116,4 +129,33 @@ await e.Context.Channel.SendPaginatedMessageAsync(e.Context.Member, pages, Pagin
ButtonPaginationBehavior.DeleteButtons);
}
}

internal static async Task OnSocketErrored(DiscordClient client, SocketErrorEventArgs e)
{
DiscordEmbedBuilder exceptionEmbed = new DiscordEmbedBuilder()
.WithAuthor("Mads-Debug")
.WithColor(new DiscordColor(0, 255, 194))
.WithTimestamp(DateTime.UtcNow)
.WithTitle($"Gateway socket exception: {e.Exception.GetType()} - ")
.WithDescription(e.Exception.Message);

if (!string.IsNullOrWhiteSpace(e.Exception.StackTrace))
{
if (e.Exception.StackTrace.Length > 1013)
{
string stacktrace = e.Exception.StackTrace[..1013] + "...";
exceptionEmbed.AddField("Stacktrace", Formatter.BlockCode(stacktrace));
}
else
{
exceptionEmbed.AddField("Stacktrace", Formatter.BlockCode(e.Exception.StackTrace));
}
}

DiscordWebhookBuilder webhookBuilder = new DiscordWebhookBuilder()
.WithUsername("Mads-Debug")
.AddEmbed(exceptionEmbed);

await MainProgram.WebhookClient.BroadcastMessageAsync(webhookBuilder);
}
}

0 comments on commit d066ba4

Please sign in to comment.