From 7b89973de461e7e8013b44e583b214664e3d76c7 Mon Sep 17 00:00:00 2001 From: Azide Date: Fri, 8 Mar 2024 13:55:18 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E4=BD=BF=E7=94=A8=20yarl.URL=20?= =?UTF-8?q?=E5=A4=84=E7=90=86=20outer=5Furl?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nonebot_bison/admin_page/__init__.py | 2 +- nonebot_bison/plugin_config.py | 8 ++++++-- tests/test_admin_page.py | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/nonebot_bison/admin_page/__init__.py b/nonebot_bison/admin_page/__init__.py index cb5f29c0a..4c2d4e682 100644 --- a/nonebot_bison/admin_page/__init__.py +++ b/nonebot_bison/admin_page/__init__.py @@ -66,7 +66,7 @@ def register_get_token_handler(): @get_token.handle() async def send_token(bot: "Bot", event: PrivateMessageEvent, state: T_State): token = tm.get_user_token((event.get_user_id(), event.sender.nickname)) - await get_token.finish(f"请访问: {plugin_config.outer_url}auth/{token}") + await get_token.finish(f"请访问: {plugin_config.outer_url / 'auth' / token}") get_token.__help__name__ = "获取后台管理地址" # type: ignore get_token.__help__info__ = "获取管理bot后台的地址,该地址会在一段时间过后过期,请不要泄漏该地址" # type: ignore diff --git a/nonebot_bison/plugin_config.py b/nonebot_bison/plugin_config.py index 60b179b66..a0734eb38 100644 --- a/nonebot_bison/plugin_config.py +++ b/nonebot_bison/plugin_config.py @@ -1,4 +1,5 @@ import nonebot +from yarl import URL from nonebot import get_plugin_config from pydantic import Field, BaseModel @@ -32,8 +33,11 @@ class PlugConfig(BaseModel): bison_platform_theme: dict[PlatformName, ThemeName] = {} @property - def outer_url(self) -> str: - return self.bison_outer_url or f"http://localhost:{global_config.port}/bison/" + def outer_url(self) -> URL: + if self.bison_outer_url: + return URL(self.bison_outer_url) + else: + return URL(f"http://localhost:{global_config.port}/bison/") plugin_config = get_plugin_config(PlugConfig) diff --git a/tests/test_admin_page.py b/tests/test_admin_page.py index 81f35b093..8874f3f08 100644 --- a/tests/test_admin_page.py +++ b/tests/test_admin_page.py @@ -28,12 +28,12 @@ async def test_command(app: App): to_me=True, ) ctx.receive_event(bot, event_1) - ctx.should_call_send(event_1, f"请访问: {plugin_config.outer_url}auth/test_token", True) + ctx.should_call_send(event_1, f"请访问: {plugin_config.outer_url / 'auth' / 'test_token'}", True) ctx.should_finished() event_2 = fake_private_message_event(message=Message("管理后台"), sender=fake_admin_user, to_me=True) ctx.receive_event(bot, event_2) - ctx.should_call_send(event_2, f"请访问: {plugin_config.outer_url}auth/test_token", True) + ctx.should_call_send(event_2, f"请访问: {plugin_config.outer_url / 'auth' / 'test_token'}", True) ctx.should_finished()