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>
0
votes
2 Answers
0
votes
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
.)
0
votes
You can use the on_message
event, which triggers whenever a message is sent.
@bot.event
async def on_message(message):
await bot.process_commands(message) # add this if also using command decorators
if message.author.id == 462616472754323457 and "lmao" in message.content.lower():
await message.delete()
await message.channel.send(f"You can't do that, {message.author.mention}")
References: