如何为我的discord.py bot添加冷却时间?



我需要为/chat命令添加冷却时间。我该怎么做呢?由于我从ChatGPT中提取数据,因此我可以对垃圾邮件的使用进行速率限制。为了解决这个问题,我需要添加每个用户的时间。

@client.tree.command(name="chat", description="Have a chat with ChatGPT")
async def chat(interaction: discord.Interaction, *, message: str):
if interaction.user == client.user:
return
username = str(interaction.user)
user_message = message
channel = str(interaction.channel)
logger.info(
f"x1b[31m{username}x1b[0m : '{user_message}' ({channel})")
await send_message(interaction, user_message)

我不知道该怎么办。

您可以使用commands.cooldown装饰器来做到这一点:

from discord.ext import commands
@commands.cooldown(1, 30, commands.BucketType.user)
async def chat(...):
...

这将允许每个用户每30秒执行一个chat命令。您也可以在服务器级别使用BucketType.server来代替。

最新更新