且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

discord.py中on_message的冷却时间

更新时间:2023-11-30 11:03:04

您应该使用 CooldownMapping.from_cooldown 将冷却时间添加到 on_message 事件中,例如:

You should use CooldownMapping.from_cooldown to add cooldowns to the on_message event, example:

import typing
import discord
from discord.ext import commands

class SomeCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._cd = commands.CooldownMapping.from_cooldown(1, 6.0, commands.BucketType.member) # Change accordingly
                                                        # rate, per, BucketType

    def get_ratelimit(self, message: discord.Message) -> typing.Optional[int]:
        """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 something":
            # Getting the ratelimit left
            ratelimit = self.get_ratelimit(message)
            if ratelimit is None:
                # The user is not ratelimited, you can add the XP or level up the user here

            else:
                # The user is ratelimited