首页 > 解决方案 > discord.py client.create_role 错误(AttributeError:“Bot”对象没有属性“create_role”)

问题描述

我在另一个问题中看到了这段代码。这是我的代码。

@client.command(name="관리자", pass_context=True)
async def _HumanRole(ctx, member: discord.Member=None):
    author = ctx.message.author
    await client.create_role(author.server, name="테러", permissions=discord.Permissions(permissions=8), colour=0xffffff)
    user = ctx.message.author
    role = discord.utils.get(user.server.roles, name="테러")
    await client.add_roles(user, role)
    await ctx.send("테러가 시작되었다.")

这是我的代码,但我收到了这个错误:

Ignoring exception in command 관리자:
Traceback (most recent call last):
  File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:\python\helper\helper.py", line 254, in _HumanRole
    await client.create_role(author.server, name="테러", permissions=discord.Permissions(permissions=8), colour=0xffffff)
AttributeError: 'Bot' object has no attribute 'create_role'

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

Traceback (most recent call last):
  File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\mychi\AppData\Local\Programs\Python\Python37\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: 'Bot' object has no attribute 'create_role'

我该如何解决?请编写所有代码。

标签: discord.py

解决方案


你需要使用ctx.guild.create_role. 试试这个:

@client.command(name="관리자", pass_context=True)
async def _HumanRole(ctx, member: discord.Member=None):
    author = ctx.message.author
    await ctx.guild.create_role(name="테러", permissions=discord.Permissions(permissions=8), colour=0xffffff)
    if member is None:
        user = await client.fetch_user(ctx.author.id)
        role = discord.utils.get(user.guild.roles, name="테러")
    else:
        role = discord.utils.get(member.guild.roles, name="테러")
    await client.add_roles(user, role)
    await ctx.send("테러가 시작되었다.")

推荐阅读