Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

import json import math import os import requests import time from keepalive import…

  1. 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)

  2. Assistant

    The code is using the requests library to make a GET request to the https://api.mcstatus.io/v2/status/java/{SERVER_IP} endpoint. It then converts the response into a JSON format using the json library.

    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.json and 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.json file with the current players list and waits for the time specified in the expires_at value before making the next request.

  3. User

    write online status to status.json and read from it, send a webhook warning server is offline if offline

  4. Assistant

    To update the online status in a status.json file and send a webhook warning if the server goes offline, you can modify the provided code as follows:

    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']
    status_file = "status.json"
    
    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)
    
            with open(status_file, "r") as file:
                status_data = json.load(file)
                previous_status = status_data.get('online', True)
    
            if online == "false":
                status_data = {'online': False}
                with open(status_file, "w") as file:
                    json.dump(status_data, file)
    
                if previous_status:
                    data = {
                        'content': 'The server is now offline.'
                    }
                    requests.post(os.environ['WEBHOOK_URL'], json=data)
    
            else:
                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)
    
                with open("players.json", "r") as file:
                    file_data = file.read()
                    file_list_names = eval(file_data)
    
                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)
    
                status_data = {'online': True}
                with open(status_file, "w") as file:
                    json.dump(status_data, file)
    
            time.sleep(expires_at - current_time)
    
        except Exception as e:
            print("Error:", e)
            status_data = {'online': False}
            with open(status_file, "w") as file:
                json.dump(status_data, file)
            data = {
                'content': 'There was an error checking the server status.'
            }
            requests.post(os.environ['WEBHOOK_URL'], json=data)
            time.sleep(60)
    

    This code adds the following changes:

    1. It introduces a status_file variable to specify the name of the file to store the online status of the server.
    2. It reads the previous online status from the status.json file. If the server goes offline, it updates the status to False in the file and sends a webhook warning if the previous status was online.
    3. If the server is online, it performs the same list comparison and webhook notification logic as before.
    4. After the list comparison, it updates the online status to True in the status.json file.
    5. If there's an exception while checking the server status, it updates the online status to False in the status.json file and sends a webhook warning about the error.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.