shrimp23
shrimp23

Reputation: 21

discord.py client.create_role error(AttributeError: 'Bot' object has no attribute 'create_role')

I saw this code in another question. This is my code.

@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("테러가 시작되었다.")

This is my code, but I got this error:

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'

How can I fix it? pls write all code.

Upvotes: 1

Views: 1048

Answers (1)

Aditya Tomar
Aditya Tomar

Reputation: 1639

You need to use ctx.guild.create_role. Try this out:

@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("테러가 시작되었다.")

Upvotes: 1

Related Questions