猿问

如何为此事件添加冷却时间或速率限制?不和谐.py

@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}>")

我需要知道如何为此事件添加冷却时间,以便人们dont发送垃圾邮件gn或晚安


海绵宝宝撒
浏览 108回答 1
1回答

慕村225694

Discord.py(重写)如何使用 on_message 事件获得冷却时间?我建议您查看这篇文章。

千万里不及你

async def create_embed(self, ctx, err):&nbsp; &nbsp; &nbsp; &nbsp; embed = discord.Embed(title=f"Error Caught!",color=discord.Colour.red())&nbsp; &nbsp; &nbsp; &nbsp; embed.add_field(name=f"Info on the error.", value=f"{err}")&nbsp; &nbsp; &nbsp; &nbsp; embed.set_thumbnail(url=self.bot.user.avatar_url_as(static_format="png"))&nbsp; &nbsp; &nbsp; &nbsp; message_cooldown = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)&nbsp; &nbsp; &nbsp; &nbsp; bucket = message_cooldown.get_bucket(ctx.message)&nbsp; &nbsp; &nbsp; &nbsp; retry_after = bucket.update_rate_limit()&nbsp; &nbsp; &nbsp; &nbsp; if retry_after:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; await ctx.send(embed=embed)这是一个很好的例子,它在函数中创建嵌入,使代码更干净,以便捕获错误,并确保它不会被冷却映射垃圾邮件。
随时随地看视频慕课网APP

相关分类

Python
我要回答