【问题标题】:in discord.py how to to delete others all message在 discord.py 中如何删除其他所有消息
【发布时间】:2021-06-13 08:20:21
【问题描述】:

我想制作机器人来删除特定服务器或频道的机器人的所有消息。

但我只能找到删除事件消息或删除频道的消息。

import asyncio
@client.event
async def on_message(message):
    content = message.content
    guild = message.guild
    author = message.author
    channel = message.channel
    if content == "!delete":
        await #delete all bot's message

【问题讨论】:

    标签: python-3.x discord discord.py


    【解决方案1】:

    如果要删除特定频道中的所有消息:

    
    guilds_list = [CHANNEL_ID, CHANNEL2_ID, CHANNEL3_ID]
    @client.event
    async def on_message(message):
        if message.channel.id in guilds_list:
            await message.delete()
    

    如果您只想删除机器人的消息,现在机器人将删除特定频道中的所有消息:

    @client.event
    async def on_message(message):
        if message.channel.id in guilds_list and message.author is client.user:
            await message.delete()
    

    注意:这个过程是自动的。所以机器人会删除它发送到特定频道的每条消息。

    【讨论】:

    • 这不行,你必须检查message.channel.id in guilds_list,即使那样,它只会删除发送的新消息,我认为OP想删除机器人以前的消息。
    【解决方案2】:

    可以使用channel.purge删除特定频道中的所有机器人消息

    async def on_message(message):
       channel = message.channel
       if message.content == '!delete':
          if len(message.mentions) == 0:
             await channel.purge(limit=100, check= lambda x: x.author.id == client.user.id)
          else:
             mentioned = message.mentions[0]
             await channel.purge(limit=100, check= lambda x: x.author.id == mentioned.id)
    
    

    如果要删除所有频道中的消息,只需循环使用guild.channels

    参考资料:

    注意: 检查函数应该返回一个布尔值,告诉用户我们是否应该删除该消息,并将该消息作为参数。

    【讨论】:

    • 谢谢。所以我从“message.mentions [0]”获取user.id,我如何编辑“await channel.purge(limit = 100,check = lambda x:x.author.id == client.user.id)”这个线?对不起。这对我来说太难了
    • 我编辑了我的答案,我建议你使用commands,这会让你的工作更轻松
    猜你喜欢
    • 2020-07-21
    • 2019-05-07
    • 2018-06-29
    • 2021-01-21
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 2017-06-30
    相关资源
    最近更新 更多