Pudge
Pudge

Reputation: 103

How to have the bot show the content of the message it deleted

@client.command()
async def clear(ctx, amount=2):
    await ctx.channel.purge(limit=amount)
    await ctx.send(f'Note:I have cleared the previous two messages\nDeleted messages:{(ctx)}')

I am trying to make the bot show which messages it has deleted, but I can't figure out how to do so.

Upvotes: 1

Views: 112

Answers (1)

Ecks Dee
Ecks Dee

Reputation: 471

For this use, channel.history. This is better because while channel.purge deletes at once, channel.history go to each and every message before deleting which means you can get the content.

@client.command()
async def clear(ctx, amount:int=2):
    messagesSaved = [] # Used to save the messages that are being deleted to be shown later after deleting everything.
    async for msg in ctx.channel.history(limit=amount, before=ctx.message): # Before makes it that it deletes everything before the command therfore not deleting the command itself.
        await msg.delete() # Delets the messages
        messagesSaved.append(msg.content) # Put the message in the list
    await ctx.send(f'Note: I have cleared the previous {amount} messages.\nDeleted messages:\n'+'\n'.join(str(message) for message in messagesSaved))

Saving the message in the list instead of saying it after deleting is good so we can send it all at once instead of deleteing and sending the message right after deleting as this could cause many notifications especially if deleleting a lot of message.

Upvotes: 1

Related Questions