【问题标题】:How to create multiple roles through discord.py bot?如何通过 discord.py bot 创建多个角色?
【发布时间】:2021-05-15 23:11:20
【问题描述】:

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

@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

任何帮助将不胜感激!

【问题讨论】:

  • 您能与我们分享您遇到的错误吗?或者至少是使用该命令的输出。
  • 哦,谢谢你的提醒,我就是这么做的。
  • 您的命令不起作用,因为您有错字。 guild.create_role 的参数名称应为 colour,而不是 color
  • 即使将其更改为colour,它也不起作用。帮忙?
  • 你仍然得到同样的错误?

标签: python discord.py


【解决方案1】:

我有点晚了,但这是解决方案。 您使用discord.Color.value 有点错误。对于颜色,您可以使用HEXRGB 代码。 对于 RGB 代码 Discord 具有此功能: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)

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 2018-03-18
    • 1970-01-01
    • 2022-01-09
    • 1970-01-01
    • 2021-06-05
    • 2018-11-26
    • 2019-10-20
    • 2020-09-13
    相关资源
    最近更新 更多