remove internal help_me, solve #26

This commit is contained in:
felinae98 2022-02-18 17:40:38 +08:00
parent 5eac7795d1
commit 5dc1059d6b
No known key found for this signature in database
GPG Key ID: 00C8B010587FF610
4 changed files with 87 additions and 13 deletions

View File

@ -1,3 +1,5 @@
import nonebot
from . import (
admin_page,
config,
@ -9,7 +11,8 @@ from . import (
types,
utils,
)
from .plugin_config import plugin_config
__help__version__ = "0.4.3"
__help__plugin__name__ = "nonebot_bison"
__usage__ = "本bot可以提供b站、微博等社交媒体的消息订阅详情" "请查看本bot文档或者at本bot发送“添加订阅”订阅第一个帐号"
__usage__ = f"本bot可以提供b站、微博等社交媒体的消息订阅详情请查看本bot文档或者{'at本bot' if plugin_config.bison_to_me else '' }发送“添加订阅”订阅第一个帐号,发送“查询订阅”或“删除订阅”管理订阅"

View File

@ -5,14 +5,16 @@ from nonebot.adapters import Event as AbstractEvent
from nonebot.adapters.onebot.v11 import Bot, Event
from nonebot.adapters.onebot.v11.message import Message
from nonebot.adapters.onebot.v11.permission import GROUP_ADMIN, GROUP_OWNER
from nonebot.internal.rule import Rule
from nonebot.matcher import Matcher
from nonebot.params import Depends
from nonebot.params import Depends, EventToMe
from nonebot.permission import SUPERUSER
from nonebot.rule import to_me
from nonebot.typing import T_State
from .config import Config
from .platform import check_sub_target, platform_manager
from .plugin_config import plugin_config
from .types import Category, Target
from .utils import parse_text
@ -23,6 +25,16 @@ def _gen_prompt_template(prompt: str):
return prompt
def _configurable_to_me(to_me: bool = EventToMe()):
if plugin_config.bison_to_me:
return to_me
else:
return True
configurable_to_me = Rule(_configurable_to_me)
common_platform = [
p.platform_name
for p in filter(
@ -31,14 +43,6 @@ common_platform = [
)
]
help_match = on_command("help", rule=to_me(), priority=5)
@help_match.handle()
async def send_help():
message = "使用方法:\n@bot 添加订阅(仅管理员)\n@bot 查询订阅\n@bot 删除订阅(仅管理员)"
await help_match.finish(Message(await parse_text(message)))
def do_add_sub(add_sub: Type[Matcher]):
@add_sub.handle()
@ -227,7 +231,10 @@ async def parse_group_number(event: AbstractEvent, state: T_State):
add_sub_matcher = on_command(
"添加订阅", rule=to_me(), permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER, priority=5
"添加订阅",
rule=configurable_to_me,
permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER,
priority=5,
)
do_add_sub(add_sub_matcher)
manage_add_sub_matcher = on_command("管理-添加订阅", permission=SUPERUSER, priority=5)
@ -241,7 +248,7 @@ async def add_sub_handle():
do_add_sub(manage_add_sub_matcher)
query_sub_matcher = on_command("查询订阅", rule=to_me(), priority=5)
query_sub_matcher = on_command("查询订阅", rule=configurable_to_me, priority=5)
do_query_sub(query_sub_matcher)
manage_query_sub_matcher = on_command("管理-查询订阅", permission=SUPERUSER, priority=5)
@ -255,7 +262,10 @@ do_query_sub(manage_query_sub_matcher)
del_sub_matcher = on_command(
"删除订阅", rule=to_me(), permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER, priority=5
"删除订阅",
rule=configurable_to_me,
permission=GROUP_ADMIN | GROUP_OWNER | SUPERUSER,
priority=5,
)
do_del_sub(del_sub_matcher)
manage_del_sub_matcher = on_command("管理-删除订阅", permission=SUPERUSER, priority=5)

View File

@ -14,6 +14,7 @@ class PlugConfig(BaseSettings):
bison_use_queue: bool = True
bison_outer_url: str = "http://localhost:8080/bison/"
bison_filter_log: bool = False
bison_to_me: bool = True
class Config:
extra = "ignore"

View File

@ -7,6 +7,66 @@ from .platforms.utils import get_json
from .utils import fake_admin_user, fake_group_message_event
@pytest.mark.asyncio
async def test_configurable_at_me_true_failed(app: App):
from nonebot.adapters.onebot.v11.bot import Bot
from nonebot.adapters.onebot.v11.message import Message
from nonebot_bison.config_manager import add_sub_matcher
from nonebot_bison.plugin_config import plugin_config
plugin_config.bison_to_me = True
async with app.test_matcher(add_sub_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event(
message=Message("添加订阅"), sender=fake_admin_user
)
ctx.receive_event(bot, event)
ctx.should_not_pass_rule()
ctx.should_pass_permission()
async with app.test_matcher(add_sub_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event(message=Message("添加订阅"), to_me=True)
ctx.receive_event(bot, event)
ctx.should_pass_rule()
ctx.should_not_pass_permission()
@pytest.mark.asyncio
async def test_configurable_at_me_false(app: App):
from nonebot.adapters.onebot.v11.bot import Bot
from nonebot.adapters.onebot.v11.message import Message
from nonebot_bison.config_manager import add_sub_matcher, common_platform
from nonebot_bison.platform import platform_manager
from nonebot_bison.plugin_config import plugin_config
plugin_config.bison_to_me = False
async with app.test_matcher(add_sub_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_group_message_event(
message=Message("添加订阅"), sender=fake_admin_user
)
ctx.receive_event(bot, event)
ctx.should_call_send(
event,
Message(
"请输入想要订阅的平台,目前支持,请输入冒号左边的名称:\n"
+ "".join(
[
"{}{}\n".format(
platform_name, platform_manager[platform_name].name
)
for platform_name in common_platform
]
)
+ "要查看全部平台请输入:“全部”"
),
True,
)
ctx.should_pass_rule()
ctx.should_pass_permission()
@pytest.mark.asyncio
@respx.mock
async def test_add_with_target(app: App):