Izzy Mailmare
Izzy Mailmare

Reputation: 25

Updating image in Embed via discord.File in discord.py

Today I was trying to code a memory game via discord Embeds, but discord.File class does not work as I thought it would. My goal is to update the embed every time a user gives a correct answer with a different image (from the PIL module). After testing, while the image itself in "temp" variable is correct and different every time, the image that displays in the embed via discord.File is not, as it always displays the first image ever generated (3-digit number).

I have a theory that you need to delete the image from the embed first and then try to replace it with a new one, but I'm not sure how to verify that. After displaying the desired number, it should delete itself by a new embed that gets edited over the old one, but the image "jumps" out of the embed and stays above it forever. The embed under it does not display any images from that point onward.

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

Upvotes: 1

Views: 880

Answers (1)

Eric Jin
Eric Jin

Reputation: 3934

You need to remove the file attachment itself from the message (docs here). Previously, the attachment did not show because you put it into the embed, but after editing, you have to do something to the attachment.

        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

There's also no need to overwrite the image to None in the edited embed - it's like that by default, and you also cause an error.

Upvotes: 1

Related Questions