How to check if a minecraft server is online and get info of stats (like number of players online) using Discord py

Viewed 12631

I have a bot that works in a discord server for a minecraft server. I want to make one of the bot's command to ping the minecraft server to check if it is up (or even get stats like number of players).

Does anybody know of a way to ping a mc server and even get stats from said server?

2 Answers

This is possible! You can use Dinnerbone's own implementation.

This basic Python script should do what you want (using hypixel as an example):

from mcstatus import MinecraftServer

server = MinecraftServer.lookup("mc.hypixel.net")
status = server.status()
print("The server has {0} players and replied in {1} ms".format(status.players.online, status.latency))

latency = server.ping()
print("The server replied in {0} ms".format(latency))

There's heaps more you can do, check it out: https://github.com/Dinnerbone/mcstatus

You can install this package by running:

python3 -m pip install mcstatus

Also note that according to the Github repo, this will only work on servers above version 1.7 :)

Related