Cooldown mapping | Discord.py(冷却映射 |不和谐.py)
本文介绍了冷却映射 |不和谐.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 on_message
扫描特定关键字的代码,以便机器人可以做出相应的响应,不,我不能使用命令来实现这一点.
I am using on_message
to scan the code for specific keywords so that the bot can respond accordingly, and no, I cannot use commands to achieve this.
我想通过打开冷却时间来防止人们向这些关键字发送垃圾邮件,以便机器人在再次检查之前等待
I want to prevent people from spamming these keywords by turning on a cooldown so the bot will wait before checking again
文档内容:
class SomeCog(commands.Cog):
def __init__(self):
self._cd = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)
async def cog_check(self, ctx):
bucket = self._cd.get_bucket(ctx.message)
retry_after = bucket.update_rate_limit()
if retry_after:
# you're rate limited
# helpful message here
pass
# you're not rate limited
我有什么:
class Listener(commands.Cog):
def __init__(self, bot):
self._cd = commands.CooldownMapping.from_cooldown(1.0, 10.0, commands.BucketType.user)
@commands.Cog.listener()
async def on_message(self, message):
async def cog_check(self, message):
bucket = self._cd.get_bucket(message)
retry_after = bucket.update_rate_limit()
if retry_after:
print('test')
pass
elif (message.guild is None):
return '.'
else:
. . . . . #code which tests for the keywords
推荐答案
class SomeCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
self._cd = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user) # Put your params here
# rate, per, BucketType
def ratelimit_check(self, message):
"""Returns the ratelimit left"""
bucket = self._cd.get_bucket(message)
return bucket.update_rate_limit()
@commands.Cog.listener()
async def on_message(self, message):
if 'check if the message contains certain words here':
# Getting the ratelimit that's left
retry_after = self.ratelimit_check(message)
if retry_after is None:
# You're not ratelimited
else:
# You're ratelimited, you can delete the message here
await message.delete()
await message.channel.send(f"You can't use those words for another {round(retry_after)} seconds.")
此处的代码评估消息是否包含某些单词,如果包含,则检查 ratelimit,如果有 - 删除消息并发送消息.
The code here evaluates if the message contains certain words, if it does, checks for ratelimit, if there is one - deletes the message and sends a message.
这篇关于冷却映射 |不和谐.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:冷却映射 |不和谐.py


基础教程推荐
猜你喜欢
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 筛选NumPy数组 2022-01-01