Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can use a Semaphore to limit the number of users who can run the command at the same time. Here's an example:

import discord
from discord.ext import commands
import asyncio

# Create a Semaphore with a limit of 1
semaphore = asyncio.Semaphore(1)

bot = commands.Bot(command_prefix='!')

@bot.command()
async def mycommand(ctx):
    # Acquire the Semaphore
    await semaphore.acquire()

    try:
        # Do some stuff
        await ctx.send('Processing...')

        # Sleep for a bit (to simulate some work)
        await asyncio.sleep(5)

        # Do some more stuff
        await ctx.send('Done!')

    finally:
        # Release the Semaphore
        semaphore.release()

bot.run('TOKEN')

In this example, the Semaphore limits the number of users running mycommand to 1 at a time. If one user is already running the command and another user tries to run it, they will have to wait until the first user completes the command and releases the Semaphore.