import os import requests import time import sqlite3 from mcstatus import JavaServer…
- User
import os import requests import time import sqlite3
from mcstatus import JavaServer from keep_alive import keep_alive keep_alive()
SQLite database file
DB_FILE = "players.db"
def send_discord_webhook(content): data = {"content": content} requests.post(os.environ['WEBHOOK_URL'], json=data)
Create Minecraft server object
server = JavaServer(os.environ['SERVER_IP'], 25565)
Create and connect to the database
conn = sqlite3.connect(DB_FILE) cursor = conn.cursor()
Create players table if it doesn't exis
cursor.execute('''CREATE TABLE IF NOT EXISTS players (name TEXT PRIMARY KEY)''')
Get the last known state of players from the database
cursor.execute('SELECT name FROM players') players = [row[0] for row in cursor.fetchall()]
while True: try: # Get current player list status = server.query() current_players = status.players.names
# Check for new players new_players = [ player for player in current_players if player not in players ] for player in new_players: players.append(player) print(f'{player} has joined the server!') send_discord_webhook(f'{player} has joined the server!') # Check for leaving players leaving_players = [ player for player in players if player not in current_players ] for player in leaving_players: players.remove(player) print(f'{player} has left the server!') send_discord_webhook(f'{player} has left the server!') # Update the database for player in new_players: cursor.execute('INSERT INTO players VALUES (?)', (player, )) for player in leaving_players: cursor.execute('DELETE FROM players WHERE name = ?', (player, )) # Commit the changes and close the connection conn.commit() # Delay between checks (in seconds) time.sleep(30)except Exception as e: print(f'Error occurred: {e}')
Close the database connection before exiting
conn.close()
- Assistant
The above code is a Python script that monitors player activity on a Minecraft server and sends notifications to a Discord webhook whenever a player joins or leaves the server.
The script uses the mcstatus library to query the server for player information. It also uses the sqlite3 library to store the last known state of players in a SQLite database.
The script starts by creating a connection to the SQLite database file specified by the DB_FILE variable. It then creates a players table in the database if it doesn't already exist.
The script then enters an infinite loop where it continuously queries the Minecraft server for player information. It compares the current player list with the last known player list stored in the database to determine if any new players have joined or if any players have left.
- User
make it print the html code into console
- Assistant
To print the HTML code of the Discord webhook request into the console, you can modify the
send_discord_webhookfunction to print the response content. Here's an updated version of the code:import os import requests import time import sqlite3 from mcstatus import JavaServer from keep_alive import keep_alive keep_alive() # SQLite database file DB_FILE = "players.db" def send_discord_webhook(content): data = {"content": content} response = requests.post(os.environ['WEBHOOK_URL'], json=data) # Print the HTML code of the response print(response.content) # Rest of the code...In the modified version of the
send_discord_webhookfunction, we use theresponse.contentproperty to access the HTML code of the response. This content is then printed to the console using theprintfunction.By including this modification, the script will now print the HTML code of the Discord webhook request into the console whenever a player joins or leaves the Minecraft server.