️ 优化自检插件逻辑,增加规则判断,添加nonebug配置 (#1792)

* ⬆️ Expand the range of nonebug version restrictions and update nonebot-plugin-alconna.

*  Update pytest configuration.

*  Add pytest hook to tag async tests with session-scoped event loop.

* ️ 优化自检插件逻辑,增加规则判断

---------

Co-authored-by: BalconyJH <balconyjh@gmail.com>
This commit is contained in:
HibiKier 2024-12-23 10:09:06 +08:00 committed by GitHub
parent 3a197c0c1d
commit 4291cda244
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 55 additions and 40 deletions

22
poetry.lock generated
View File

@ -2227,23 +2227,23 @@ reference = "aliyun"
[[package]]
name = "nonebot-plugin-alconna"
version = "0.53.1"
version = "0.54.1"
description = "Alconna Adapter for Nonebot"
optional = false
python-versions = ">=3.9"
files = [
{file = "nonebot_plugin_alconna-0.53.1-py3-none-any.whl", hash = "sha256:a7734d4c7f1b2fedec586b7d235da119ffaa23d0e17a8aa0a0acc0e76a1662d0"},
{file = "nonebot_plugin_alconna-0.53.1.tar.gz", hash = "sha256:dc9eef228bb819d05ce9816f8a5a1bead455ee85d1ea7d5e409f9fb11665805e"},
{file = "nonebot_plugin_alconna-0.54.1-py3-none-any.whl", hash = "sha256:4edb4b081cd64ce37717c7a92d31aadd2cf287a5a0adc2ac86ed82d9bcad5048"},
{file = "nonebot_plugin_alconna-0.54.1.tar.gz", hash = "sha256:66fae03120b8eff25bb0027d65f149e399aa6f73c7585ebdd388d1904cecdeee"},
]
[package.dependencies]
arclet-alconna = ">=1.8.31,<2.0"
arclet-alconna = ">=1.8.35,<2.0"
arclet-alconna-tools = ">=0.7.10"
importlib-metadata = ">=4.13.0"
nepattern = ">=0.7.4,<1.0"
nepattern = ">=0.7.7,<1.0"
nonebot-plugin-waiter = ">=0.6.0"
nonebot2 = ">=2.3.0"
tarina = ">=0.6.3,<0.7"
tarina = ">=0.6.8,<0.7"
[package.source]
type = "legacy"
@ -2420,13 +2420,13 @@ reference = "aliyun"
[[package]]
name = "nonebug"
version = "0.3.7"
version = "0.4.3"
description = "nonebot2 test framework"
optional = false
python-versions = ">=3.8,<4.0"
python-versions = ">=3.9,<4.0"
files = [
{file = "nonebug-0.3.7-py3-none-any.whl", hash = "sha256:c39f462aafe20660602a8b789a575db6c9346ab5b6f1985eb9d98b861528299a"},
{file = "nonebug-0.3.7.tar.gz", hash = "sha256:8a75183400681f34eafc7caa2bb6dd511c3b5660c59264f1c379a088c7ac2247"},
{file = "nonebug-0.4.3-py3-none-any.whl", hash = "sha256:eb9b2c8ab3d45459a4f00ebdaae90729e9e9628575c0685fca4c871dd4cfd425"},
{file = "nonebug-0.4.3.tar.gz", hash = "sha256:e9592d2c7a42b76f4a336f98726cba92e1300f6bab155c8822e865919786f10c"},
]
[package.dependencies]
@ -4932,4 +4932,4 @@ reference = "aliyun"
[metadata]
lock-version = "2.0"
python-versions = "^3.10"
content-hash = "d2be32cc7ba17b302a015de327b8e8d815bee185400547f4024fd93e141d3ac8"
content-hash = "37341271bbd43fcb6af878cb09491459cf21b13c24659a5fd94f9c2b5248a5d8"

View File

@ -50,7 +50,7 @@ nonebot-plugin-uninfo = "^0.4.1"
nonebot-plugin-alconna = "^0.54.0"
[tool.poetry.dev-dependencies]
nonebug = "^0.4.2"
nonebug = "^0.4"
pytest-cov = "^5.0.0"
pytest-mock = "^3.6.1"
pytest-asyncio = "^0.23.5"
@ -137,6 +137,7 @@ disableBytesTypePromotions = true
[tool.pytest.ini_options]
asyncio_mode = "auto"
asyncio_default_fixture_loop_scope = "session"
[build-system]
requires = ["poetry-core>=1.0.0"]

View File

@ -57,7 +57,7 @@ nonebot-adapter-discord==0.1.8 ; python_version >= "3.10" and python_version < "
nonebot-adapter-dodo==0.1.4 ; python_version >= "3.10" and python_version < "4.0"
nonebot-adapter-kaiheila==0.3.4 ; python_version >= "3.10" and python_version < "4.0"
nonebot-adapter-onebot==2.4.6 ; python_version >= "3.10" and python_version < "4.0"
nonebot-plugin-alconna==0.53.1 ; python_version >= "3.10" and python_version < "4.0"
nonebot-plugin-alconna==0.54.1 ; python_version >= "3.10" and python_version < "4.0"
nonebot-plugin-apscheduler==0.3.0 ; python_version >= "3.10" and python_version < "4.0"
nonebot-plugin-htmlrender==0.3.5 ; python_version >= "3.10" and python_version < "4.0"
nonebot-plugin-session==0.2.3 ; python_version >= "3.10" and python_version < "4.0"

View File

@ -8,6 +8,7 @@ from nonebug import NONEBOT_INIT_KWARGS
from nonebug.app import App
from nonebug.mixin.process import MatcherContext
import pytest
from pytest_asyncio import is_async_test
from pytest_mock import MockerFixture
from respx import MockRouter
@ -22,6 +23,13 @@ def get_response_json(path: str) -> dict:
)
def pytest_collection_modifyitems(items: list[pytest.Item]):
pytest_asyncio_tests = (item for item in items if is_async_test(item))
session_scope_marker = pytest.mark.asyncio(loop_scope="session")
for async_test in pytest_asyncio_tests:
async_test.add_marker(session_scope_marker, append=False)
def pytest_configure(config: pytest.Config) -> None:
config.stash[NONEBOT_INIT_KWARGS] = {
"driver": "~fastapi+~httpx+~websockets",

View File

@ -2,7 +2,7 @@ from nonebot import on_notice
from nonebot.adapters.onebot.v11 import PokeNotifyEvent
from nonebot.permission import SUPERUSER
from nonebot.plugin import PluginMetadata
from nonebot.rule import to_me
from nonebot.rule import Rule, to_me
from nonebot_plugin_alconna import Alconna, on_alconna
from nonebot_plugin_htmlrender import template_to_pic
@ -41,9 +41,32 @@ __plugin_meta__ = PluginMetadata(
)
def commandRule() -> Rule:
return Rule(lambda: Config.get_config("check", "type") in {"message", "mix"})
def noticeRule() -> Rule:
return Rule(lambda: Config.get_config("check", "type") in {"poke", "mix"})
_self_check_matcher = _self_check_matcher = on_alconna(
Alconna("自检"),
rule=to_me() & commandRule(),
permission=SUPERUSER,
block=True,
priority=1,
)
_self_check_poke_matcher = on_notice(
priority=5,
permission=SUPERUSER,
block=False,
rule=notice_rule(PokeNotifyEvent) & to_me() & noticeRule(),
)
async def handle_self_check():
try:
logger.info("触发自检")
data = await get_status_info()
image = await template_to_pic(
template_path=str((TEMPLATE_PATH / "check").absolute()),
@ -56,34 +79,17 @@ async def handle_self_check():
wait=2,
)
await MessageUtils.build_message(image).send()
logger.info("自检成功")
logger.info("自检成功", "自检")
except Exception as e:
await MessageUtils.build_message(f"自检失败: {e}").send()
logger.error("自检失败", e=e)
logger.error("自检失败", "自检", e=e)
check_type = Config.get_config("check", "type")
if check_type in {"message", "mix"}:
# message
_self_check_matcher = on_alconna(
Alconna("自检"), rule=to_me(), permission=SUPERUSER, block=True, priority=1
)
@_self_check_matcher.handle()
async def handle_message_check():
await handle_self_check()
@_self_check_matcher.handle()
async def handle_message_check():
await handle_self_check()
if check_type in {"poke", "mix"}:
# poke
_self_check_poke_matcher = on_notice(
priority=5,
permission=SUPERUSER,
block=False,
rule=notice_rule(PokeNotifyEvent) & to_me(),
)
@_self_check_poke_matcher.handle()
async def handle_poke_check(event: PokeNotifyEvent):
await handle_self_check()
@_self_check_poke_matcher.handle()
async def handle_poke_check():
await handle_self_check()