简体   繁体   English

如何清除 Discord.py 中特定用户的消息

[英]How to purge messages for specific users in Discord.py

Hey so I was trying to make a command in discord.py rewrite version was doing something like >purgemessage 5 would purge 5 messages of the user who sent that message but for some reason, it won't work.嘿,所以我试图在 discord.py 重写版本中创建一个命令正在执行类似>purgemessage 5它将清除发送该消息的用户的 5 条消息,但由于某种原因,它不起作用。 I am using this in a reply so it won't show the error;-;我在回复中使用它,所以它不会显示错误;-; but here is what I have done so far to try to make it work!但这是我到目前为止所做的尝试使其工作!

# purges author's messages
@commands.command(name='purgemessage', aliases=['pm'])
@commands.cooldown(1, 300)
@commands.has_any_role(OwnerRole, GuildMasterRole, AdminRole, ExclusiveMemberRole)
async def purgemessage(self, ctx, amount:int, *, arg:str=None):
    msg = []
    channel = ctx.message.channel.history(limit=None)
    async for message in channel:
        if message.author == ctx.message.author:
            msg.append(message)
    await ctx.message.channel.delete_messages(msg)
    await asyncio.sleep(.5)
    botMessage = await ctx.send(f'**{amount}** messages were successfully deleted!')
    await asyncio.sleep(.5)
    await botMessage.delete()

I haven't done thorough tests, but the first error I am getting (after rewriting to work outside a cog so I could quickly test), is我还没有做过彻底的测试,但我得到的第一个错误是(重写后在 cog 外工作以便我可以快速测试)是

discord.errors.HTTPException: 400 Bad Request (error code: 50034): You can only bulk delete messages that are under 14 days old. discord.errors.HTTPException:400 错误请求(错误代码:50034):您只能批量删除 14 天以下的消息。

This error might be caused by your code not containing anything to stop adding messages after reaching amount , and continuing backward for a user's entire history in a channel.此错误可能是由于您的代码不包含任何内容以在达到amount后停止添加消息,并继续向后查看用户在频道中的整个历史记录。
This could very well not be the error you're getting, depending on the channel you're testing in. I would highly recommend you start working on an environment that will show you errors so we can help you better.这很可能不是您遇到的错误,具体取决于您正在测试的频道。我强烈建议您开始在一个会显示错误的环境中工作,以便我们可以更好地为您提供帮助。
I don't know enough to debug the code you've given, but here's something that does what you want in less code and I know works:我的知识不足以调试您提供的代码,但这里有一些可以用更少的代码完成您想要的操作,而且我知道它有效:

channel = ctx.channel
await channel.purge(limit=amount, check=lambda message: message.author == ctx.author)

ctx.channel is the channel the command was sent in, ctx.author is who sent it. ctx.channel是发送命令的频道, ctx.author是发送它的人。
channel.purge will purge limit amount of messages that meet the check function, which in this case is a lambda (a function defined in one line, looks like this: lambda argument: expression . The lambda will implictly return the expression). channel.purge清除满足check函数的limit数量的消息,在这种情况下是一个 lambda(在一行中定义的函数,如下所示: lambda argument: expression 。lambda 将隐式返回表达式)。

The check keyword will pass message as an argument, so I've named by lambda argument appropriately. check 关键字会将message作为参数传递,因此我已适当地通过 lambda 参数命名。 The expression checks that the message we're checking ( message.author ) was written by the invoker of the command ( ctx.author ).该表达式检查我们正在检查的消息 ( message.author ) 是否由命令的调用者 ( ctx.author ) ctx.author

This code works, even for messages over 14 days old, but it does take some time for larger amounts of messages.此代码即使对于超过 14 天的消息也有效,但对于大量消息确实需要一些时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM