【问题标题】:Discord.py: Using Variable As Discord Embed colorDiscord.py:使用变量作为 Discord 嵌入颜色
【发布时间】:2021-03-31 19:27:51
【问题描述】:

所以我正在尝试为我的不和谐机器人创建一个嵌入构建器的命令。我希望命令的用户能够输入嵌入颜色的十六进制值。这是我尝试过的:

value = message.content

embed=discord.Embed(title='Hey', description="How are you?", color=value)
await output.edit(content=None, embed=embed)

但是当我这样做时,我得到了错误:

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: Expected discord.Colour, int, or Embed.Empty but received str instead.

我该如何解决这个问题?谢谢。

【问题讨论】:

  • 我认为这是一个令人惊奇的问题!当我开始编写 Discord 机器人时,我遇到了一个非常相似的问题,我花了几个小时搜索文档试图找到答案!

标签: python discord discord.py embed discord.py-rewrite


【解决方案1】:
@client.command()
async def embed(ctx, content, colour):

embed=discord.Embed(title='Embed', description=f"{content}", color=colour)
await output.edit(content=None, embed=embed)

这对你的情况有用吗?

【讨论】:

  • 至少一个例子
  • 不,在我的情况下,我接受嵌入的不同部分的不同响应,直到机器人开始着色。在这里你可以看到我在哪里等待输入:message = await self.bot.wait_for("message", check=check)
【解决方案2】:

您需要将message.content中的用户输入转换为RGB颜色值。

例如对于绿色,Embed 的期望如下所示:

discord.Embed(title="Hey", description="How are you?", color=0x00ff00)

所以你可以让用户直接传递颜色值:

color = int(message.content, 16)  # content should look like this: "0x00ff00"
discord.Embed(title="Hey", description="How are you?", color=color)

或者将一些颜色名称映射到对应的值:

color_name = message.content  # content should look like this: "green"

colors = {"green": 0x00ff00, "red": 0xff0000, "blue": 0x0000ff}

discord.Embed(title="Hey", description="How are you?", color=colors[color_name])

【讨论】:

  • 啊,我明白了。这行得通,谢谢(:
  • 很高兴能帮上忙。 :)
【解决方案3】:

试试这个:

embed=discord.Embed(title='Hey', description="How are you?", color=hex(value))

【讨论】:

  • 我们感谢您的回答,但请对您的回答提供一点解释——来自审查
【解决方案4】:

我将继续假设您所期望的输入类似于#ffffff,如果我弄错了,请纠正我。为了将其转换为 Discord 可以读取的内容,我们可以使用以下方法。我将假设 message 是您等待他们响应的消息对象。

sixteenIntegerHex = int(message.content.replace("#", ""), 16)
readableHex = int(hex(sixteenIntegerHex), 0)

embed = discord.Embed(
    title = "Hey",
    description = "How are you?",
    color = readableHex
)

您甚至可以将两个整数转换语句合并为一个!

readableHex = int(hex(int(message.content.replace("#", ""), 16)), 0)

【讨论】:

  • 好的,所以,我尝试实现这个,但我收到错误discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: int() can't convert non-string with explicit base
  • 我在代码中犯了一个简单的错误,非常抱歉!现在一切都应该正常工作了。
【解决方案5】:
questions = ["What should be the name of the embed?", 
            "What should be the desc",
            "What is the colour of the embed?"]

answers = []

def check(m):
    return m.author == ctx.author and m.channel == ctx.channel 

for i in questions:
    await ctx.send(i)

    try:
        msg = await client.wait_for('message', timeout=15.0, check=check)
    except asyncio.TimeoutError:
        await ctx.send('You didn\'t answer in time, please be quicker next time!')
        return
    else:
        answers.append(msg.content)


title = answers[1]
desc = answers[2]
colour = answers[3]


embed = discord.Embed(title = f"{title}", description = f"{desc}", color = colour)

embed.set_footer(text = f"My embed")

await channel.send(embed = embed)

【讨论】:

  • 我会试试这个,可能有点难以按照我尝试的方式实现,但是嘿,值得一试!感谢您的贡献(:
猜你喜欢
  • 1970-01-01
  • 2020-06-17
  • 2021-05-06
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 2019-07-25
  • 2021-11-09
  • 1970-01-01
相关资源
最近更新 更多