How to get the second last message from a channel discord py

Question:

i’m trying to make a command that edit the second last message (after the !command message) embed in a channel, but i cant find a way to get the second last message; i get this error

discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'async_generator' object has no attribute 'flatten'

I tried this block of code :

@bot.command()
async def edit(ctx,gif):

  channel=ctx.channel
  messages = await channel.history(limit=2).flatten()
  second_to_last_message_id = messages[1].id

  await ctx.message.delete()

  msg = await channel.fetch_message(second_to_last_message_id)
  
  print(msg)

  a=msg.embeds[0]
  
  a.set_image(url=gif)
  
  await msg.edit(embed=msg.embeds[0])

If anyone could help me, thank you!

Asked By: IhaveAquestionnn

||

Answers:

You should use list comprehension to get a list of messages from the async generator since the flatten() function has been removed:

messages = [message async for message in channel.history(limit=2)]
Answered By: kaiffeetasse
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.