Search code examples
discordmessagediscord.py

{discord.py} Delete a specific message from a specific user


I want to delete a specific message from a specific user using discord.py The user's id is: 462616472754323457 The message that he can't send: lmao So if he sends lmao, it deletes the message and send "You can't do that <@462616472754323457>


Solution

  • Try discord.TextChannel.purge.

    @client.event
    async def on_message(message):
        if len(await message.channel.purge(limit=200, check=lambda x: ('lmao' in x.content.strip().lower()) and x.author.id == 462616572754323457)) > 0:
            await message.channel.send('You are not allowed to do that, <@462616572754323457>')
    

    Accounting for when the bot is offline, the bot will check 200 messages before the current message and delete any that are from 462616572754323457 and have 'lmao' in the contents. (You can further enhance this by using re.)