Skip to content

Commit

Permalink
add minecraft server module
Browse files Browse the repository at this point in the history
  • Loading branch information
BlueGlassBlock committed Feb 26, 2022
1 parent 208b9a0 commit c1dcd12
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .pdm.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[python]
path = "C:/Users/blueg/AppData/Local/Programs/Python/Python39/python.EXE"
path = "C:\\Users\\Lenovo\\AppData\\Local\\Programs\\Python\\Python39\\python.EXE"
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
],
"cSpell.words": [
"behaviour",
"behaviours"
"behaviours",
"mcstatus"
],
"restructuredtext.languageServer.disabled": true
}
45 changes: 45 additions & 0 deletions module/mc_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import List, Optional
from graia.ariadne.app import Ariadne
from graia.ariadne.event.message import MessageEvent
from graia.ariadne.message.chain import MessageChain
from graia.ariadne.message.element import Image, Plain
import mcstatus
from graia.saya import Channel
from mcstatus.pinger import PingResponse
from graia.ariadne.message.commander.saya import CommandSchema

channel = Channel.current()

@channel.use(
CommandSchema("[.server|服务器] {server_address:str}")
)
async def get_info(event: MessageEvent, app: Ariadne, server_address: str):
try:
server = mcstatus.MinecraftServer.lookup(server_address)
stat = await server.async_status()
players: Optional[List[PingResponse.Players.Player]] = stat.players.sample
except Exception as e:
reply = MessageChain.create(
[
Plain(f"{repr(e)}"),
]
)
else:
reply = MessageChain(
[
Plain(f"{server_address} 状态\n"),
Image(base64=stat.favicon.removeprefix("data:image/png;base64,"))
if stat.favicon is not None
else Plain(""),
Plain(
f"玩家数:{stat.players.online}/{stat.players.max}\n"
f"延迟:{stat.latency}ms\n"
"在线玩家:\n"
),
Plain(
"\n".join(i.name for i in players) if players is not None else "无"
),
]
)
del server, stat
await app.sendMessage(event, reply)
25 changes: 12 additions & 13 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 10 additions & 15 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,29 @@
[project]
name = ""
version = {from = "lib/__init__.py"}
version = { from = "lib/__init__.py" }
description = ""
authors = [
{name = "BlueGlassBlock", email = "[email protected]"},
]
dependencies = [
"graia-ariadne[full]>=0.5.3.post3",
"toml>=0.10.2",
"peewee>=3.14.8",
]
authors = [{ name = "BlueGlassBlock", email = "[email protected]" }]
dependencies = ["graia-ariadne[full]>=0.6.0", "toml>=0.10.2", "peewee>=3.14.8"]
requires-python = ">=3.9"
license = {text = "AGPL-3.0"}
license = { text = "AGPL-3.0" }

[project.urls]
homepage = ""

[project.optional-dependencies]
plugin = [
"pillow>=9.0.1",
"mcstatus>=6.5.0",
]
plugin = ["pillow>=9.0.1", "mcstatus>=6.5.0"]

[tool]

[tool.pdm]

[tool.pdm.dev-dependencies]
dev = [
"black>=22.1.0",
"isort>=5.10.1",
"flake8>=4.0.1",
]

[tool.pdm.scripts]
xenon = "python main.py"

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

[tool.black]
line-length = "110"
line-length = "110"

0 comments on commit c1dcd12

Please sign in to comment.