简体   繁体   中英

How do I change a role color using a hex color code argument?

I'm trying to change the color of role using a command with a hex color code argument (the name of role is the name of the user). I tried the following code but the bot does nothing. It has admin permissions and discord.Intents.all()

@client.command(name="color")
async def role_color(ctx, arg):
    name = ctx.author.name
    guild_id = ctx.guild.id
    guild = discord.utils.find(lambda g: g.id == guild_id, client.guilds)
    role = discord.utils.get(guild.roles, name=name)
    await role.edit(color=f"0x{arg}")

As described in Role.edit , you have to pass in an instance of discord.Colour or an int value. This means you have to convert arg . Thankfully, you are expecting arg to be hexcode so we can just convert it to int right away (no need to add "0x").

await role.edit(color=int(arg, 16))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM