Created
October 19, 2024 19:15
-
-
Save zopieux/7fa1f519d28c59a828bbda70cc7fe99d to your computer and use it in GitHub Desktop.
Satisfactory status (API) to Discord message
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
systemd.timers.satisfactory-status = lib.mkIf botEnabled { | |
description = "Satisfactory status to Discord"; | |
wantedBy = [ "timers.target" ]; | |
requires = [ "network-online.target" ]; | |
timerConfig = { | |
OnCalendar = "minutely"; | |
Persistent = true; | |
}; | |
}; | |
systemd.services.satisfactory-status = lib.mkIf botEnabled { | |
description = "Satisfactory status to Discord"; | |
wantedBy = [ "multi-user.target" ]; | |
serviceConfig = { | |
Restart = "no"; | |
User = "satisfactory"; | |
Group = "satisfactory"; | |
}; | |
script = | |
let | |
python = pkgs.python3.withPackages (ps: with ps; [ requests ]); | |
script = pkgs.writeText "satisfactory-discord.py" '' | |
import requests | |
import traceback | |
import sys | |
import json | |
import subprocess | |
BEARER = 'Bearer ${cfg.apiBearer}' | |
DISCORD = '${cfg.discordHook}' | |
try: | |
satisf = subprocess.run([ | |
'${pkgs.curl}/bin/curl', '-k', 'https://0.0.0.0:7777/api/v1', | |
'-H', f'Authorization: {BEARER}', | |
'-H', 'Content-Type: application/json', | |
'--data', '{"function": "QueryServerState", "data": {}}', | |
], check=True, capture_output=True) | |
satisf = json.loads(satisf.stdout) | |
satisf = satisf['data']['serverGameState'] | |
hours_played = satisf['totalGameDuration'] // 3600 | |
players = satisf['numConnectedPlayers'] | |
tier = satisf['techTier'] | |
connected = f'{players} connecté{"s" if players > 1 else ""}' if players > 0 else "personne n'est connecté" | |
message = f"Le serveur est up, {connected}, tier {tier}, {hours_played} heures jouées." | |
except: | |
# traceback.print_exc() | |
message = "Le serveur est down." | |
# print("Setting message:", message, file=sys.stderr) | |
discord = requests.get(DISCORD).json() | |
if discord['content'] != message: | |
requests.patch(DISCORD, json={'content': message}) | |
''; | |
in | |
"${python.interpreter} ${script}"; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment