How to retrieve previous messages with discord.py 1

How to retrieve previous messages with discord.py

@client.event
async def on_message(message):
    c_channel = discord.utils.get(message.guild.text_channels, name='counting channel')
    messages = await c_channel.history(limit=2).flatten()
    if message.channel == c_channel and int(messages[1].content) + 1 != int(message.content):
        await message.delete()

Here is what the above code is Doing:
1. We’re getting the counting channel by using the get() method on the guild’s text channels.
2. We’re getting the last two messages in the counting channel by using the history() method on the channel.
3. We’re checking if the message is in the counting channel and if the last message’s content + 1 is not equal to the current message’s content.
4. If the above is true, we’re deleting the message.

Similar Posts