Skip to content

Commit

Permalink
⚡️ 优化自检插件逻辑,增加规则判断,添加nonebug配置 (#1792)
Browse files Browse the repository at this point in the history
* ⬆️ 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>
  • Loading branch information
HibiKier and BalconyJH authored Dec 23, 2024
1 parent 3a197c0 commit 4291cda
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 40 deletions.
22 changes: 11 additions & 11 deletions poetry.lock

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

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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

Expand All @@ -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",
Expand Down
60 changes: 33 additions & 27 deletions zhenxun/builtin_plugins/check/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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

Expand Down Expand Up @@ -41,9 +41,32 @@
)


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()),
Expand All @@ -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)


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
)
logger.error("自检失败", "自检", e=e)

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

0 comments on commit 4291cda

Please sign in to comment.