4

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?

shnap
  • 175
  • 1
  • 1
  • 10

2 Answers2

4

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 :)

Lemon
  • 1,184
  • 11
  • 33
  • About checking if the server is offline. Do I need to just check with the query command because I can't seem to find a command that can actually show me if the server is online or not (status will still work if the server offline) – shnap Dec 06 '20 at 12:23
  • Very relevant GitHub issue that should help you here: https://github.com/Dinnerbone/mcstatus/issues/79. This could possibly help also: https://github.com/Dinnerbone/mcstatus/issues/57#issuecomment-490328948 – Lemon Dec 06 '20 at 12:28
0

You can do that with SourceQuery - pick the code from github and you can import that in your DC Bot and it gives you all the information any GoldSRC Server can give you, MC works with it as well.

https://github.com/olli-e/ISRT-Insurgency-Sandstorm-RCON-Query-Tool/blob/main/bin/SourceQuery.py

You will find an example usage at the end of the script.

You can also use mcrcon, but I assume you want to wrap it in python.