【问题标题】:How do i add a cooldown or a ratelimit to this event? discord.py如何为此事件添加冷却时间或速率限制?不和谐.py
【发布时间】:2021-03-08 10:19:24
【问题描述】:
@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 或晚安

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    【讨论】:

      【解决方案2】:
          async def create_embed(self, ctx, err):
              embed = discord.Embed(title=f"Error Caught!",color=discord.Colour.red())
              embed.add_field(name=f"Info on the error.", value=f"{err}")
              embed.set_thumbnail(url=self.bot.user.avatar_url_as(static_format="png"))
              message_cooldown = commands.CooldownMapping.from_cooldown(1.0, 60.0, commands.BucketType.user)
              bucket = message_cooldown.get_bucket(ctx.message)
              retry_after = bucket.update_rate_limit()
              if retry_after:
                  return
              else:
                  await ctx.send(embed=embed)
      

      这是一个很好的例子,它可以在函数中创建嵌入以使代码更清晰以捕获错误,并确保它不会被冷却映射发送垃圾邮件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-18
        • 2022-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多