python - 如何在 discord.py 的“commands.Bucket”中获取限速人员
问题描述
所以我有一个反垃圾邮件系统,它是在on_message
discord.py 事件中制作的。相同的代码如下-->
@bot.event
async def on_message(message):
await bot.wait_until_ready()
if message.author == bot.user:
return
bucket = bot.AntiSpam.get_bucket(message)
retry_after = bucket.update_rate_limit()
if retry_after:
print('Someone was being naughty')
await message.channel.purge(before=datetime.utcnow(), after=datetime.utcnow()-timedelta(seconds=5))
await message.channel.send(f'Please don\'t spam, {message.author.mention}', delete_after=5.0)
这将删除自最后 5 秒以来发送的每条消息。如何使其仅删除该垃圾邮件发送者发送的消息?我知道我必须使用check
kwarg of TextChannel.purge()
,但我需要一种方法来进行检查。我能得到任何帮助吗?
解决方案
检查将非常简单:
def check(m):
return m.author == message.author
await message.channel.purge(before=datetime.utcnow(), after=datetime.utcnow()-timedelta(seconds=5), check=check)
我们正在检查传递给检查的消息的作者是否与上一条消息的作者相同。
推荐阅读
- node.js - 如何使用环回包含关系过滤数据?
- python - 使用 Python 从互联网上获取与给定关键字相关的所有 url
- unity3d - ElectronJS 中的 Unity 游戏?
- ios - App Store 上仅更新 iPad 应用程序
- java - 如何在 nanohttpd 中发送 JSON 数据作为响应
- android - 带有 SharedElementTransition 的 AsyncLayoutInflater
- ios - 当我保存核心数据时,Int64 不显示 0
- javascript - 如何将使用 javascript 和 ASP.NET 生成的字段中的数据发送到 Microsoft SQL?
- python - 数据框:在一天中的一周内累积日期
- java-8 - 通过 Java 8 Streams API 操作 StringBuilder