Search code examples
pythonpython-3.xexceptiondiscorddiscord.py

discord.py MemberNotFound exception when passing a real member


When I pass *grant @user add in Discord I get the following exception:

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.

@user is a mention of myself in Discord and it's being recognized by Discord, tho not by my bot when converting to a Member. grant function's code (tho it's a private room perms system):

@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 😝')

Solution

  • You kind of messed up with the order of your arguments in your code I believe.

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

    I think it should be this instead, so you can separate the command and the member.

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