如何将存储为字符串的十六进制值转换为python中的十六进制值?

2024-10-01 17:41:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个discord bot,我正在处理的命令将使用用户提供的参数发送一个嵌入,由用户自行决定。这意味着嵌入颜色的十六进制代码存储为字符串,而不是十六进制。我已经尝试了很多方法,包括使用int(variable, 16)转换为base16 int,然后使用hex(variable),使用utf-8编码在两者之间转换,但到目前为止还没有任何效果。我目前的代码是:

@commands.command(description='Send an embed message with Title, Colour, Footer and Field customization.')
  async def embed(self, ctx, *, args=None):
    if args == None:
      #code that explains how to use the command
    else:
      embedConfig=args.split(" | ")
      if (len(embedConfig)-1) > 3:
        await ctx.send("Too many arguments!")
      else:
        embedConfig[1] = embedConfig[1].lstrip("#")
        embedConfig[1] = ("0x" + embedConfig[1])
        embedConfig[1] = int(embedConfig[1], 16)
        embedConfig[1] = hex(embedConfig[1])
        embed=discord.Embed(title=embedConfig[0], description=embedConfig[2], color=embedConfig[1])
        embed.set_footer(text=embedConfig[3])
        embed.timestamp = datetime.now()
        await ctx.send(embed=embed)

我正在使用discord.py重写和命令扩展。上面的命令位于cog中,有效语法如下:fa!embed Hi | #ffffff | Hi | Made by me。谢谢


Tags: 代码用户命令noneifargsembeddescription

热门问题