Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
347 views
in Technique[技术] by (71.8m points)

python - How to create multiple roles through discord.py bot?

I have been trying to make my discord bot create multiple roles through a command. But it simply doesn't work. Here is what I have done so far:

@commands.command()
    async def create_roles(self, ctx):
        guild = self.client.get_guild(783547639944839178)
        channel = self.client.get_channel(809683610058752010)
        await guild.create_role(name="red", color=discord.Color.value('F51720'))
        await guild.create_role(name="skyblue", color=discord.Colour.value('11A7BB'))
        await guild.create_role(name="yellow", color=discord.Colour.value('F8D210'))
        await channel.send("Done.")

When I run this code I get this error:

Ignoring exception in command create_roles:
Traceback (most recent call last):
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:Userswave computerPycharmProjectspythonProjectcogs
oles.py", line 14, in create_roles
    await guild.create_role(name="red", colour=discord.Colour.value('F51720'))
TypeError: 'member_descriptor' object is not callable

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

Traceback (most recent call last):
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandsot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:Userswave computerPycharmProjectspythonProjectvenvlibsite-packagesdiscordextcommandscore.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'member_descriptor' object is not callable

Any help would be appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I am a bit late, but here is the solution. You used discord.Color.value a bit wrong. For the colors you can use either a HEX or RGB code. For a RGB code Discord has this function: discord.Color.from_rgb() and for a hex code you can simply define the color, for example: customred = FF0000. For your code I took once the RGB variant:

@commands.command()
async def create_roles(self, ctx):
    guild = self.client.get_guild(GuildID)
    channel = self.client.get_channel(ChannelID)
    await guild.create_role(name="red", color=discord.Color.from_rgb(245, 23, 32))
    await guild.create_role(name="skyblue", color=discord.Color.from_rgb(17, 167, 187))
    await guild.create_role(name="yellow", color=discord.Colour.from_rgb(248, 210, 16))
    await ctx.send("Done.")

You can of course define the color beforehand - cred = discord.Colour.from_rgb(248, 210, 16)) - and then use only cred as color:

await guild.create_role(name="red", color=cred)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...