Everything works except when I type 10seconds for duration it only does 2 seconds and 10m does 1min

Viewed 19

Code

Everything works and all but it does 2 seconds instead of 10seconds and does 1minute instead of 10 I was just wandering if there is anything I did wrong because I have been trying to figure it out for the last 20 minutes and I couldnt find the answer to why it isn't doing the right duration


@client.command()
async def temprole(ctx, member : discord.Member, role : discord.Role, duration, *, reason=None):
    if ctx.message.author.guild_permissions.manage_roles:
        time_convert = {"s":1, "m":60, "h":3600,"d":86400}
        temprole= int(duration[0]) * time_convert[duration[-1]]
        await member.add_roles(role)
        await ctx.send(embed = discord.Embed(description=f"{member.mention} has been given the role {role} by {ctx.author.mention} for {reason}", color=discord.Color.yellow()), delete_after=15)
        embed = discord.Embed(title="Temprole Log", description=f"{member.mention} has been temporarily given the role {role} for {duration} by {ctx.author.mention}", color=0x1355ed)
        embed.add_field(name="User", value=f"{member}", inline=True)
        embed.add_field(name="UserID", value=f"{member.id}", inline=True)
        embed.add_field(name="Moderator", value=f"{ctx.author}", inline=True)
        embed.add_field(name="Reason", value=f"{reason}", inline=True)
        embed.add_field(name="duration", value=f"{duration}", inline=True)
        embed.set_footer(text=f"Temprole Log : {member.name}")
        embed.timestamp = datetime.datetime.utcnow()
        logchannel = client.get_channel(1015274200841330749)
        await logchannel.send(embed=embed)
        await ctx.message.delete()
        print(f"{member.name} was given the role {role}")
        await asyncio.sleep(temprole)
        await member.remove_roles(role)
        await ctx.send(embed = discord.Embed(description=f"{member.mention} has been removed from the role {role} after {duration}", color=discord.Color.yellow()), delete_after=15)
        embed = discord.Embed(title="Temprole Log", description=f"{member.mention} has been removed from the role {role} automatically by <@1013161207072571482> after {duration}", color=0x1355ed)
        embed.add_field(name="User", value=f"{member}", inline=True)
        embed.add_field(name="UserID", value=f"{member.id}", inline=True)
        embed.add_field(name="Moderator", value=f"{ctx.author}", inline=True)
        embed.add_field(name="Reason", value=f"{reason}", inline=True)
        embed.add_field(name="duration", value=f"{duration}", inline=True)
        embed.set_footer(text=f"Temprole Log : {member.name}")
        embed.timestamp = datetime.datetime.utcnow()
        logchannel = client.get_channel(1015274200841330749)
        await logchannel.send(embed=embed)
        print(f"{member.name} was removed from the role {role}")
    else:
        em = discord.Embed(title="Permissions Required!", description=f"{ctx.author.name} You do not have the required Permissions to use this command", color=discord.Colour.red())
        await ctx.send(embed=em)
2 Answers

I'm not sure what you're passing in for the duration parameter but you're calling a key from the dictionary so duration would only ever return 1 second, 1 minute, 1 hour, or 1 day.

In your code:

temprole= int(duration[0]) * time_convert[duration[-1]]

I assume that this is supposed to get the number in your input. However, your code will not not working as intended.

duration is a str object, so duration[0] returns the first character in the string. So for example, if duration = '10s', duration[0] will return '1', and int(duration[0]) will return 1.

To fix this, change int(duration[0]) to int(duration[:-1]).

Basically, [:-1] is the same as [0:-1], which means "get all the elements from index 0 (the first index) to index -2 (2nd last element)".

Related