如何在命令 (discord.py) 中创建一个函数,它只允许您在一分钟内使用一次命令?

How do you create a function in a command(discord.py) that only lets you use a command once in one minute?

如何在命令 (discord.py) 中创建一个只允许您在一分钟内使用一次命令的函数? 这是我想要计时的代码部分:

@bot.command()
async def busk(ctx):
        member = ctx.message.author.id #gets the user ID, which i use as the _id in pymongo because everyones is different.
        results1 = collection.find_one({"_id": member})#checks if their account exists
        results = collection.find({"_id": member})#opens to their 'account'
        if results1 == None: #if their account does not exist
                post = {"_id": member, "coins": 0} #create an account with their member ID as the _id
                collection.insert_one(post)#inserts the document(account) to the database.
                await ctx.send("You did not have an account, so we created one for you! try running this command again.")
        else:#If they do have an account:
                for result in results:
                        randomdonation = random.choice([1,2,1,'none',1,'none',10,'none',10,12,11,'none',11,'none',14,25,15,'none',15,13,13,12,18,'none',18,19,20,21,21,2,2,100,3,4,5,14,14])#random choices, selected from an array so I can choose which commonly pop up.
                        if randomdonation == "none":#if they get the choice 'none'
                                await ctx.send("srry u get " + randomdonation)
                        else:
                                db.testing.update_one({"_id": member}, {'$inc': {"coins": randomdonation}})
                                await ctx.send(str(randomdonation) + " Coins were added to your bank!")

这就是我想要计时的命令,我尝试使用 time.sleep(),但它只会让整个机器人休眠。

@bot.command()
@commands.cooldown(1, 60, commands.BucketType.user)
async def busk(ctx):
        member = ctx.message.author.id #gets the user ID, which i use as the _id in pymongo because everyones is different.
        results1 = collection.find_one({"_id": member})#checks if their account exists
        results = collection.find({"_id": member})#opens to their 'account'
        if results1 == None: #if their account does not exist
                post = {"_id": member, "coins": 0} #create an account with their member ID as the _id
                collection.insert_one(post)#inserts the document(account) to the database.
                await ctx.send("You did not have an account, so we created one for you! try running this command again.")
        else:#If they do have an account:
                for result in results:
                        randomdonation = random.choice([1,2,1,'none',1,'none',10,'none',10,12,11,'none',11,'none',14,25,15,'none',15,13,13,12,18,'none',18,19,20,21,21,2,2,100,3,4,5,14,14])#random choices, selected from an array so I can choose which commonly pop up.
                        if randomdonation == "none":#if they get the choice 'none'
                                await ctx.send("srry u get " + randomdonation)
                        else:
                                db.testing.update_one({"_id": member}, {'$inc': {"coins": randomdonation}})
                                await ctx.send(str(randomdonation) + " Coins were added to your bank!")

有一个@command.cooldown 属性 可以添加到您的命令中。在我使用它的情况下,您可以每 60 秒使用一次。如果您多次使用它,它会在控制台中记录错误。如果您想处理该错误,请在代码顶部添加如下内容:

@bot.event
async def on_command_error(ctx, error):
        if isinstance(error, commands.CommandOnCooldown):
                await ctx.send('You are on cooldown! Try this command again later.')
                return
        raise error

因为它会引发错误 CommandOnCooldown。