【问题标题】:python Discord.py delete all messages in a text channelpython Discord.py 删除文本通道中的所有消息
【发布时间】:2021-10-08 21:34:46
【问题描述】:

所以我试图让我的不和谐机器人删除文本频道中的所有消息,因为我喜欢它干净但我不知道该怎么做这是我尝试过的

@CLIENT.command()
async def Clear(message):
    return await CLIENT.delete_message(message)

似乎无法弄清楚谁能帮忙谢谢

【问题讨论】:

    标签: python python-3.x discord


    【解决方案1】:

    如果您想批量删除消息(即一次删除多条消息,请使用await Client.delete_messages(list_of_messages)。这是一个示例

    import asyncio
    import discord
    from discord.ext.commands import Bot
    
    Client = Bot('!')
    
    
    @Client.command(pass_context = True)
    async def clear(ctx, number):
        mgs = [] #Empty list to put all the messages in the log
        number = int(number) #Converting the amount of messages to delete to an integer
        async for x in Client.logs_from(ctx.message.channel, limit = number):
            mgs.append(x)
        await Client.delete_messages(mgs)
    
    Client.run(Token)
    

    注意: 这样做仅适用于 14 天前和 的消息,您不能一次删除超过 100 条消息,这意味着输入此 !clear 120会引发错误。然而,这并非不可能。如果你真的想要,你可以在其中添加一个while 循环,但这可能会产生意想不到的结果。

    现在,如果您收到早于 14 天的消息怎么办?你不能使用Client.delete_messages(list_of_messages)。相反,您可以使用Client.delete_message(Message) 这一次只会删除一条消息。是的,我知道很慢,但现在,这就是我们所拥有的。因此,您可以修改原始代码,使其每次在 logs_from() 中循环时删除。

    类似这样的:

    import asyncio
    import discord
    from discord.ext.commands import Bot
    
    Client = Bot('!')
    
    @Client.command(pass_context = True)
    async def clear(ctx, number):
        number = int(number) #Converting the amount of messages to delete to an integer
        counter = 0
        async for x in Client.logs_from(ctx.message.channel, limit = number):
            if counter < number:
                await Client.delete_message(x)
                counter += 1
                await asyncio.sleep(1.2) #1.2 second timer so the deleting process can be even
    
    Client.run(Token)
    

    【讨论】:

    • 只是为了我的理解......我使用 Python 和 @client.events(使用 async def some_event)开发了一个完整的 Discord 机器人,我完全不知道你在导入 Bot 时在做什么并将! 作为参数传递。 @Client.command 是否与使用 async def on_messagemessage.content.startswith 相同?请赐教,因为似乎只有通过查看您的简短示例,我才能删除 100 行代码并让我的代码在获得确切结果的同时更好地呼吸......
    • @dnLL All @Client.command() is,基本上只是一个命令处理程序,所以它仍然只是一个on_message 事件侦听器,除了它解析消息内容以确保它甚至是一个命令,解析出参数和所有的爵士乐,所以你可以使用@Client.command()。您仍然只能使用on_message,但您可能会使用if 语句拆分每个命令,除非您编写自己的命令处理程序,否则可能会被填充,由您决定。
    • 到目前为止使用 if 语句。两者之间是否有任何性能差异,或者除了使其更易于阅读之外没有其他差异吗?
    • @dnLL 几乎没有任何性能差异。如果有的话,您可能会直接使用on_message 节省一些速度,但非常非常非常少,主要是为了可读性和更容易调试。但我喜欢至少删除默认的帮助命令并自己制作。
    • 为什么我们还要降级到 3.6 版本的 python 才能使用一些类似 logs_from 的函数?他们不能解决这个问题吗?
    【解决方案2】:
    client = commands.Bot(command_prefix='-')
    
    @client.command(name='clear', help='this command will clear msgs')
    async def clear(ctx, amount = 5):
        await ctx.channel.purge(limit=amount)
    

    如果没有提到要删除的消息数量,则默认删除4条消息,即(amount-1)

    使用命令-clear-clear [number] 删除消息。 写完'clear'后不要在上一行使用括号

    【讨论】:

      【解决方案3】:

      您可以使用client.logs_from(someChannel) 获取频道中所有消息的列表。从那里,只需使用client.delete_message(msg)

      使用readme of discord.py's repo 中列出的示例作为基础,这是一个适用于 Python 3.5 的方法。用“!clear”触发它:

      client = discord.Client()
      
      @client.event
      async def on_message(message):
          if message.content.startswith('!clear'):
              tmp = await client.send_message(message.channel, 'Clearing messages...')
              async for msg in client.logs_from(message.channel):
                  await client.delete_message(msg)
      

      【讨论】:

      • 我不确定现在发生了什么,这是第一次使用 discord.py 进行编码,当我运行它时,我告诉“'yield from' inside async function”不确定我应该做什么跨度>
      • 哦,是的,我搞砸了,它应该是 await 而不是 yield from,因为 delete_message() 不是生成器。不知道我从哪里得到的。
      【解决方案4】:

      如果您仍在寻找删除频道中所有消息的方法。您可以使用await ctx.channel.purge() 即时删除所有消息。

      这是一个例子:

      import discord
      from discord.ext import commands
      
      client = commands.Bot(command_prefix = ".")
      
      @client.command()
      async def clear(ctx):
        await ctx.channel.purge()
      

      【讨论】:

        【解决方案5】:

        此代码将删除机器人的命令并发送成功消息..

        import asyncio
        
        #clear
        @bot.command()
        async def clear(ctx, amount=0):
          if amount == 0:
            fail = await ctx.send ("Please enter an amount to delete!")
          await asyncio.sleep (6)
          await fail.delete()
        
          if amount < 3:
            await ctx.channel.purge(limit=amount)
            sucess = await ctx.send (f"{amount} messages has been deleted <a:Verified:878231325469974599>") #sending success msg
            await asyncio.sleep (6) #wait 6 seconds
            await sucess.delete() #deleting the sucess msg
        
          else :
            if amount == 0:
            fail = await ctx.send ("Please enter an amount to delete!")
            await asyncio.sleep (6)
            await fail.delete()
        
        
        

        您可以使用命令-clear 12清除12条消息。将12替换为您的号码

        【讨论】:

          猜你喜欢
          • 2018-06-22
          • 2019-05-07
          • 1970-01-01
          • 2019-10-14
          • 2021-09-26
          • 2021-06-30
          • 2021-06-13
          • 2018-06-29
          • 2021-06-06
          相关资源
          最近更新 更多