Skip to content Skip to sidebar Skip to footer

Deleting User Messages In Discord.py

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 a

Solution 1:

Yup, it should be possible.

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

@client.eventasyncdefon_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.

Solution 2:

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.eventasyncdefon_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 listifany(word in msg_content for word in curseWord):
        await message.delete()

if you are using COG:

@commands.Cog.listener()asyncdefon_message(message):
    # rest same as above 

Solution 3:

This is the code you need:

@client.command()asyncdefdelete():
    message = ctx.message.idawait message.delete()

Solution 4:

if you're trying to delete the last sent message, e.g if a user is calling a command and you want to remove their message and then send the command. Use this "await ctx.message.delete()" at the top of your command, it will find the last sent message and delete it.

Solution 5:

I figure out a code to delete discord invitations

@client.eventasyncdefon_message(message):
    msg_cnt = message.content.lower()
    if"discord.gg"in msg_cnt:
        await message.delete()

So this does that every time a user send something that contains "discord.gg" it deletes it

You can change "discord.gg" whatever you want excepts "https", that somehow has given me problems

Post a Comment for "Deleting User Messages In Discord.py"