Discord.py mute command

Question:

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.

Asked By: jwjw2069

||

Answers:

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")    
Answered By: tjallo

since discord.py 2.0 was released, you should use discord.Member.timeout. I coded a app_command wich timeouts a specified member for, in argument given, time.

Code:

@app_commands.command(name="mute", description="mutes specified member.")
    @app_commands.default_permissions(moderate_members=True)
    async def timeout(self, ctx, member: discord.Member, seconds: int = 0, minutes: int = 0, hours: int = 0, days: int = 0, weeks: int = 0, reason: str = None):
        duration = datetime.timedelta(seconds=seconds, minutes=minutes, hours= hours, days=days, weeks=weeks)
        await member.timeout(duration, reason=reason)
        await ctx.response.send_message(f"{member.mention} has been muted until <t:{duration}:R>.")
Answered By: raion
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.