Ask Your Question
2

How can I permit other users to run a command while one user is already running it in discord.py?

asked 2021-06-07 11:00:00 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-10-31 14:00:00 +0000

david gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-06-07 11:00:00 +0000

Seen: 8 times

Last updated: Oct 31 '21