-
-
Save Brottweiler/8293ebc61f80fcc9070cf2e1a87f437c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mcstatus import JavaServer | |
import discord | |
import asyncio | |
import re | |
IP = '' | |
TOKEN = '' | |
server = JavaServer.lookup(IP) | |
client = discord.Client(intents=discord.Intents.default()) | |
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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