所以我得到了应该发送带有图像的嵌入的代码,但由于某种原因图像不在其中
@bot.command(name='abrir')
async def abrir_case(ctx, case_name: str):
user_id = str(ctx.author.id)
current_balance = get_balance(user_id)
if current_balance < 10:
embed = discord.Embed(title="Erro ao abrir o caso", description=":x: Você não possui saldo suficiente para abrir o caso.", color=0xff0000)
await ctx.reply(embed=embed)
return
update_balance(user_id, current_balance - 10)
case_folder_path = f"C:\\Users\\Admin\\Desktop\\bot\\Itens\\{case_name}"
if not os.path.exists(case_folder_path):
embed = discord.Embed(title="Erro ao abrir o caso", description=":x: Este caso não existe.", color=0xff0000)
await ctx.reply(embed=embed)
return
total_weight = sum(RARITY_CHANCES.values())
chosen_rarity = random.choices(list(RARITY_CHANCES.keys()), weights=[RARITY_CHANCES[rarity] / total_weight for rarity in RARITY_CHANCES])[0]
rarity_folder_path = os.path.join(case_folder_path, chosen_rarity)
items = os.listdir(rarity_folder_path)
chosen_item = random.choice(items)
desguaste = random.uniform(0, 1)
embed = discord.Embed(title="Caso Aberto", description=f"Você abriu o caso {case_name} e obteve:", color=0x00ff00)
embed.add_field(name="Item", value=chosen_item[:-4], inline=False)
embed.add_field(name="Raridade", value=chosen_rarity, inline=False)
embed.add_field(name="DESGUASTE", value=desguaste, inline=False)
item_image_path = os.path.join(rarity_folder_path, chosen_item)
try:
if os.path.exists(item_image_path) and os.path.isfile(item_image_path):
file = discord.File(item_image_path, filename=chosen_item)
embed.set_image(url=f"attachment://{chosen_item}")
await ctx.reply(file=file, embed=embed)
else:
await ctx.reply(embed=embed)
except discord.HTTPException as e:
print(f"Error sending image: {e}")
await ctx.reply(embed=embed)
我以为图像会出现在嵌入消息中,我尝试使用 chatgpt 但他没有修复它
这是因为您使用的是本地图像附件。 文档说:
不和谐上传图像附件并在嵌入中使用它的特殊情况,这样它就不会单独显示,而是在嵌入的缩略图、图像、页脚或作者图标中显示。
为此,请使用 abc.Messageable.send() 正常上传图像,然后 将嵌入的图像 URL 设置为 Attachment://image.png,其中 image.png 是您要发送的图像的文件名。
请参阅此处。解决方法是托管一个 Flask 服务器或托管图像的东西,您可以将其发送到不和谐。