此更改角色的代码不起作用

时间:2020-08-17 19:37:32

标签: discord.py

我得到了这段代码来更改用户角色,但是它不起作用。

@client.command(pass_context=True)
async def ruleBreak(ctx):
    member = ctx.message.author
    role = discord.utils.get(member.server.roles, name="RuleBreakers")
    await client.add_roles(member, role)

但这是导致此错误的原因

Ignoring exception in command ruleBreak:
Traceback (most recent call last):
  File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/unkno/Desktop/Code_Tests/Python/discord_bot/WIP/bot2.py", line 19, in ruleBreak
    role = discord.utils.get(member.server.roles, name="RuleBreakers")
AttributeError: 'Member' object has no attribute 'server'

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

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

我该怎么办,为什么会发生这种情况,我该在哪里更改?

谢谢!

1 个答案:

答案 0 :(得分:0)

您不需要传递上下文,因为它已在重写分支中完成。

@client.command()
async def ruleBreak(ctx):
    member = ctx.message.author
    role = discord.utils.get(member.guild.roles, name="RuleBreakers")
    await discord.Member.add_roles(member, role)

编辑:这里的主要问题是在重写版本中Client.add_roles更改为Member.add_roles()Migrating to v1.0