传递真实成员时出现discord.py MemberNotFound异常

Posted

技术标签:

【中文标题】传递真实成员时出现discord.py MemberNotFound异常【英文标题】:discord.py MemberNotFound exception when passing a real member 【发布时间】:2021-04-27 22:27:24 【问题描述】:

当我在 Discord 中通过 *grant @wolex9 add 时,我得到以下异常:

Ignoring exception in command grant:
Traceback (most recent call last):
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 856, in invoke
    await self.prepare(ctx)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 790, in prepare
    await self._parse_arguments(ctx)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 706, in _parse_arguments
    kwargs[name] = await self.transform(ctx, param)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 552, in transform
    return await self.do_conversion(ctx, converter, argument, param)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 505, in do_conversion
    return await self._actual_conversion(ctx, converter, argument, param)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/core.py", line 451, in _actual_conversion
    ret = await instance.convert(ctx, argument)
  File "/Users/test/PycharmProjects/slapdash/venv/lib/python3.8/site-packages/discord/ext/commands/converter.py", line 191, in convert
    raise MemberNotFound(argument)
discord.ext.commands.errors.MemberNotFound: Member "<@!id> add" not found.

@wolex9 是在 Discord 中提及我自己,它被 Discord 识别,而不是在转换为会员时被我的机器人识别。 grant函数代码(虽然是私人房间烫发系统):

@bot.command()
async def grant(ctx, *, member: discord.Member = None, command=None):
    if (
            member is not None and  # if member argument provided
            command is not None and  # if command argument provided
            ctx.channel.category_id in instances and  # if in dictionary
            member.id != instances[ctx.channel.category_id]['admin_id'] and  # if not admin
            command in ['add', 'remove', 'exit', 'grant', 'deny'] and
            (
                    ctx.author.id == instances[ctx.channel.category_id]['admin_id'] or  # if admin
                    ctx.author.id in instances[ctx.channel.category_id]['granted']['grant_id_list']  # if granted
            )
    ):
        if command == 'add' and member.id not in instances[ctx.channel.category_id]['granted']['add_id_list']:
            instances[ctx.channel.category_id]['granted']['add_id_list'].append(member.id)
            await ctx.send(f"Successfully granted to member.mention, but you'd better grant `remove` as well!")
    elif (
            member is not None and  # if member argument provided
            command is None and  # if command argument not provided
            ctx.channel.category_id in instances and  # if in dictionary
            member.id != instances[ctx.channel.category_id]['admin_id'] and  # if not admin
            (
                ctx.author.id == instances[ctx.channel.category_id]['admin_id'] or  # if admin
                ctx.author.id in instances[ctx.channel.category_id]['granted']['grant_id_list']  # if granted
            )
    ):
        if member.id not in instances[ctx.channel.category_id]['granted']['add_id_list']:  # if not add
            instances[ctx.channel.category_id]['granted']['add_id_list'].append(member.id)
        if member.id not in instances[ctx.channel.category_id]['granted']['remove_id_list']:  # if not remove
            instances[ctx.channel.category_id]['granted']['remove_id_list'].append(member.id)
        await ctx.send(f"Successfully granted `add` and `remove` to member.mention!")
    else:
        await ctx.send('Use `*help` to learn commands ????')

【问题讨论】:

这是一个非常混乱的代码,请编辑您的答案以便于阅读 @ŁukaszKwieciński 现在好点了吗?我删除了不相关的 elifs 【参考方案1】:

我相信你的代码中参数的顺序有点混乱。

async def grant(ctx, *, member: discord.Member = None, command=None):

我认为应该是这样,所以你可以将命令和成员分开。

async def grant(ctx, member: discord.Member = None, *, command=None):

【讨论】:

非常感谢,原来是这个问题!可悲的是,我不能赞成你的回答(

以上是关于传递真实成员时出现discord.py MemberNotFound异常的主要内容,如果未能解决你的问题,请参考以下文章

向新加入的 Discord 成员公开问候

尝试制作 discord.py 级别机器人时出现错误消息

在 discord.py 中获取成员的角色

在 gcc 上将成员函数指针传递给模板成员函数时出现问题

使用 %s 时出现不支持的操作数类型错误

Discord.py SQLite3 禁用词系统 - 问题