如何在创建时为角色添加随机颜色? (不和谐.py)

时间:2021-07-22 02:38:48

标签: python discord.py

在创建我的机器人时,我想在角色创建类型的事物上添加随机颜色。 这是我当前的代码:

import discord
from discord.ext import commands
import random
from discord import Permissions
from colorama import Fore
import asyncio
#imports for reference


@client.command()
@commands.is_owner()
async def role(ctx):
      guild = ctx.guild
      name = 'name'
      created = 0
      for _ in range(200 - len(guild.roles)):
       try:
        await guild.create_role(name=name)
        created += 1
       except:
            continue
      return created
#Solved, as with adding what FoxGames01 said, and removing the try it works.
  

1 个答案:

答案 0 :(得分:0)

Discord.py 带有一个 Generate a random color 函数。

将第 await guild.create_role(name=name) 行更改为 await guild.create_role(name=name, colour = discord.Color.random()),这样就可以了。