【问题标题】:{discord.py} Delete a specific message from a specific user{discord.py} 删除特定用户的特定消息
【发布时间】:2020-09-13 18:56:15
【问题描述】:

我想使用 discord.py 删除来自特定用户的特定消息 用户id为:462616472754323457 他无法发送的消息:lmao 因此,如果他发送 lmao,它会删除消息并发送“你不能这样做

【问题讨论】:

    标签: discord message discord.py


    【解决方案1】:

    试试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 进一步增强此功能。)

    【讨论】:

    • 发生了什么?有什么例外吗?请记住,这仅在发件人的 ID 为 462616572754323457 时才有效。如果您希望所有人都能使用,请尝试将代码中的 x.author.id == 462616572754323457 更改为 True
    【解决方案2】:

    您可以使用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}")
    

    参考资料:

    【讨论】:

    • @SP73 是否抛出错误?你能用你试过的代码编辑你的问题吗?
    • @SP73 请您将代码编辑到您当前程序中的问题中。这可能是一个简单的错字或您遗漏的内容。
    猜你喜欢
    • 2018-01-17
    • 1970-01-01
    • 2021-01-27
    • 2019-06-02
    • 2021-05-18
    • 1970-01-01
    • 2012-07-09
    • 2022-01-20
    • 2021-08-17
    相关资源
    最近更新 更多