3
votes

I'm currently trying to make a discord bot with the ability to .mute a user. I have created this script so far that allows people with a "staff" role to run the command, and gives the tagged user a "Muted" role. It also creates one if it doesn't exist already. The problem is the code below does not work. It doesn't say anything in the console but if you have the staff role and you run the command nothing happens.

@commands.has_role("staff")
async def mute(ctx, member: discord.Member=None):
    guild = ctx.guild
    if (not guild.has_role(name="Muted")):
        perms = discord.Permissions(send_messages=False, speak=False)
        await guild.create_role(name="Muted", permissions=perms)
    role = discord.utils.get(ctx.guild.roles, name="Muted")
    await member.add_roles(role)      
    print("???? "+member+" was muted.")
    if (not member):
        await ctx.send("Please specify a member to mute")
        return
@mute.error
async def mute_error(ctx, error):
    if isinstance(error, commands.CheckFailure):
        await ctx.send("You don't have the 'staff' role")  
1

1 Answers

1
votes

This specific line:

print("🔨 "+member+" was muted.")

Prints it in the terminal, or wherever you ran the command. Try await ctx.send Also, try using f-strings if you have version > 3.6 of Python. Also, your error is wrong.

@client.command()
@commands.has_role("staff")
async def mute(ctx, member: discord.Member):
    role = discord.utils.get(ctx.guild.roles, name="Muted")
    guild = ctx.guild
    if role not in guild.roles:
        perms = discord.Permissions(send_messages=False, speak=False)
        await guild.create_role(name="Muted", permissions=perms)
        await member.add_roles(role)
        await ctx.send(f"🔨{member} was muted.")
    else:
        await member.add_roles(role) 
        await ctx.send(f"🔨{member} was muted.")     

@mute.error
async def mute_error(ctx, error):
    if isinstance(error, commands.MissingRole):
        await ctx.send("You don't have the 'staff' role") 
@mute.error
async def mute_error(ctx, error):
    if isinstance(error, commands.BadArgument):
        await ctx.send("That is not a valid member")