Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

python - cooldown for on_message in discord.py

I have made a Leveling system but i can't figure out to make a cooldown in on_message i want to add a BucketType.member cooldown and as im using MondoDB for database so i can't afford to store the last time they send message instead im looking for a cooldown for on_message which works similar to commands cooldown so it can automatically take any action
This is the code so far

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...