How do I set an optional argument in discord.py?

Viewed 5468

I've tried to research online, but no other questions were able to help me with my issue.

Here's my scenario.

I am making a mute command in discord.py.

I want the time to be optional, but if the time is not specified I want that argument to be part of the reason.

Clarification on what I mean:

@client.command()
@commands.has_permissions(kick_members=True)
async def mute(ctx, member: Member = None, time: int = None, *, reason = None):
    pass

Here, if time is None, then make it part of reason.

The bot will accept both of these:

!mute @user 1h spam and !mute @user spam

Is this possible?

2 Answers

You can optionally wait asynchronously and then unmute the user:

@bot.command()
async def mute(ctx, member: discord.Member, time: typing.Optional[int]):
    await member.edit(mute=True)
    if time:
        await asyncio.sleep(time)
        await member.edit(mute=False)

You can set a default argument:

@bot.command()
async def mute(ctx, member: discord.Member, time=None):
    if not time:
        # Mute indefinitely? do whatever you want
    else:
        # Mute for x amount of time

References:

Related