1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Why doesn't my timeout command take time correctly?

Discussão em 'Python' iniciado por Stack, Setembro 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a timeout command for my Discord bot, but I need help with parsing time.

    The command is supposed to go like +timeout @member 2d 3h 40m but it just takes the 2d, times out the user for 2 days, and puts "3h 40min" as the reason, which it's not supposed to do.

    This is how the code is so far: (missing permissions/arguments and the rest are handled below in @timeout.error)

    @commands.command(
    name="timeout",
    aliases=["mute", "t"],
    help=""
    )
    @has_permissions(moderate_members=True)
    async def timeout(self, ctx, user: discord.Member, time, *, reason = "No reason provided"):
    if user == ctx.author:
    await ctx.send("You can't timeout yourself!")
    return
    if user.top_role >= ctx.guild.me.top_role:
    await ctx.send("I can't timeout this user, they have a role that's higher or equal to mine.")
    return
    if user.top_role >= ctx.author.top_role and ctx.author != ctx.guild.owner:
    await ctx.send("You can't timeout this user, they have a role that's higher or equal to yours.")
    return

    pattern = re.compile(r"(\d+)([smhd])")
    matches = list(pattern.finditer(time.lower()))

    if not matches:
    await ctx.send("Invalid time format, try: 20m, 1h, 3d.")
    return

    total_duration = datetime.timedelta(0)

    for match in matches:
    value = int(match.group(1))
    unit = match.group(2)

    if unit == "s":
    total_duration += datetime.timedelta(seconds=value)
    elif unit == "m":
    total_duration += datetime.timedelta(minutes=value)
    elif unit == "h":
    total_duration += datetime.timedelta(hours=value)
    elif unit == "d":
    total_duration += datetime.timedelta(days=value)

    timeoutEmbed = discord.Embed(
    title = f"Timeout on user {user.name} set successfully.",
    description = f"**Reason:** {reason}\n**Moderator:** {ctx.author.mention}")
    await ctx.send(embed = timeoutEmbed)

    Continue reading...

Compartilhe esta Página