1

i want to make tempmute command in discord.py, but I don't have idea how can I do this. So I'm asking SO.

I want to make command which will add Muted role to user for specified time, e.g. mute @member 10m spamming -> it'll mute user for 10 minutes with reason = "spamming". (My trails are over time expressed only in secconds). I want to can chose time between s (seccond), m (minute), h (hour), d (day).

This is what I made:

@client.command()
@commands.has_permissions(kick_members=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
    if reason == None:
        reason = "no reason provided"
    
    if member.id == ctx.author.id:
        await ctx.send(f"{ctx.author.mention}, you can't mute yourself")

    role = discord.utils.get(ctx.guild.roles, name="Muted")

    if role in ctx.guild.roles:
        await member.add_roles(role)
        await ctx.send(f"Muted {member.mention}")
        await asyncio.sleep(time)
        await member.remove_roles(role)
    else:
        await ctx.send("No role named `Muted`!")
iXekPL
  • 39
  • 2
  • 7

2 Answers2

0

The way I would do it would be to add each temp-muted member to a list (or just a special temp-mute role) and record the time at which they should be unmuted. (You can convert the selected duration in (h/m/s) to a specific time using the datetime module (specifically, timedelta).)

Then create one timer thread (an example is outlined in this answer) that runs a function to check if any of the temp-muted members should be unmuted be comparing the current time to that member's scheduled unmute time.

To unmute, simply remove that member from the list/role.

John Paul R
  • 649
  • 5
  • 10
  • I think its make sense, but im beginer in programing, and I don't fullu understand what do you mean, so can You make exemplary code for me? – iXekPL Dec 07 '20 at 20:02
  • What part of this are you confused about in particular? Do you need help parsing the time (10h 3m 2s -> datetime object)? Or are you unsure about how to set up the threaded timer? Or something else? (or multiple?) Let me know what, specifically, you need help with. – John Paul R Dec 07 '20 at 22:10
  • In [this gist](https://gist.github.com/John-Paul-R/bd0d428e90977e1ae54b628ce123785c), I show how to parse the user-submitted time and store in a numerical format, using regex. Perhaps you'll find this useful. – John Paul R Dec 07 '20 at 23:38
0

You can simply use a Time converter ;D

# Here you can choose your Time letters, you can set own letters if you want to.
time_convert = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800}

# here you can convert your time easy by doing a converttime("20m")
# after that it will automatically convert it into a int with the right time.
def converttime(time):
    try:
        return int(time[:-1] ) * time_convert[time[-1]]
    except:
        return time

edit: you have to make your time variable into a str so a user can input a letter behind the numbers :D

Extracted
  • 44
  • 3