added features.
This commit is contained in:
parent
ffdaad5025
commit
4d5b55a7f1
4 changed files with 129 additions and 1 deletions
2
.env.example
Normal file
2
.env.example
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
url = https://discord.com/api/webhooks/...
|
||||
hostname = your.domain.tld:port
|
||||
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
*.json
|
||||
.env
|
||||
.vscode/
|
||||
!.vscode/extensions.json
|
||||
*.code-workspace
|
||||
|
|
@ -1,3 +1,3 @@
|
|||
# mss-webhooksu
|
||||
|
||||
Discord webhook that pings you.
|
||||
Discord webhook that pings you when someone joins or leaves a minecraft server.
|
||||
121
main.py
Normal file
121
main.py
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
import json
|
||||
import requests
|
||||
from dotenv import dotenv_values
|
||||
|
||||
config = dotenv_values(".env")
|
||||
|
||||
WEBHOOK_URL = config["url"]
|
||||
HOSTNAME = config["hostname"]
|
||||
|
||||
STATUS_FILE = "previous_status.json" # could get it from the .env, but it's not secret information so :shrug:
|
||||
|
||||
with open(STATUS_FILE, "r") as file:
|
||||
previous_status = json.load(file)
|
||||
|
||||
try:
|
||||
response = requests.get(
|
||||
f"https://api.mcsrvstat.us/3/{HOSTNAME}",
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
server_status = response.json()
|
||||
except requests.RequestException as e:
|
||||
print(f"Failed to get server status: {e}")
|
||||
exit(1)
|
||||
|
||||
previous_players_data = previous_status.get("players", {})
|
||||
current_players_data = server_status.get("players", {})
|
||||
|
||||
previous_online = previous_players_data.get("online", 0)
|
||||
current_online = current_players_data.get("online", 0)
|
||||
|
||||
previous_max = previous_players_data.get("max", 0)
|
||||
current_max = current_players_data.get("max", 0)
|
||||
|
||||
previous_player_list = {
|
||||
player["name"]
|
||||
for player in previous_players_data.get("list", [])
|
||||
}
|
||||
|
||||
current_player_list = {
|
||||
player["name"]
|
||||
for player in current_players_data.get("list", [])
|
||||
}
|
||||
|
||||
if (
|
||||
previous_online == current_online
|
||||
and previous_player_list == current_player_list
|
||||
):
|
||||
print("nothing changed.")
|
||||
exit()
|
||||
|
||||
|
||||
if current_online > previous_online:
|
||||
pc_diff_sign = "+"
|
||||
elif current_online < previous_online:
|
||||
pc_diff_sign = "-"
|
||||
else:
|
||||
pc_diff_sign = " "
|
||||
|
||||
|
||||
if previous_online != current_online:
|
||||
pc_diff_previously = f"(previously {previous_online})"
|
||||
else:
|
||||
pc_diff_previously = ""
|
||||
|
||||
|
||||
playercount_diff_formatted = (
|
||||
f"{pc_diff_sign} {current_online}/{current_max} "
|
||||
f"{pc_diff_previously}"
|
||||
)
|
||||
|
||||
|
||||
joined_players = current_player_list - previous_player_list
|
||||
left_players = previous_player_list - current_player_list
|
||||
|
||||
player_diff = []
|
||||
|
||||
for player in sorted(current_player_list):
|
||||
if player in joined_players:
|
||||
player_diff.append(f"+ {player}")
|
||||
else:
|
||||
player_diff.append(f" {player}")
|
||||
|
||||
for player in sorted(left_players):
|
||||
player_diff.append(f"- {player}")
|
||||
|
||||
player_diff_formatted = "\n".join(player_diff) or " no one is here."
|
||||
|
||||
|
||||
payload = {
|
||||
"content": f"""---
|
||||
# status changed on {HOSTNAME}
|
||||
|
||||
```diff
|
||||
{playercount_diff_formatted}
|
||||
```
|
||||
|
||||
```diff
|
||||
{player_diff_formatted}
|
||||
```
|
||||
"""
|
||||
}
|
||||
|
||||
|
||||
try:
|
||||
response = requests.post(
|
||||
WEBHOOK_URL,
|
||||
json=payload,
|
||||
timeout=10
|
||||
)
|
||||
response.raise_for_status()
|
||||
except requests.RequestException as e:
|
||||
print(f"failed to send webhook: {e}")
|
||||
exit(1)
|
||||
|
||||
|
||||
print("success")
|
||||
|
||||
|
||||
with open(STATUS_FILE, "w") as file:
|
||||
json.dump(server_status, file, indent=2)
|
||||
Loading…
Add table
Reference in a new issue