Skip to content

Instantly share code, notes, and snippets.

@indrer
Last active November 30, 2024 21:12
Show Gist options
  • Save indrer/13ed67e01739884dee694280407eda0a to your computer and use it in GitHub Desktop.
Save indrer/13ed67e01739884dee694280407eda0a to your computer and use it in GitHub Desktop.
from mcstatus import JavaServer
import discord
import asyncio
import re
IP = ''
TOKEN = ''
server = JavaServer.lookup(IP)
client = discord.Client()
async def getServerState():
try:
status = server.status()
map_name = status.description.split('»')[1]
except Exception as e:
print(e)
return 'nothing right now'
curr_map = re.sub(r'§[0-9A-FK-OR]|(\xC2)|(\xAB)', '', map_name, 0, re.MULTILINE | re.IGNORECASE)
players = status.players.online
state = ''
if players == 0:
state = f'with no players on {curr_map}'
else:
plr = 'player' if players == 1 else 'players'
state = f'with {str(players)} {plr} on {curr_map}'
return state
@client.event
async def on_ready():
print('Enyo Minecraft server bot is running')
while True:
state = await getServerState()
try:
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name=state))
except ConnectionResetError as e:
print('State :', state)
print(e)
continue
await asyncio.sleep(5)
client.run(TOKEN)
discord.py >= 2.3.2
mcstatus >= 11.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment