Skip to content

Commit

Permalink
fix server
Browse files Browse the repository at this point in the history
  • Loading branch information
DoroWolf committed Jan 12, 2025
1 parent ad990e9 commit 039b0d8
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions modules/mcserver/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,25 @@
async def query_java_server(
msg: Bot.MessageSession, address: str, raw: bool = False, showplayer: bool = False
) -> str:
match_object = re.match(r"(.*)[\s:](\d*)", address, re.M | re.I)
serip = match_object.group(1) if match_object else address
port = match_object.group(2)if match_object else 25565
servers = []
query_msg = []

try:
server = JavaServer.lookup(f"{serip}:{port}")
server = JavaServer.lookup(address)
status = await server.async_status()
Logger.debug(str(status))
servers.append("[JE]")
query_msg.append("[JE]")

description = status.description
if isinstance(description, str):
servers.append(description)
query_msg.append(description)
elif isinstance(description, dict):
servers.append(description.get("text", ""))
servers.append(
query_msg.append(description.get("text", ""))
query_msg.append(
"".join(extra.get("text", "") for extra in description.get("extra", []))
)

onlinesplayer = f"{msg.locale.t('server.message.player')}{status.players.online} / {status.players.max}"
servers.append(onlinesplayer)
query_msg.append(onlinesplayer)

if showplayer:
playerlist = (
Expand All @@ -43,55 +40,52 @@ async def query_java_server(
players_text = (
"\n".join(playerlist) if playerlist else msg.locale.t("message.none")
)
servers.append(
query_msg.append(
msg.locale.t("server.message.player.current") + "\n" + players_text
)

if hasattr(status, "version") and hasattr(status.version, "name"):
servers.append(msg.locale.t("server.message.version") + status.version.name)
query_msg.append(msg.locale.t("server.message.version") + status.version.name)

servers.append(f"{serip}:{port}")
query_msg.append(address)

except Exception:
if Config("debug", False):
Logger.error(traceback.format_exc())
return ""

if raw:
return "\n".join(servers)
return re.sub(r"§\w", "", "\n".join(servers))
return "\n".join(query_msg)
return re.sub(r"§\w", "", "\n".join(query_msg))


async def query_bedrock_server(msg, address, raw=False):
match_object = re.match(r"(.*)[\s:](\d*)", address, re.M | re.I)
serip = match_object.group(1) if match_object else address
port = match_object.group(2) if match_object else 19132
servers = []
query_msg = []

try:
server = BedrockServer.lookup(f"{serip}:{port}")
server = BedrockServer.lookup(address)
status = await server.async_status()
Logger.debug(str(status))
servers.append("[BE]")
servers.append(status.motd.raw)
query_msg.append("[BE]")
query_msg.append(status.motd.raw)

player_count = f"{msg.locale.t('server.message.player')}{status.players_online} / {status.players_max}"
servers.append(player_count)
query_msg.append(player_count)

if hasattr(status, "version") and hasattr(status.version, "name"):
servers.append(msg.locale.t("server.message.version") + status.version.name)
query_msg.append(msg.locale.t("server.message.version") + status.version.name)

if hasattr(status, "gamemode"):
game_mode = msg.locale.t("server.message.gamemode") + status.gamemode
servers.append(game_mode)
query_msg.append(game_mode)

servers.append(f"{serip}:{port}")
query_msg.append(address)

except Exception:
if Config("debug", False):
Logger.error(traceback.format_exc())
return ""

if raw:
return "\n".join(servers)
return re.sub(r"§\w", "", "\n".join(servers))
return "\n".join(query_msg)
return re.sub(r"§\w", "", "\n".join(query_msg))

0 comments on commit 039b0d8

Please sign in to comment.