简体   繁体   中英

Discord.py Tempmute Command

I'm trying to make a tempmute command in discord.py. I've made the actual command, but I want to be able to use 1s for one second, 1m for one minute, 1h for one hour and 1d for one day.

At the moment, I can only use the time argument in seconds. How do I make it like i aforementioned?

Here is my code:

@client.command()
@commands.has_any_role(569518553112510475, 641312393447866398, 673530318166294550, 670694992314105927, 680137747822018560, 689135000414715980, 677844825004834827, 677848974224392192)
async def tempmute(ctx, member : discord.Member, time=0, reason=None):
    if not member or time == 0 or time == str:
        await ctx.channel.send(embed=commanderror)
        return
    elif reason == None:
        reason = "No Reason Provided"

    muteRole = discord.utils.get(ctx.guild.roles, id=663076470180151339)
    await member.add_roles(muteRole)

    tempMuteEmbed = discord.Embed(colour=embedcolour, description=f"**Reason:** {reason}")
    tempMuteEmbed.set_author(name=f"{member} Has Been Muted", icon_url=f"{member.avatar_url}")
    tempMuteEmbed.set_footer(text=embedfooter)

    await ctx.channel.send(embed=tempMuteEmbed)

    tempMuteModLogEmbed = discord.Embed(color=embedcolour)
    tempMuteModLogEmbed.set_author(name=f"[MUTE] {member}", icon_url=f"{member.avatar_url}")
    tempMuteModLogEmbed.add_field(name="User", value=f"{member.mention}")
    tempMuteModLogEmbed.add_field(name="Moderator", value=f"{ctx.message.author}")
    tempMuteModLogEmbed.add_field(name="Reason", value=f"{reason}")
    tempMuteModLogEmbed.add_field(name="Duration", value=f"{str(time)}")
    tempMuteModLogEmbed.set_footer(text=embedfooter)
    modlog = client.get_channel(638783464438759464)
    await modlog.send(embed=tempMuteModLogEmbed)

    tempMuteDM = discord.Embed(color=embedcolour, title="Mute Notification", description="You Were Muted In **The Official Vanmanyo Discord Server**")
    tempMuteDM.set_footer(text=embedfooter)
    tempMuteDM.add_field(name="Reason", value=f"{reason}")
    tempMuteDM.add_field(name="Duration", value=f"{time}")

    userToDM = client.get_user(member.id)
    await userToDM.send(embed=tempMuteDM)

    await asyncio.sleep(time)
    await member.remove_roles(muteRole)

    unMuteModLogEmbed = discord.Embed(color=embedcolour)
    unMuteModLogEmbed.set_author(name=f"[UNMUTE] {member}", icon_url=f"{member.avatar_url}")
    unMuteModLogEmbed.add_field(name="User", value=f"{member.mention}")
    unMuteModLogEmbed.set_footer(text=embedfooter)
    modlog = client.get_channel(638783464438759464)
    await modlog.send(embed=unMuteModLogEmbed)

You can create a seperate function that converts the values into the desired seconds.

For example:

time_convert = {"s": 1, "m": 60, "h": 3600, "d": 86400}

def convert_time_to_seconds(time):
    try:
        return int(time[:-1]) * time_convert[time[-1]]
    except:
        return time

This will try to convert the time. If it cant convert it it will just return original value. So you can either use "40s" or "40".

Make the bot accept every character after 'time' as 'reason'

async def tempmute(ctx, member : discord.Member, time=0,*, reason=None):

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM