import json import math import os import requests import time from keepalive import…
- User
import json import math import os import requests import time from keep_alive import keep_alive
keep_alive()
url = ("https://api.mcstatus.io/v2/status/java/" + os.environ['SERVER_IP'])
while True: try: response = requests.get(url) json_data = json.loads(response.text)
expires_at = json_data['expires_at'] / 1000 current_time = time.time() online = json_data['online'] print("Current time:", current_time, " Expires at:", expires_at) if online == "false": players_list = json_data['players']['list'] players_online = json_data['players']['online'] players_list_names = [player["name_clean"] for player in players_list] print("Players online:", players_online) if players_online > 0: print(players_list_names) # Read the players list from the file with open("players.json", "r") as file: file_data = file.read() file_list_names = eval(file_data)Compare the lists
common_players = set(players_list_names) & set(file_list_names) new_players = set(players_list_names) - set(file_list_names) removed_players = set(file_list_names) - set(players_list_names) if len(common_players) > 0: print("Common Players:", common_players) if len(new_players) > 0: print("New Players:", new_players) if len(removed_players) > 0: print("Removed Players:", removed_players) if len(removed_players) or len(new_players): list_leaving = f"- {', '.join(removed_players)}" list_joining = f"+ {', '.join(new_players)}" web_content = f"<t:{math.trunc(expires_at)}> Online: {players_online}" web_content += "```diff" if len(removed_players) > 0: web_content += f"\n{list_leaving}" if len(new_players) > 0: web_content += f"\n{list_joining}" web_content += "```" data = { 'content': web_content } requests.post(os.environ['WEBHOOK_URL'], json=data) with open("players.json", "w") as file: json.dump(players_list_names, file) time.sleep(expires_at - current_time)except Exception as e: print ("Error:", e) time.sleep(60)
- Assistant
The code is using the
requestslibrary to make a GET request to thehttps://api.mcstatus.io/v2/status/java/{SERVER_IP}endpoint. It then converts the response into a JSON format using thejsonlibrary.The code continuously runs in a loop, checking the server status every few seconds. It retrieves the number of players online, as well as their names. If there are any changes in the players list compared to the previous check, it prints out the common players, new players, and removed players.
The code also reads a list of players from a file called
players.jsonand compares it to the current players list. It identifies any players who have joined or left the server since the last check.If there are any changes in the players list, it constructs a message with the leaving players prefixed with a "-", joining players prefixed with a "+", and sends this message to a specified Discord webhook URL.
The code then updates the
players.jsonfile with the current players list and waits for the time specified in theexpires_atvalue before making the next request. - User
write online status to status.json and read from it, send a webhook warning server is offline if offline
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.