Skip to content

Commit

Permalink
try/catch Uptime Kuma push to avoid killing the bot on failure
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Aug 8, 2024
1 parent 874203b commit cb92ca8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Events/HeartbeatEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@ public static async Task OnHeartbeat(IGatewayClient client)
Program.discord.Logger.LogDebug("Heartbeat ping: {ping}", client.Ping.TotalMicroseconds);
if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL")) && client.IsConnected)
{
var response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL") + client.Ping.TotalMicroseconds);
HttpResponseMessage response;
try
{
response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL") + client.Ping.TotalMicroseconds);
}
catch (Exception ex)
{
Program.discord.Logger.LogError(ex, "Uptime Kuma push failed during heartbeat event!");
return;
}
if (response.StatusCode == HttpStatusCode.OK)
{
Program.discord.Logger.LogDebug("Heartbeat ping succeeded.");
Expand Down
11 changes: 10 additions & 1 deletion Events/ReadyEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,16 @@ public static async Task OnStartup(DiscordClient client)

if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL")))
{
var response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL"));
HttpResponseMessage response;
try
{
response = await Program.httpClient.GetAsync(Environment.GetEnvironmentVariable("UPTIME_KUMA_PUSH_URL"));
}
catch (Exception ex)
{
discord.Logger.LogError(ex, "Uptime Kuma push failed during startup!");
return;
}
if (response.StatusCode == HttpStatusCode.OK)
{
discord.Logger.LogDebug("Heartbeat ping succeeded.");
Expand Down

0 comments on commit cb92ca8

Please sign in to comment.