i have an api call to https://api.mcstatus.io/v2/status/java/147.135.125.35 with…
- User
i have an api call to https://api.mcstatus.io/v2/status/java/147.135.125.35
with response:
{"online":true,"host":"147.135.125.35","port":25565,"ip_address":"147.135.125.35","eula_blocked":false,"retrieved_at":1698194380393,"expires_at":1698194440393,"version":{"name_raw":"Paper 1.20.1","name_clean":"Paper 1.20.1","name_html":"\u003cspan\u003e\u003cspan style="color: #ffffff;"\u003ePaper 1.20.1\u003c/span\u003e\u003c/span\u003e","protocol":763},"players":{"online":0,"max":100,"list":[]},"motd":{"raw":"§\u0000AmV10z","clean":"AmV10z","html":"\u003cspan\u003e\u003cspan style="color: #ffffff;"\u003eAmV10z\u003c/span\u003e\u003c/span\u003e"},"icon":null,"mods":[],"software":"Paper on 1.20.1-R0.1-SNAPSHOT","plugins":[{"name":"squaremap","version":"1.1.16"},{"name":"StreamMC","version":"1.0"},{"name":"voicechat","version":"2.4.24"},{"name":"squaremarker","version":"1.0.4"},{"name":"BlueMap","version":"3.16"},{"name":"LuckPerms","version":"5.4.98"}],"srv_record":null}
make a python bot that sends a webhook every time there is a player joining and leaving the server
- Assistant
To create a Python bot that sends a webhook every time a player joins or leaves the server, you can use the
requestslibrary to make HTTP requests and thejsonlibrary to handle JSON responses. Here's an example code snippet:import requests import json webhook_url = "YOUR_WEBHOOK_URL" # Replace with your webhook URL def send_webhook(content): payload = { "content": content } headers = { "Content-Type": "application/json" } response = requests.post(webhook_url, data=json.dumps(payload), headers=headers) if response.status_code == 204: print("Webhook sent successfully") else: print("Failed to send webhook") def check_players(): response = requests.get("https://api.mcstatus.io/v2/status/java/147.135.125.35") if response.status_code == 200: data = response.json() players_online = data.get("players").get("online") old_players = check_players.last_players if hasattr(check_players, "last_players") else None check_players.last_players = players_online if old_players is not None: if players_online > old_players: send_webhook(f"A player joined the server!") elif players_online < old_players: send_webhook(f"A player left the server!") else: print("Failed to fetch server status") # Run the check_players function every X seconds import time while True: check_players() time.sleep(10) # Replace 10 with the desired interval in seconds