Bot unmuting member when use tempmute command

Viewed 85

I made the tempmute command, mute command, and unmute command. My problem is when I mute a member with mute command and I try to tempmute him, bot is unmuting this member. This is my full code:

@bot.command()
async def tempmute(ctx, member:discord.Member, czas:int, *, reason = "Brak powodu"):
  if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
    muted = discord.utils.get(ctx.guild.roles, name="Muted")
    if muted in member.roles:
      await ctx.send("Nie możesz wyciszyć kogoś, kto już jest wyciszony!")
    else:
      await member.add_roles(muted)
      await ctx.send(f"Wyciszono użytkownika {member.mention} na {czas} minut(-ę/-y) z powodu: {reason}")
      await asyncio.sleep(czas*60)
    if muted in member.roles:
      await member.remove_roles(muted)
      await ctx.send(f"Odciszono użytkownika {member.mention}, ponieważ jego czas wyciszenia ({czas} minut(-a/-y)) minął")
    else:
      pass
  else:
      await ctx.send("Nie posiadasz permisji!")

@tempmute.error
async def tempmute_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
      await ctx.send("Poprawne użycie (przykład):\n,tempmute <@839076078651441204> 1 (czas w minutach) powód")
    else:
      await ctx.send("Nie posiadasz permisji!")

@bot.command()
async def unmute(ctx, *, member:discord.Member):
  if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
    muted = discord.utils.get(ctx.guild.roles, name="Muted")
    if muted in member.roles:
      await member.remove_roles(muted)
      await ctx.send(f"Odciszono {member.mention}")
    else:
      await ctx.send(f"{member.mention} nie jest wyciszony/a!")

@unmute.error
async def unmute_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
      await ctx.send("Poprawne użycie (przykład):\n,unmute <@839076078651441204>")
    else:
      await ctx.send("Nie posiadasz permisji!")

@bot.command()
async def mute(ctx, member:discord.Member, *, reason="Brak powodu"):
  if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
    muted = discord.utils.get(ctx.guild.roles, name="Muted")
    if muted in member.roles:
      await ctx.send(f"{member.mention} jest już wyciszony/a!")
    else:
      await member.add_roles(muted)
      await ctx.send(f"Wyciszono {member.mention} z powodu: {reason}")

@mute.error
async def mute_error(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    if ctx.message.author.guild_permissions.administrator or ctx.message.author.guild_permissions.manage_roles:
      await ctx.send("Poprawne użycie (przykład):\n,mute <@839076078651441204> powód")
    else:
      await ctx.send("Nie posiadasz permisji!")

Works just like in this picture, can someone help me?

1 Answers

I don't really think the way you are trying to write the command is feasible.

I am going to explain one by one the procedure of writing a code for the command.

TEMPMUTE Command.

Step 1: Define the function and permissions

We are going to define the function with some permissions restrictions on the use. People with the permissions specified permissions.

Before you use this, you need to have the following in your code:

from discord.ext import commands

(Ignore if you have it)

Here is how:

@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time, *, reason=None):

You can add many more permission if you want and use True or False to allow or deny.

Step 2: Making a role finding condition. If the role doesn't exist then create a Muted role, if exist then use it.

So I am creating a condition for if the role exist. It will check if the role exist. If it exist then we will simply use it but if it doesn't we will create one with specific permissions.

Here is how:

if discord.utils.get(ctx.guild.roles, name="Muted"):
    mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
else:
    perms = discord.Permissions(send_messages=False, add_reactions=False, connect=False, speak=False)
    await bot.create_role(name="Muted", permissions=perms)
    mute_role = discord.utils.get(ctx.guild.roles, name="Muted") 

Step 3: Checking if someone is muted or not.

Now I am creating a condition for if the member is muted or not. We will through the member's roles and check it.

Here is how:

if mute_roles in member.roles:
    await ctx.channel.send(f"{member.mention} is already muted!")
else:
    # code here (more steps will explain how)

Step 4: Adding conditions and muting the member.

Now we will add restriction permissions for this command. Who all people can't be muted and who all can be.

First we add our first condition, that administrators can not be muted. Here is how:

if member.guild_permissions.administrator:    
    isadminembed=discord.Embed(title="Tempmute", description=f"Hi {ctx.author.mention}, you can't mute {member.mention} as they are a Server Administrator.", color=discord.Colour.red())
    isadminembed.set_author(name="Bot")
    await ctx.channel.send(embed=isadminembed)

Now we will add else condition, that all other members except administrators can be muted. Here is how:

else:
    time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "M": 2419200, "y": 29030400}
    mute_time = int(time[:-1]) * time_conversion[time[-1]]

    await member.add_roles(mute_role)
    mutedembed=discord.Embed(title="Tempmute", description=f"The member, {member.mention} has been muted by the moderator {ctx.author.mention}. \n \nTime: {mute_time} seconds \nReason: {reason}", color=discord.Colour.random())
    mutedembed.set_author(name="Bot")
    await ctx.channel.send(embed=mutedembed)

    await asyncio.sleep(mute_time)
    await member.remove_roles(mute_role)
    await ctx.channel.send(f"{member.mention} has been unmuted!")

I have added time conversion here. The time input has to be:

1s for one second, 1m for one minute, etc. You can use it for years too.


Tempmute Command Compiled

This is all the code compiled together as the command.

@bot.command()
@commands.has_permissions(manage_roles=True)
async def tempmute(ctx, member: discord.Member, time, *, reason=None):
    if discord.utils.get(ctx.guild.roles, name="Muted"):
        mute_role = discord.utils.get(ctx.guild.roles, name="Muted")
    else:
        perms = discord.Permissions(send_messages=False, add_reactions=False, connect=False, speak=False)
        await bot.create_role(name="Muted", permissions=perms)
        mute_role = discord.utils.get(ctx.guild.roles, name="Muted")

    if mute_roles in member.roles:
        await ctx.channel.send(f"{member.mention} is already muted!")
    
    else:
        if member.guild_permissions.administrator:    
            isadminembed=discord.Embed(title="Tempmute", description=f"Hi {ctx.author.mention}, you can't mute {member.mention} as they are a Server Administrator.", color=discord.Colour.red())
            isadminembed.set_author(name="Bot")
            await ctx.channel.send(embed=isadminembed)
        
        else:
            time_conversion = {"s": 1, "m": 60, "h": 3600, "d": 86400, "w": 604800, "M": 2419200, "y": 29030400}
            mute_time = int(time[:-1]) * time_conversion[time[-1]]

            await member.add_roles(mute_role)
            mutedembed=discord.Embed(title="Tempmute", description=f"The member, {member.mention} has been muted by the moderator {ctx.author.mention}. \n \nTime: {mute_time} seconds \nReason: {reason}", color=discord.Colour.random())
            mutedembed.set_author(name="Bot")
            await ctx.channel.send(embed=mutedembed)

            await asyncio.sleep(mute_time)
            await member.remove_roles(mute_role)
            await ctx.channel.send(f"{member.mention} has been unmuted!")

TEMPMUTE Command Error Handling

Step 1: Defining the function as an event.

We will define the function in the script as tempmute command's error and declare it as an error.

Here is how:

@bot.error
async def tempmute_error(ctx, error):

Step 2: Adding the instance of the error.

We will now add a condition for the error. Our error will be: MissingRequiredArgument error as the command can lack necessary arguments.

So the isinstance will check the error and then do the stuff.

Here is how:

if isinstance(error, discord.ext.commands.MissingRequiredArgument):
    tempmuteerrorembed=discord.Embed(title=f"Missing Argument! {error}", description=f"Hello {ctx.author.mention}! You have not entered the needed argument. \n Either you forgot to **mention the member** or you forgot to **enter the time of the mute** you want. \n \n Please check this again and add the necessary argument in the command. \n \nThis is the syntax: \n```!tempmute <mention member> <time: 1s, 2h, 4y etc..> <reason (optional)>```", color=discord.Colour.red())
    tempmuteerrorembed.set_author(name="Bot")
    await ctx.send(embed=tempmuteerrorembed)

This will work for MissingRequiredArguement and show the error with the syntax in it for the command's correct usage.


TEMPMUTE Command Error Handling Compiled

Here is the compiled code of Error Handling of TEMPMUTE Command.

@bot.error
async def tempmute_error(ctx, error):
    if isinstance(error, discord.ext.commands.MissingRequiredArgument):
        tempmuteerrorembed=discord.Embed(title=f"Missing Argument! {error}", description=f"Hello {ctx.author.mention}! You have not entered the needed argument. \n Either you forgot to **mention the member** or you forgot to **enter the time of the mute** you want. \n \n Please check this again and add the necessary argument in the command. \n \nThis is the syntax: \n```!tempmute <mention member> <time: 1s, 2h, 4y etc..> <reason (optional)>```", color=discord.Colour.red())
        tempmuteerrorembed.set_author(name="Bot")
        await ctx.send(embed=tempmuteerrorembed)

The text inside the command is not what you have written as this is the command I created for my bot. You can change them yourself accordingly.

This will work for you and I hope you understand it. Please ask about any issue in the comments. :)

Thank You! :D

Related