【问题标题】:issues converting a string to a discord.Color() object, and then back again将字符串转换为 discord.Color() 对象时出现问题,然后再返回
【发布时间】:2018-12-26 11:08:16
【问题描述】:

我正在为 Discord 开发的机器人会编译有关一个人的角色的信息,然后以一些嵌入的形式发回一个角色表。

我的很多功能都被关闭了,所以现在我正在开发天赋。我想做的一件事是让人们改变嵌入侧面小条纹的颜色,结果比我想象的要困难得多。

老实说,我已经忘记了所有我尝试过的事情,到目前为止,这已经是一个 5 小时的问题,我仍在到处尝试随机调整。

    ###allows the user to set the stripe color of their character sheet embed.
@character.command(name="setcol")
async def color_set(self, ctx, *, color:str):
    if "#" in color:
        color = color.replace("#", "")
    member = ctx.message.author
    col = discord.Color(value=int(color, 16))
    await self.config.member(member).color.set(col.to_rgb())
    await asyncio.sleep(1)
    col = await self.config.member(member).color()
    pvw = discord.Embed(name="Preview", description="Preview", color=discord.Color.from_rgb(*col))
    await ctx.send(embed=pvw)
    await ctx.send("{}".format(col))

预期: ##character setcol 00fe00: 将 00fe00 转换为 discord.color() 对象, 将对象转换为字符串, 将字符串保存在颜色配置变量中, 将字符串转回 discord.Color() 对象, 返回具有正确颜色的预览嵌入, 打印十六进制代码

实际: ##character setcol 00fe00: 将 00fe00 转换为 discord.color() 对象, 将对象转换为字符串, 将字符串保存在颜色配置变量中, 返回错误TypeError: Expected int parameter, received str instead, 函数终止

【问题讨论】:

  • 完整的错误信息是什么?是您的 self.config.member 呼叫失败还是嵌入?将颜色直接从转换器传递到嵌入应该可以工作,我不确定你为什么要将它更改为/从字符串
  • 当我将原始用户输入放入 discord.Color() 时,它会抛出一个错误,指出它需要一个 int 或 discord.Color 对象,但收到的是一个字符串,这就是为什么我有所有的皈依者
  • 在 red discord bot 社区服务器的帮助下,我得以解决。必须做一些魔法才能让所有变量都发挥得很好,发布更新的代码

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


【解决方案1】:
###allows the user to set the stripe color of their character sheet embed.
@character.command(name="setcol")
async def color_set(self, ctx, *, color:str):
    if "#" in color:
        color = color.replace("#", "")
    member = ctx.message.author
    col = discord.Color(value=int(color, 16))
    await self.config.member(member).color.set(col.to_rgb())
    await asyncio.sleep(1)
    col = await self.config.member(member).color()
    pvw = discord.Embed(name="Preview", description="Preview", color=discord.Color.from_rgb(*col))
    await ctx.send(embed=pvw)
    await ctx.send("{}".format(col))

不知道为什么我一开始不这样做,而是更新原始版本,但是,嘿,无论如何。是的。那是固定代码。基本上必须采用十六进制,从中删除#,将其转换为RGB,将RGB保存为元组,在调用元组时将元组转换回RGB。这很痛苦,但是,嘿,它可以做我想做的事!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    • 2019-04-12
    • 2019-12-30
    • 1970-01-01
    相关资源
    最近更新 更多