nonebot/adapter-onebot

同意加群邀请事件报错

Taokyla opened this issue · 4 comments

addbot = on_request()

@addbot.handle()
async def _(bot: Bot, event: GroupRequestEvent):
    if event.sub_type == 'invite':
        await event.approve(bot)

功能就是这样的,会有一个报错

02-05 15:13:25 [INFO] nonebot | Matcher(type='request') running complete
02-05 15:13:25 [ERROR] nonebot | Running Matcher(type='request') failed.
Traceback (most recent call last):
  File "bot.py", line 37, in <module>
    nonebot.run(app="__mp_main__:app")
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/__init__.py", line 273, in run
    get_driver().run(*args, **kwargs)
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/drivers/fastapi.py", line 172, in run
    uvicorn.run(
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/uvicorn/main.py", line 569, in run
    server.run()
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/uvicorn/server.py", line 60, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "/usr/lib/python3.8/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/message.py", line 142, in _check_matcher
    await _run_matcher(Matcher, bot, event, state, stack, dependency_cache)
> File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/message.py", line 188, in _run_matcher
    await matcher.run(bot, event, state, stack, dependency_cache)
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/internal/matcher/matcher.py", line 727, in run
    await self.simple_run(bot, event, state, stack, dependency_cache)
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/internal/matcher/matcher.py", line 702, in simple_run
    await handler(
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/dependencies/__init__.py", line 108, in __call__
    return await cast(Callable[..., Awaitable[R]], self.call)(**values)
  File "/home/ubuntu/QQbot/YomiNonebot/src/plugins/addbot/__init__.py", line 36, in _
    await event.approve(bot)
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/adapters/onebot/v11/event.py", line 520, in approve
    return await bot.set_group_add_request(
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/internal/adapter/bot.py", line 120, in call_api
    raise exception
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/internal/adapter/bot.py", line 98, in call_api
    result = await self.adapter._call_api(self, api, **data)
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/adapters/onebot/v11/adapter.py", line 132, in _call_api
    return handle_api_result(
  File "/home/ubuntu/.cache/pypoetry/virtualenvs/yomi-RV53Z6Ru-py3.8/lib/python3.8/site-packages/nonebot/adapters/onebot/v11/utils.py", line 58, in handle_api_result
    raise ActionFailed(**result)
nonebot.adapters.onebot.v11.exception.ActionFailed

可能是因为这个没有返回响应内容
image

ActionFailed请看gocq报错

gocq好像没有报错,确实有一个警告,可能是我的写法有问题。

addbot = on_request()


@addbot.handle()
async def _(bot: Bot, event: FriendRequestEvent):
    info = await bot.get_stranger_info(user_id=event.user_id, no_cache=True)
    if info:
        if info.get('level', 0) >= 64 or info.get('login_days', 0) > 30:
            await event.approve(bot)
        else:
            await event.reject(bot)


@addbot.handle()
async def _(bot: Bot, event: GroupRequestEvent):
    if event.sub_type == 'invite':
        await event.approve(bot)

完整的功能实现是这样的。

以下是gocq当时的日志

[2023-02-05 15:13:03] [INFO]: 收到来自 2887(00000000) 的好友请求: 我是朽木。
[2023-02-05 15:13:04] [INFO]: 添加了新好友: 朽木。(00000000)
[2023-02-05 15:13:25] [INFO]: 收到来自群 原神德云社(00000000) 内用户 玺之郎(00000000) 的加群邀请.
[2023-02-05 15:13:25] [WARNING]: 处理群系统消息失败: 无法操作已处理的消息.
[2023-02-05 15:13:25] [INFO]: Bot进入了群 原神德云社(00000000).

50人群以下自动同意邀请,所以你的bot已经自动入群了,才会有这个warning,无法操作已经入群。

明白了!