通过 discord.File 在 discord.py 中更新嵌入图像
Updating image in Embed via discord.File in discord.py
今天我试图通过 discord Embeds 编写一个记忆游戏,但是 discord.File class 并没有像我想象的那样工作。
我的目标是每次用户使用不同的图像(来自 PIL 模块)给出正确答案时更新嵌入。经过测试,虽然“temp”变量中的图像本身是正确的并且每次都不同,但通过 discord.File 在嵌入中显示的图像却不是,因为它总是显示有史以来生成的第一张图像(3 位数字) .
我认为您需要先从嵌入中删除图像,然后尝试用新图像替换它,但我不确定如何验证这一点。在显示所需的数字后,它应该通过在旧嵌入上编辑的新嵌入删除自己,但图像“跳”出嵌入并永远停留在它上面。它下面的嵌入不再显示任何图像。
end = False
length = 2
def draw(num):
img = Image.new("RGB", (250, 50), color = (44, 47, 51))
d = ImageDraw.Draw(img)
font = ImageFont.truetype("Fonts/Kollektif.ttf", 25)
d.text((125, 25), num, font = font, fill = (255, 255, 255), anchor = "mm")
return img
while not end:
length += 1
number = ""
for i in range(length):
number += str(randint(1,9))
with BytesIO() as image_binary:
temp = draw(number)
temp.save(image_binary, "PNG")
image_binary.seek(0)
file = discord.File(fp = image_binary, filename = "image" + number + ".png")
embed = discord.Embed(title = "Memory", description = "Memorize this number and write it afterwards!", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
embed.set_image(url = "attachment://image" + number + ".png")
embed.set_footer(text = ctx.author, icon_url = ctx.author.display_avatar.url)
if length == 3:
emb = await ctx.send(embed = embed, file = file)
else:
await emb.edit(embed = embed)
await asyncio.sleep(5)
embed = discord.Embed(title = "Memory", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
embed.add_field(name = "Write the number!", value = "Length: " + str(length) + " digits", inline = True)
embed.set_image(url = None)
embed.set_footer(text = ctx.author, icon_url = ctx.author.display_avatar.url)
await emb.edit(embed = embed)
def check(message):
return message.author == ctx.author
msg = await self.bot.wait_for("message", check=check)
# other stuff
您需要从邮件中删除文件附件本身 (docs here)。以前,附件没有显示,因为你把它放到嵌入中,但编辑后,你必须对附件进行一些处理。
embed = discord.Embed(title = "Memory", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
embed.add_field(name = "Write the number!", value = "Length: " + str(length) + " digits", inline = True)
await emb.edit(embed = embed, attachments=[]) # remove the image attachment
也不需要在编辑后的嵌入中将图像覆盖到 None
- 默认情况下就是这样,而且您也会导致错误。
今天我试图通过 discord Embeds 编写一个记忆游戏,但是 discord.File class 并没有像我想象的那样工作。 我的目标是每次用户使用不同的图像(来自 PIL 模块)给出正确答案时更新嵌入。经过测试,虽然“temp”变量中的图像本身是正确的并且每次都不同,但通过 discord.File 在嵌入中显示的图像却不是,因为它总是显示有史以来生成的第一张图像(3 位数字) .
我认为您需要先从嵌入中删除图像,然后尝试用新图像替换它,但我不确定如何验证这一点。在显示所需的数字后,它应该通过在旧嵌入上编辑的新嵌入删除自己,但图像“跳”出嵌入并永远停留在它上面。它下面的嵌入不再显示任何图像。
end = False
length = 2
def draw(num):
img = Image.new("RGB", (250, 50), color = (44, 47, 51))
d = ImageDraw.Draw(img)
font = ImageFont.truetype("Fonts/Kollektif.ttf", 25)
d.text((125, 25), num, font = font, fill = (255, 255, 255), anchor = "mm")
return img
while not end:
length += 1
number = ""
for i in range(length):
number += str(randint(1,9))
with BytesIO() as image_binary:
temp = draw(number)
temp.save(image_binary, "PNG")
image_binary.seek(0)
file = discord.File(fp = image_binary, filename = "image" + number + ".png")
embed = discord.Embed(title = "Memory", description = "Memorize this number and write it afterwards!", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
embed.set_image(url = "attachment://image" + number + ".png")
embed.set_footer(text = ctx.author, icon_url = ctx.author.display_avatar.url)
if length == 3:
emb = await ctx.send(embed = embed, file = file)
else:
await emb.edit(embed = embed)
await asyncio.sleep(5)
embed = discord.Embed(title = "Memory", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
embed.add_field(name = "Write the number!", value = "Length: " + str(length) + " digits", inline = True)
embed.set_image(url = None)
embed.set_footer(text = ctx.author, icon_url = ctx.author.display_avatar.url)
await emb.edit(embed = embed)
def check(message):
return message.author == ctx.author
msg = await self.bot.wait_for("message", check=check)
# other stuff
您需要从邮件中删除文件附件本身 (docs here)。以前,附件没有显示,因为你把它放到嵌入中,但编辑后,你必须对附件进行一些处理。
embed = discord.Embed(title = "Memory", timestamp = datetime.datetime.utcnow(), color = discord.Color.green())
embed.add_field(name = "Write the number!", value = "Length: " + str(length) + " digits", inline = True)
await emb.edit(embed = embed, attachments=[]) # remove the image attachment
也不需要在编辑后的嵌入中将图像覆盖到 None
- 默认情况下就是这样,而且您也会导致错误。