py-mine / mcstatus

A Python library for checking the status of Minecraft servers
https://mcstatus.readthedocs.io
Apache License 2.0
483 stars 37 forks source link

How would I await the functions? #429

Closed TomSipsOnCurry closed 2 years ago

TomSipsOnCurry commented 2 years ago

I remember this used to have an async alternative but now I cant find the function.

ItsDrike commented 2 years ago

Most of the async alternatives are simply prefixed by async_, here's a simple example:

import asyncio
from mcstatus import JavaServer

async def main():
    server = await JavaServer.async_lookup("mc.hypixel.net")
    status = await server.async_status()
    print(status.description)

asyncio.run(main())
TomSipsOnCurry commented 2 years ago

Oh, thank you. I just want to ask how I would get the player list without using query. Because when I used this before I remember doing it with status.

PerchunPak commented 2 years ago

Try this with example by ItsDrike

if status.players.sample is not None:
    players = [player.name for player in status.players.sample]
else:
    players = []

Note that this can be empty as well as contain some advertisement by server.

TomSipsOnCurry commented 2 years ago

For some reason tis always returning none

PerchunPak commented 2 years ago

Are you trying one server?

TomSipsOnCurry commented 2 years ago

Yes

PerchunPak commented 2 years ago

So you will always get the same result. This depends on server's configuration, and if it didn't change - result also will not change.

ItsDrike commented 2 years ago

Yeah, the player sample is not a perfect solution, it's often going to be incomplete and might only include first few players, or even be completely empty, because many servers run plugins to hide it, especially the bigger ones.

In some cases, these plugins can even alter the player sample to contain fake players for purposes of advertisement, so for example there might be a player named "SkyBlock now updated!", or similar things.

Using status for getting player names is very unreliable, however it's the best option you have if the server doesn't support query. For vanilla servers, using sample should work without issues.

TomSipsOnCurry commented 2 years ago

It's my server. I don't want to use query because I'm making a discord bot and since query takes more than 3 seconds the discord.py interaction times out. Sample might be alright because we only have max 7 players.

PerchunPak commented 2 years ago

Then you should find how to enable players list while hovering their count in servers list on your core. Do not use plugins, it's already implemented even in Vanilla. image

kevinkjt2000 commented 2 years ago

@TomSipsOnCurry

query takes more than 3 seconds

Because there is either a firewall or query was not enabled in server.properties.

TomSipsOnCurry commented 2 years ago

Then you should find how to enable players list while hovering their count in servers list on your core. Do not use plugins, it's already implemented even in Vanilla. image

It works

TomSipsOnCurry commented 2 years ago

@TomSipsOnCurry

query takes more than 3 seconds

Because there is either a firewall or query was not enabled in server.properties.

I use a server hoster, but I enabled query

TomSipsOnCurry commented 2 years ago

Nvr mind turns out i put the wrong port :(