discord.py How to add Mute command

39,730

Solution 1

I rewritten my code and got a working peice of code, here it is if others need it :)

@bot.command(pass_context = True)
async def mute(ctx, member: discord.Member):
     if ctx.message.author.server_permissions.administrator or ctx.message.author.id == '194151340090327041':
        role = discord.utils.get(member.server.roles, name='Muted')
        await bot.add_roles(member, role)
        embed=discord.Embed(title="User Muted!", description="**{0}** was muted by **{1}**!".format(member, ctx.message.author), color=0xff00f6)
        await bot.say(embed=embed)
     else:
        embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
        await bot.say(embed=embed)

Solution 2

you should use this :

await member.edit(mute = True)
Share:
39,730
Moog
Author by

Moog

Updated on July 15, 2022

Comments

  • Moog
    Moog almost 2 years

    I'm making a discord bot and I really want to make a mute command to mute toxic users. This is the code I've currently done, this isn't the rewrite version.

        @bot.command(pass_context = True)
    async def mute(ctx, user_id, userName: discord.User):
        if ctx.message.author.server_permissions.administrator:
            user = ctx.message.author
            role = discord.utils.get(user.server.roles, name="Muted")
            await client.add_roles(user, role)
         else:
           embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
           await bot.say(embed=embed)
    
    • user3483203
      user3483203 about 6 years
      What is your question?
    • Moog
      Moog about 6 years
      I want to know how to make a mute command. Up above is my current code and it doesnt work and I dont know how to fix it.
    • user3483203
      user3483203 about 6 years
      "Why isn't my code working" is considered off topic on Stack overflow.
    • Moog
      Moog about 6 years
      and if you know how, how would I also add a time interval e.g >mute @toxicuser 60m
    • Patrick Haugh
      Patrick Haugh about 6 years
      @chrisz No it's not. @Leighton. Keep a dictionary or set of Users that have been muted, and the mute command just adds them to that list. Then in your on_message event you can call delete_message on their messages. See this similar question about muting channels
  • David Buck
    David Buck over 3 years
    Welcome to Stack Overflow. When posting modified code as an answer, please help the OP and other users understand what you fixed and why with a written explanation. "I fixed some things" requires people to both hunt for the changes and, when they have found them, wonder why you have changed these things.
  • norahCii
    norahCii about 3 years
    message.author.id is an int so you should do ctx.message.author.id == 194151340090327041