How do you create a function in a command(discord.py) that only lets you use a command once in one minute? this is the part of my code I would like to time:
@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!")
thats the command I would like to time, and I have tried to use time.sleep(), but it just sleeps the whole bot.
@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!")
there is an @command.cooldown property you can add to your command. In the case that I used it, you can use it once per 60 seconds. If you use it more than that, it logs an error in the console. If you want to handle that error, add something like this to the top of your code:
@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
because it raises the error CommandOnCooldown.