chore:添加群管理命令的取消功能以及测试

This commit is contained in:
Azide 2022-06-02 00:24:51 +08:00
parent b623943b77
commit dffbc17349
2 changed files with 96 additions and 8 deletions

View File

@ -353,7 +353,7 @@ async def send_group_list(bot: Bot, event: PrivateMessageEvent, state: T_State):
for idx, group in enumerate(groups, 1):
group_number_idx[idx] = group["group_id"]
res_text += f'{idx}. {group["group_id"]} - {group["group_name"]}\n'
res_text += "请输入左侧序号"
res_text += "请输入左侧序号\n中止操作请输入'取消'"
# await group_manage_matcher.send(res_text)
state["_prompt"] = res_text
state["group_number_idx"] = group_number_idx
@ -365,11 +365,16 @@ async def _parse_group_idx(state: T_State, event_msg: str = EventPlainText()):
group_number_idx: Optional[dict[int, int]] = state.get("group_number_idx")
assert group_number_idx
try:
assert event_msg != "取消", "userAbort"
idx = int(event_msg)
assert idx in group_number_idx.keys()
assert idx in group_number_idx.keys(), "idxNotInList"
state["group_idx"] = idx
except:
await group_manage_matcher.reject("请输入正确序号")
except AssertionError as AE:
errType = AE.args[0]
if errType == "userAbort":
await group_manage_matcher.finish("已取消")
elif errType == "idxNotInList":
await group_manage_matcher.reject("请输入正确序号")
@group_manage_matcher.got(
@ -383,13 +388,13 @@ async def do_choose_group_number(state: T_State):
async def _check_command(event_msg: str = EventPlainText()):
if event_msg not in {"添加订阅", "查询订阅", "删除订阅"}:
if event_msg not in {"添加订阅", "查询订阅", "删除订阅", "取消"}:
await group_manage_matcher.reject("请输入正确的命令")
return
@group_manage_matcher.got(
"command", "请输入需要使用的命令:添加订阅,查询订阅,删除订阅", [Depends(_check_command)]
"command", "请输入需要使用的命令:添加订阅,查询订阅,删除订阅,取消", [Depends(_check_command)]
)
async def do_dispatch_command(
bot: Bot,
@ -398,6 +403,8 @@ async def do_dispatch_command(
matcher: Matcher,
command: str = ArgStr(),
):
if command == "取消":
await group_manage_matcher.finish("已取消")
permission = await matcher.update_permission(bot, event)
new_matcher = Matcher.new(
"message",

View File

@ -25,7 +25,9 @@ async def test_query_with_superuser_private(app: App):
"get_group_list", {}, [{"group_id": 101, "group_name": "test group"}]
)
ctx.should_call_send(
event, Message("请选择需要管理的群:\n1. 101 - test group\n请输入左侧序号"), True
event,
Message("请选择需要管理的群:\n1. 101 - test group\n请输入左侧序号\n中止操作请输入'取消'"),
True,
)
event_1_err = fake_private_message_event(
message=Message("0"),
@ -43,7 +45,7 @@ async def test_query_with_superuser_private(app: App):
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event_1_ok)
ctx.should_call_send(event_1_ok, "请输入需要使用的命令:添加订阅,查询订阅,删除订阅", True)
ctx.should_call_send(event_1_ok, "请输入需要使用的命令:添加订阅,查询订阅,删除订阅,取消", True)
event_2_err = fake_private_message_event(
message=Message("222"),
sender=fake_superuser,
@ -64,6 +66,85 @@ async def test_query_with_superuser_private(app: App):
ctx.should_pass_permission()
@pytest.mark.asyncio
async def test_query_with_abort_on_idx(app: App):
from nonebot.adapters.onebot.v11.bot import Bot
from nonebot.adapters.onebot.v11.message import Message
from nonebot_bison.config_manager import group_manage_matcher
async with app.test_matcher(group_manage_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_private_message_event(
message=Message("群管理"),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event)
ctx.should_pass_rule()
ctx.should_pass_permission()
ctx.should_call_api(
"get_group_list", {}, [{"group_id": 101, "group_name": "test group"}]
)
ctx.should_call_send(
event,
Message("请选择需要管理的群:\n1. 101 - test group\n请输入左侧序号\n中止操作请输入'取消'"),
True,
)
event_abort = fake_private_message_event(
message=Message("取消"),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event_abort)
ctx.should_call_send(event_abort, "已取消", True)
ctx.should_finished()
async def test_query_with_abort_on_command(app: App):
from nonebot.adapters.onebot.v11.bot import Bot
from nonebot.adapters.onebot.v11.message import Message
from nonebot_bison.config_manager import group_manage_matcher
async with app.test_matcher(group_manage_matcher) as ctx:
bot = ctx.create_bot(base=Bot)
event = fake_private_message_event(
message=Message("群管理"),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event)
ctx.should_pass_rule()
ctx.should_pass_permission()
ctx.should_call_api(
"get_group_list", {}, [{"group_id": 101, "group_name": "test group"}]
)
ctx.should_call_send(
event,
Message("请选择需要管理的群:\n1. 101 - test group\n请输入左侧序号\n中止操作请输入'取消'"),
True,
)
event_1_ok = fake_private_message_event(
message=Message("1"),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event_1_ok)
ctx.should_call_send(event_1_ok, "请输入需要使用的命令:添加订阅,查询订阅,删除订阅,取消", True)
event_abort = fake_private_message_event(
message=Message("取消"),
sender=fake_superuser,
to_me=True,
user_id=fake_superuser.user_id,
)
ctx.receive_event(bot, event_abort)
ctx.should_call_send(event_abort, "已取消", True)
ctx.should_finished()
@pytest.mark.asyncio
async def test_query_with_superuser_group_tome(app: App):
from nonebot.adapters.onebot.v11.bot import Bot