Skip to content Skip to sidebar Skip to footer

Tempmute Command With Converting Time In Discord.py

i want to make something like time convert in my discord bot. Now, to use tempmute command, i need to set time only in seconds, and i want to make converting e.g. 1s = 1; 1h = 3600

Solution 1:

If your input may be 15s, 20min, 1h, 2d, your possible solution is:

import re

deftempmute(time=0):
    time_list = re.split('(\d+)',time)
    if time_list[2] == "s":
        time_in_s = int(time_list[1])
    if time_list[2] == "min":
        time_in_s = int(time_list[1]) * 60if time_list[2] == "h":
        time_in_s = int(time_list[1]) * 60 * 60if time_list[2] == "d":
        time_in_s = int(time_list[1]) * 60 * 60 * 60return time_in_s


print(tempmute("15h"))

>>> 54000

Update: so the complete code should look something like this. Your Input will be a string, dont return None if its not a string! You input will be in the form of like 15min, 3s or 5h, otherwise the timeout will be 0 seconds.

# tempmute@client.command()@commands.has_permissions(kick_members=True)asyncdeftempmute(ctx, member: discord.Member, time=0, reason=None):
    ifnot member or time == 0:
        returnelif reason == None:
        reason = 'no reason provided'try:
        if time_list[2] == "s":
            time_in_s = int(time_list[1])
        if time_list[2] == "min":
            time_in_s = int(time_list[1]) * 60if time_list[2] == "h":
            time_in_s = int(time_list[1]) * 60 * 60if time_list[2] == "d":
            time_in_s = int(time_list[1]) * 60 * 60 * 60except:
        time_in_s = 0
 
    tempmuteembed = discord.Embed(colour=discord.Colour.from_rgb(0, 255, 0))
    tempmuteembed.set_author(icon_url=member.avatar_url, name=f'{member} has been tempmuted!')
    tempmuteembed.set_footer(text=f"{ctx.guild.name}{datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
    tempmuteembed.add_field(name=f'ID:', value=f'{member.id}', inline=False)
    tempmuteembed.add_field(name='Reason:', value=f"{reason}")
    tempmuteembed.add_field(name='Duration:', value=f"{time}")
    tempmuteembed.add_field(name=f'By:', value=f'{ctx.author.name}#{ctx.author.discriminator}', inline=False)
 
    await ctx.channel.purge(limit=1)
 
    guild = ctx.guild
    for role in guild.roles:
        if role.name == 'Muted':
            await member.add_roles(role)
            await ctx.send(embed=tempmuteembed)
            await asyncio.sleep(time_in_s)
            await member.remove_roles(role)
            return

Post a Comment for "Tempmute Command With Converting Time In Discord.py"