{discord.py} 删除来自特定用户的特定消息

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

我想使用 discord.py 删除来自特定用户的特定消息 用户的id是:462616472754323457 他发不出去的信息:lmao 因此,如果他发送 lmao,它会删除消息并发送“你不能那样做 <@462616472754323457>

尝试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>')

考虑到机器人离线时,机器人将检查当前消息之前的 200 条消息,并删除来自 462616572754323457 且内容中包含 'lmao' 的任何消息。 (您可以使用 re 进一步增强这一点。)

您可以使用 on_message 事件,该事件在发送消息时触发。

@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}")

参考文献: