繁体   English   中英

如何通过 discord.py bot 创建多个角色?

[英]How to create multiple roles through discord.py bot?

我一直试图让我的 discord 机器人通过命令创建多个角色。 但这根本行不通。 这是我到目前为止所做的:

@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.")

当我运行此代码时,我收到此错误:

Ignoring exception in command create_roles:
Traceback (most recent call last):
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\cogs\roles.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:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\lib\site-packages\discord\ext\commands\core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\wave computer\PycharmProjects\pythonProject\venv\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: TypeError: 'member_descriptor' object is not callable

任何帮助,将不胜感激!

我有点晚了,但这是解决方案。 您使用discord.Color.value有点错误。 对于 colors,您可以使用HEXRGB代码。 对于 RGB 代码 Discord 有这个 function: discord.Color.from_rgb()对于十六进制代码,您可以简单地定义颜色: customred = FF0000 ,例如对于您的代码,我使用了一次 RGB 变体:

@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.")

您当然可以事先定义颜色 - cred = discord.Colour.from_rgb(248, 210, 16)) - 然后只使用cred作为颜色:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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