Search code examples
pythondiscord.pypython-asyncio

Discord.py asyncio.sleep


@client.command()
@commands.cooldown(1, 10,commands.BucketType.user)
async def barrels(ctx, member:discord.Member = None):
    if member == None:
        member = ctx.message.author

    await asyncio.sleep(2100)
    await ctx.send("The barrel timer has been started.")
    await ctx.send(f"{member.mention} The barrels are almost ready to be picked up!")

Hello, I was wondering if there is a way to add a "Check" persay to see if the asyncio.sleep(2100) is already active and if it is then to not allow it. I'm fairly new to using asyncio and could not find anything after looking for a bit.

I tried using an if statement but that obviously did not work. I haven't tried to much. It's mainly just research that I've done.


Solution

  • The easiest approach would be to simply use max_concurrency

    @client.command()
    @commands.cooldown(1, 10, commands.BucketType.user)
    @commands.max_concurrency(1, commands.BucketType.user)
    async def barrels(ctx, member:discord.Member = None):
        if member == None:
            member = ctx.message.author
    
        await ctx.send("The barrel timer has been started.")
        await ctx.send(f"{member.mention} The barrels are almost ready to be picked up!")
        await asyncio.sleep(2100)
    

    If the command is active (it hasn't finished execution, 'cause of asyncio.sleep) it won't allow the user to run the command again.