shnap
shnap

Reputation: 185

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

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?

Upvotes: 4

Views: 16977

Answers (2)

Lemon
Lemon

Reputation: 1440

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

Upvotes: 4

user14757127
user14757127

Reputation:

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.

Upvotes: 0

Related Questions