簡體   English   中英

如何將存儲為字符串的十六進制值轉換為 python 中的十六進制值?

[英]How do I convert a hex value that is stored as a string into a hex value in python?

我有一個 discord 機器人,我正在處理的命令將使用用戶提供的 arguments 由用戶自行決定發送嵌入。 這意味着嵌入顏色的十六進制代碼存儲為字符串,而不是十六進制。 我嘗試了多種方法,包括使用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 fa!embed Hi | #ffffff | Hi | Made by me fa!embed Hi | #ffffff | Hi | Made by me 謝謝!

@Tim Roberts 建議使用這種方法:

刪除我的轉換並調用int(embedConfig[1][1:],16)作為顏色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM