10
votes

Is there any way to delete a message sent by anyone other than the bot itself, the documentation seems to indicate that it is possible

Your own messages could be deleted without any proper permissions. However to delete other people’s messages, you need the proper permissions to do so.

But I can't find a way to target the message to do so in an on_message event trigger, am I missing something or is it just not possible?

2

2 Answers

17
votes

Yup, it should be possible.

You need the bot/user account to have the "Manage Messages" permission.

@client.event
async def on_message(message):
    await message.delete(message)

So, the event would occur something like

User sends message
Bot detects that the user has sent a message
Bot deletes the message that the user sent

Hopefully from this you should be able to see how user messages are deleted, just ensure that the bot/user account as the "Manage Messages" permission.

2
votes

you can use message.delete() to delete user message.

it's a code if the user sends some curse word to the server so the bot will clean(delete) the message.

@bot.event
async def on_message(message):
    """ some on_message command """
    if message.author.id == bot.user.id:
        return
    msg_content = message.content.lower()

    curseWord = ['curse1', 'curse2']
    
    # delete curse word if match with the list
    if any(word in msg_content for word in curseWord):
        await message.delete()

if you are using COG:

@commands.Cog.listener()
async def on_message(message):
    # rest same as above