mirror of
https://github.com/suyiiyii/nonebot-bison.git
synced 2025-06-08 04:43:00 +08:00
chore:添加群管理命令的取消功能以及测试
This commit is contained in:
parent
b623943b77
commit
dffbc17349
@ -353,7 +353,7 @@ async def send_group_list(bot: Bot, event: PrivateMessageEvent, state: T_State):
|
|||||||
for idx, group in enumerate(groups, 1):
|
for idx, group in enumerate(groups, 1):
|
||||||
group_number_idx[idx] = group["group_id"]
|
group_number_idx[idx] = group["group_id"]
|
||||||
res_text += f'{idx}. {group["group_id"]} - {group["group_name"]}\n'
|
res_text += f'{idx}. {group["group_id"]} - {group["group_name"]}\n'
|
||||||
res_text += "请输入左侧序号"
|
res_text += "请输入左侧序号\n中止操作请输入'取消'"
|
||||||
# await group_manage_matcher.send(res_text)
|
# await group_manage_matcher.send(res_text)
|
||||||
state["_prompt"] = res_text
|
state["_prompt"] = res_text
|
||||||
state["group_number_idx"] = group_number_idx
|
state["group_number_idx"] = group_number_idx
|
||||||
@ -365,10 +365,15 @@ async def _parse_group_idx(state: T_State, event_msg: str = EventPlainText()):
|
|||||||
group_number_idx: Optional[dict[int, int]] = state.get("group_number_idx")
|
group_number_idx: Optional[dict[int, int]] = state.get("group_number_idx")
|
||||||
assert group_number_idx
|
assert group_number_idx
|
||||||
try:
|
try:
|
||||||
|
assert event_msg != "取消", "userAbort"
|
||||||
idx = int(event_msg)
|
idx = int(event_msg)
|
||||||
assert idx in group_number_idx.keys()
|
assert idx in group_number_idx.keys(), "idxNotInList"
|
||||||
state["group_idx"] = idx
|
state["group_idx"] = idx
|
||||||
except:
|
except AssertionError as AE:
|
||||||
|
errType = AE.args[0]
|
||||||
|
if errType == "userAbort":
|
||||||
|
await group_manage_matcher.finish("已取消")
|
||||||
|
elif errType == "idxNotInList":
|
||||||
await group_manage_matcher.reject("请输入正确序号")
|
await group_manage_matcher.reject("请输入正确序号")
|
||||||
|
|
||||||
|
|
||||||
@ -383,13 +388,13 @@ async def do_choose_group_number(state: T_State):
|
|||||||
|
|
||||||
|
|
||||||
async def _check_command(event_msg: str = EventPlainText()):
|
async def _check_command(event_msg: str = EventPlainText()):
|
||||||
if event_msg not in {"添加订阅", "查询订阅", "删除订阅"}:
|
if event_msg not in {"添加订阅", "查询订阅", "删除订阅", "取消"}:
|
||||||
await group_manage_matcher.reject("请输入正确的命令")
|
await group_manage_matcher.reject("请输入正确的命令")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
@group_manage_matcher.got(
|
@group_manage_matcher.got(
|
||||||
"command", "请输入需要使用的命令:添加订阅,查询订阅,删除订阅", [Depends(_check_command)]
|
"command", "请输入需要使用的命令:添加订阅,查询订阅,删除订阅,取消", [Depends(_check_command)]
|
||||||
)
|
)
|
||||||
async def do_dispatch_command(
|
async def do_dispatch_command(
|
||||||
bot: Bot,
|
bot: Bot,
|
||||||
@ -398,6 +403,8 @@ async def do_dispatch_command(
|
|||||||
matcher: Matcher,
|
matcher: Matcher,
|
||||||
command: str = ArgStr(),
|
command: str = ArgStr(),
|
||||||
):
|
):
|
||||||
|
if command == "取消":
|
||||||
|
await group_manage_matcher.finish("已取消")
|
||||||
permission = await matcher.update_permission(bot, event)
|
permission = await matcher.update_permission(bot, event)
|
||||||
new_matcher = Matcher.new(
|
new_matcher = Matcher.new(
|
||||||
"message",
|
"message",
|
||||||
|
@ -25,7 +25,9 @@ async def test_query_with_superuser_private(app: App):
|
|||||||
"get_group_list", {}, [{"group_id": 101, "group_name": "test group"}]
|
"get_group_list", {}, [{"group_id": 101, "group_name": "test group"}]
|
||||||
)
|
)
|
||||||
ctx.should_call_send(
|
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(
|
event_1_err = fake_private_message_event(
|
||||||
message=Message("0"),
|
message=Message("0"),
|
||||||
@ -43,7 +45,7 @@ async def test_query_with_superuser_private(app: App):
|
|||||||
user_id=fake_superuser.user_id,
|
user_id=fake_superuser.user_id,
|
||||||
)
|
)
|
||||||
ctx.receive_event(bot, event_1_ok)
|
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(
|
event_2_err = fake_private_message_event(
|
||||||
message=Message("222"),
|
message=Message("222"),
|
||||||
sender=fake_superuser,
|
sender=fake_superuser,
|
||||||
@ -64,6 +66,85 @@ async def test_query_with_superuser_private(app: App):
|
|||||||
ctx.should_pass_permission()
|
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
|
@pytest.mark.asyncio
|
||||||
async def test_query_with_superuser_group_tome(app: App):
|
async def test_query_with_superuser_group_tome(app: App):
|
||||||
from nonebot.adapters.onebot.v11.bot import Bot
|
from nonebot.adapters.onebot.v11.bot import Bot
|
||||||
|
Loading…
x
Reference in New Issue
Block a user