pythoncommandmute

Discord.py mute command


So I finished my mute command then I tested it out on one person and it worked. But when it comes to muting to different people it doesn't. I muted one person for 1 minute and the other person for 10 seconds. Since I did the 1m mute first it did that mute first then after that I did the 10 second mute on a different person. It waited till the one minute mute had finished before doing the 10 second mute.How do I stop this from happening? here is my code:

@client.command()
@commands.has_role("Mod")
async def mute(ctx, user : discord.Member, duration = 0,*, unit = None):
    roleobject = discord.utils.get(ctx.message.guild.roles, id=730016083871793163)
    await ctx.send(f":white_check_mark: Muted {user} for {duration}{unit}")
    await user.add_roles(roleobject)
    if unit == "s":
        wait = 1 * duration
        time.sleep(wait)
    elif unit == "m":
        wait = 60 * duration
        time.sleep(wait)
    await user.remove_roles(roleobject)
    await ctx.send(f":white_check_mark: {user} was unmuted")

There are no errors.


Solution

  • You are using time.sleep(wait), this halts all other code until the wait time period is over. When you are using time.sleep python does not accept any other inputs because it is still 'busy' with sleeping.

    You should look into coroutines to solve this problem.

    This post give a great example of what you are trying to achieve: I need help making a discord py temp mute command in discord py

    I think this edit should work:

    #This should be at your other imports at the top of your code
    import asyncio
    
    async def mute(ctx, user : discord.Member, duration = 0,*, unit = None):
        roleobject = discord.utils.get(ctx.message.guild.roles, id=730016083871793163)
        await ctx.send(f":white_check_mark: Muted {user} for {duration}{unit}")
        await user.add_roles(roleobject)
        if unit == "s":
            wait = 1 * duration
            await asyncio.sleep(wait)
        elif unit == "m":
            wait = 60 * duration
            await asyncio.sleep(wait)
        await user.remove_roles(roleobject)
        await ctx.send(f":white_check_mark: {user} was unmuted")