删除多条消息

Delete multiple messages

好吧,我现在正在创建一个不和谐的机器人,我想添加一个删除消息的功能。 基本上,机器人会等待一条消息弹出,如果这条消息包含给定的命令, 在这种情况下,“sudo clean (number)”,它会删除 (number) 条消息(所以如果数字是 5,它会删除 5 条消息)。好吧,我的意思是它应该这样做,但是是的,它没有做它应该做的事情。

我得到discord.errors.NotFound: 404 Not Found (error code: 10008): Unknown Message

那是我的代码:

elif message.content.lower().startswith("sudo clean"):
            userMessage = message.content
            message.delete()    

            for word in userMessage.split():    
                if word.isdigit():
                    textToClean = int(word)
                    for i in range(0, textToClean):
                        await message.delete()

有什么问题?

这是我使用的,如果你不想要任何花哨的东西,它会容易得多

 @client.command(name="clear",
                  brief="Deletes a certain amount of messages ex. .clear 10")
async def clear(ctx, amount=5):
    amount += 1

  await ctx.channel.purge(limit=amount)

这是一个使用斜杠命令的非常简单的实现,我目前正在使用它。

 @commands.slash_command(description='Delete messages from a channel.')
 @commands.has_permissions(manage_messages=True)
async def purge(
        self,
        inter,
        amount: int
):
    """Delete a certain amount of messages in the chat

        Parameters
        ----------
        amount: The amount of messages you want to delete, can't be greater than 100
        """

    if amount > 100:
        await inter.send("Wow, take it easy, no more than 100 messages at a time", ephemeral=True)
        return

    await inter.channel.purge(limit=amount, check=lambda msg: not msg.pinned)
    await inter.send('Messages deleted.', ephemeral=True)

当然,如果你不使用斜线命令也可以很容易的变成一个普通的前缀命令

 @commands.command()
 @commands.has_permissions(manage_messages=True)
async def purge(
        self,
        ctx,
        amount: int
):
    """Delete a certain amount of messages in the chat

        Parameters
        ----------
        amount: The amount of messages you want to delete, can't be greater than 100
        """

    if amount > 100:
        await ctx.send("Wow, take it easy, no more than 100 messages at a time")
        return

    await ctx.channel.purge(limit=amount, check=lambda msg: not msg.pinned)
    await ctx.send('Messages deleted.')

检查是为了避免固定消息