【问题标题】:cooldown for on_message in discord.pydiscord.py 中 on_message 的冷却时间
【发布时间】:2021-05-02 13:28:05
【问题描述】:

我已经制作了一个练级系统,但我无法在on_message 中制作一个冷却 我想添加一个BucketType.member 冷却时间,因为我使用 MondoDB 作为数据库,所以我无法存储他们最后一次发送消息的时间,而是我正在寻找on_message 的冷却时间,它的工作原理类似于命令冷却时间,所以它可以自动采取任何行动
这是目前的代码

@commands.Cog.listener()
    async def on_message(self , message):
        if message.channel.id in talk_channels:
            stats = leveling.find_one({"id":message.author.id})
            if not message.author.bot:
                if stats is None:
                    new_user = {"id" : message.author.id, "xp" : 0}
                    leveling.insert_one(new_user)
                else:
                    
                    xp = stats["xp"] + 5
                    leveling.update_one({"id" : message.author.id}, {"$set" : {"xp" : xp}})
                    lvl = 0
                    while True:
                        if xp < ((50*(lvl**2))+(50*lvl)):
                            break
                        lvl += 1
                    xp -= ((50*((lvl-1)**2))+(50*(lvl-1)))
                    if xp == 0:
                        await message.channel.send(f"Congo you leveled up {message.author.mention} to **level: {lvl}**")
                        for i in range(len(level_role)):
                            if lvl == levelnum[i]:
                                await message.author.add_roles(discord.utils.get(message.author.guild.roles, name=level_role[i]))
                                embed = discord.Embed(title="LEVEL UP", description=f"You have reached a mile stone of {lvl} and has got role **{level_role[i]}**", color=0x00ffff)
                                embed.set_thumbnail(url=message.author.avatar_url)
                                await message.channel.send(embed=embed)

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    您应该使用CooldownMapping.from_cooldownon_message 事件添加冷却时间,例如:

    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
    

    【讨论】:

    • 非常感谢兄弟,我只需要同样的东西
    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2021-03-25
    • 2018-10-21
    • 2021-05-15
    • 2020-10-14
    • 1970-01-01
    • 2021-03-11
    相关资源
    最近更新 更多