Python 如何在此事件中添加冷却时间或速率限制?不和谐 @commands.Cog.listener() _消息上的异步定义(self,message): user_in=message.content.lower() 如果.split(“”)中的“gn”或“good night”中的“good night”: if message.author.bot:返回 如果不是message.guild:返回 等待消息。频道。发送(f“晚安”)

Python 如何在此事件中添加冷却时间或速率限制?不和谐 @commands.Cog.listener() _消息上的异步定义(self,message): user_in=message.content.lower() 如果.split(“”)中的“gn”或“good night”中的“good night”: if message.author.bot:返回 如果不是message.guild:返回 等待消息。频道。发送(f“晚安”),python,discord,discord.py,Python,Discord,Discord.py,我需要知道如何为这个活动添加冷却时间,这样人们就不会发垃圾邮件或晚安了 我建议你看看这篇文章。async def create\u embed(self,ctx,err): embed=discord.embed(title=f“错误捕获!”,color=discord.color.red()) add_字段(name=f“错误信息”,value=f“{err}”) embed.set_缩略图(url=self.bot.user.avatar_url_为(static_format=“png”)

我需要知道如何为这个活动添加冷却时间,这样人们就不会发垃圾邮件或晚安了

我建议你看看这篇文章。

async def create\u embed(self,ctx,err):
embed=discord.embed(title=f“错误捕获!”,color=discord.color.red())
add_字段(name=f“错误信息”,value=f“{err}”)
embed.set_缩略图(url=self.bot.user.avatar_url_为(static_format=“png”))
message_cooldown=commands.CooldownMapping.from_cooldown(1.0,60.0,commands.BucketType.user)
bucket=消息\u冷却。获取\u bucket(ctx.message)
重试\u after=bucket.update\u rate\u limit()
如果在以下时间后重试\u:
返回
其他:
等待ctx.send(嵌入=嵌入)
这是一个很好的例子,它创建了一个嵌入函数,以使您的代码更干净,便于捕获错误,并确保这不能通过冷却映射进行垃圾邮件处理

@commands.Cog.listener()
    async def on_message(self, message):
        user_in  = message.content.lower()
        if "gn" in  user_in.split(" ") or "good night" in user_in :
            if message.author.bot: return
            if not message.guild: return
            await message.channel.send(f"Good Night, <@{message.author.id}>")