Skip to content

Commit

Permalink
Add Uptime Kuma push support
Browse files Browse the repository at this point in the history
  • Loading branch information
FloatingMilkshake committed Sep 8, 2024
1 parent c260334 commit 4e6a6bd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 6 deletions.
2 changes: 2 additions & 0 deletions ConfigJson.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class BaseConfig
[JsonProperty("sshHosts")] public string[] SshHosts { get; private set; }

[JsonProperty("useServerSpecificFeatures")] public bool UseServerSpecificFeatures { get; private set; }

[JsonProperty("uptimeKumaHeartbeatUrl")] public string UptimeKumaHeartbeatUrl { get; private set; }
}

public class IdsConfig
Expand Down
22 changes: 22 additions & 0 deletions Events/HeartbeatEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
namespace MechanicalMilkshake.Events;

public class HeartbeatEvent
{
public static async Task Heartbeated(DiscordClient client, HeartbeatEventArgs e)
{
if (Program.ConfigJson.Base.UptimeKumaHeartbeatUrl is null or "") return;

try
{
var heartbeatResponse = await Program.HttpClient.GetAsync($"{Program.ConfigJson.Base.UptimeKumaHeartbeatUrl}{e.Ping}");
if (heartbeatResponse.IsSuccessStatusCode)
Program.Discord.Logger.LogDebug(Program.BotEventId, "Successfully sent Uptime Kuma heartbeat with ping {ping}ms", e.Ping);
else
Program.Discord.Logger.LogWarning(Program.BotEventId, "Uptime Kuma heartbeat failed with status code {statusCode}", heartbeatResponse.StatusCode);
}
catch (Exception ex)
{
Program.Discord.Logger.LogWarning(Program.BotEventId, "Uptime Kuma heartbeat failed: {exType}: {exMessage}\n{stackTrace}", ex.GetType(), ex.Message, ex.StackTrace);
}
}
}
6 changes: 6 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ internal static async Task Main()

DisabledCommands.Add("feedback");
}

if (ConfigJson.Base.UptimeKumaHeartbeatUrl is null or "")
{
Discord.Logger.LogWarning(BotEventId, "Uptime Kuma heartbeats disabled due to missing push URL.");
}

// Register slash commands as guild commands in home server when
// running in development mode
Expand Down Expand Up @@ -250,6 +255,7 @@ internal static async Task Main()
Discord.GuildCreated += GuildEvents.GuildCreated;
Discord.GuildDeleted += GuildEvents.GuildDeleted;
Discord.GuildMemberUpdated += GuildEvents.GuildMemberUpdated;
Discord.Heartbeated += HeartbeatEvent.Heartbeated;
commands.CommandErrored += ErrorEvents.CommandsNextService_CommandErrored;
slash.SlashCommandErrored += ErrorEvents.SlashCommandErrored;
slash.SlashCommandExecuted += InteractionEvents.SlashCommandExecuted;
Expand Down
8 changes: 2 additions & 6 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
"botToken": "",
"homeChannel": "",
"homeServer": "",

"hastebinUrl": "",

"wolframAlphaAppId": "",

"authorizedUsers": [
"",
""
],

"sshHosts": [
"",
""
],

"useServerSpecificFeatures": false
"useServerSpecificFeatures": false,
"uptimeKumaHeartbeatUrl": ""
},

"ids": {
Expand Down

0 comments on commit 4e6a6bd

Please sign in to comment.