Skip to content

Commit c1dcd12

Browse files
add minecraft server module
1 parent 208b9a0 commit c1dcd12

File tree

5 files changed

+70
-30
lines changed

5 files changed

+70
-30
lines changed

.pdm.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[python]
2-
path = "C:/Users/blueg/AppData/Local/Programs/Python/Python39/python.EXE"
2+
path = "C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python39\\python.EXE"

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
],
88
"cSpell.words": [
99
"behaviour",
10-
"behaviours"
10+
"behaviours",
11+
"mcstatus"
1112
],
1213
"restructuredtext.languageServer.disabled": true
1314
}

module/mc_server.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from typing import List, Optional
2+
from graia.ariadne.app import Ariadne
3+
from graia.ariadne.event.message import MessageEvent
4+
from graia.ariadne.message.chain import MessageChain
5+
from graia.ariadne.message.element import Image, Plain
6+
import mcstatus
7+
from graia.saya import Channel
8+
from mcstatus.pinger import PingResponse
9+
from graia.ariadne.message.commander.saya import CommandSchema
10+
11+
channel = Channel.current()
12+
13+
@channel.use(
14+
CommandSchema("[.server|服务器] {server_address:str}")
15+
)
16+
async def get_info(event: MessageEvent, app: Ariadne, server_address: str):
17+
try:
18+
server = mcstatus.MinecraftServer.lookup(server_address)
19+
stat = await server.async_status()
20+
players: Optional[List[PingResponse.Players.Player]] = stat.players.sample
21+
except Exception as e:
22+
reply = MessageChain.create(
23+
[
24+
Plain(f"{repr(e)}"),
25+
]
26+
)
27+
else:
28+
reply = MessageChain(
29+
[
30+
Plain(f"{server_address} 状态\n"),
31+
Image(base64=stat.favicon.removeprefix("data:image/png;base64,"))
32+
if stat.favicon is not None
33+
else Plain(""),
34+
Plain(
35+
f"玩家数:{stat.players.online}/{stat.players.max}\n"
36+
f"延迟:{stat.latency}ms\n"
37+
"在线玩家:\n"
38+
),
39+
Plain(
40+
"\n".join(i.name for i in players) if players is not None else "无"
41+
),
42+
]
43+
)
44+
del server, stat
45+
await app.sendMessage(event, reply)

pdm.lock

Lines changed: 12 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,29 @@
11
[project]
22
name = ""
3-
version = {from = "lib/__init__.py"}
3+
version = { from = "lib/__init__.py" }
44
description = ""
5-
authors = [
6-
{name = "BlueGlassBlock", email = "[email protected]"},
7-
]
8-
dependencies = [
9-
"graia-ariadne[full]>=0.5.3.post3",
10-
"toml>=0.10.2",
11-
"peewee>=3.14.8",
12-
]
5+
authors = [{ name = "BlueGlassBlock", email = "[email protected]" }]
6+
dependencies = ["graia-ariadne[full]>=0.6.0", "toml>=0.10.2", "peewee>=3.14.8"]
137
requires-python = ">=3.9"
14-
license = {text = "AGPL-3.0"}
8+
license = { text = "AGPL-3.0" }
159

1610
[project.urls]
1711
homepage = ""
1812

1913
[project.optional-dependencies]
20-
plugin = [
21-
"pillow>=9.0.1",
22-
"mcstatus>=6.5.0",
23-
]
14+
plugin = ["pillow>=9.0.1", "mcstatus>=6.5.0"]
15+
2416
[tool]
17+
2518
[tool.pdm]
19+
2620
[tool.pdm.dev-dependencies]
2721
dev = [
2822
"black>=22.1.0",
2923
"isort>=5.10.1",
3024
"flake8>=4.0.1",
3125
]
26+
3227
[tool.pdm.scripts]
3328
xenon = "python main.py"
3429

@@ -40,4 +35,4 @@ build-backend = "pdm.pep517.api"
4035
profile = "black"
4136

4237
[tool.black]
43-
line-length = "110"
38+
line-length = "110"

0 commit comments

Comments
 (0)