Browse Source

初提交

Moeyuuko 2 years ago
commit
f438e5139c
2 changed files with 179 additions and 0 deletions
  1. 134 0
      .gitignore
  2. 45 0
      __init__.py

+ 134 - 0
.gitignore

@@ -0,0 +1,134 @@
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+pip-wheel-metadata/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+.cache
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+*.log
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+.python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.env
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+
+# vscode
+.vscode/
+

+ 45 - 0
__init__.py

@@ -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()