Home > Software design >  How to get the second last message from a channel discord py
How to get the second last message from a channel discord py

Time:01-10

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!

CodePudding user response:

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)]
  • Related