|
@@ -0,0 +1,45 @@
|
|
|
+from nonebot import on_command
|
|
|
+from nonebot.rule import to_me
|
|
|
+from nonebot.typing import T_State
|
|
|
+from nonebot.adapters import Bot, Event
|
|
|
+import traceback
|
|
|
+
|
|
|
+from mcstatus import MinecraftServer
|
|
|
+#pip3 install -U mcstatus==6.0.0 注意版本
|
|
|
+
|
|
|
+mcstatus = on_command("MC服务器状态", rule=to_me(), priority=5, block=True)
|
|
|
+
|
|
|
+
|
|
|
+@mcstatus.handle()
|
|
|
+async def handle_first_receive(bot: Bot, event: Event, state: T_State):
|
|
|
+ args = str(event.get_message()).strip() # 首次发送命令时跟随的参数
|
|
|
+ if args:
|
|
|
+ state["hostport"] = args # 如果用户发送了参数则直接赋值
|
|
|
+
|
|
|
+
|
|
|
+@mcstatus.got("hostport", prompt="请再给一个服务器地址 默认?")
|
|
|
+async def handle_hostport(bot: Bot, event: Event, state: T_State):
|
|
|
+ hostport = state["hostport"]
|
|
|
+ if hostport == "默认":
|
|
|
+ hostport = "172.30.104.193:25568"
|
|
|
+ status = await get_status(hostport)
|
|
|
+ await mcstatus.finish(status)
|
|
|
+
|
|
|
+
|
|
|
+async def get_status(hostport: str):
|
|
|
+
|
|
|
+ try:
|
|
|
+ Minecraft_Server = MinecraftServer.lookup(hostport)
|
|
|
+ status = Minecraft_Server.status()
|
|
|
+ try:
|
|
|
+ query = Minecraft_Server.query()
|
|
|
+ players = ", ".join(query.players.names)
|
|
|
+ if len(query.players.names) == 0:
|
|
|
+ players = " 无"
|
|
|
+ except Exception as e:
|
|
|
+ players = str(e)
|
|
|
+ re = "玩家数: {0} 延迟 {1} ms \n在线玩家:{2}".format(status.players.online, status.latency, players)
|
|
|
+ return re
|
|
|
+ except Exception as e:
|
|
|
+ return str(e)
|
|
|
+ #return traceback.print_exc()
|