Discord bot中有关添加角色的命令不起作用

时间:2020-05-08 19:57:35

标签: python bots discord

所以我想在我的Discord机器人中添加一个命令,您可以在其中向成员添加角色。那就是错误。

@bot.command()
@commands.has_permissions(administrator=True)
async def approve(ctx, arg1, member: discord.Member = None):
    if not arg1:
        await ctx.send("Please specify the role")
    if not member:
        await ctx.send("Please specify a member")
    await ctx.channel.purge(limit=1)
    user = ctx.message.author
    role = discord.utils.get(ctx.guild.roles, name=arg1)
    await member.add_roles(role)
    await ctx.send(f"Congratulations {member.mention} :tada:"
                   f"You successfully passed the test for {arg1} :hugging: \n"
                   f"Tested by {user.name}.")

但是我一直收到此错误:

Ignoring exception in command approve:
Traceback (most recent call last):
  File "C:\Users\gg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 83, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:/Users/gg/Desktop/Στέλιος/phoenix/phoenix-bot.py", line 29, in approve
    await member.add_roles(role)
  File "C:\Users\gg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\member.py", line 641, in add_roles
    await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\gg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\gg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 797, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\gg\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 92, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'id'

我该怎么办才能起作用

0 个答案:

没有答案