Search code examples
pythonpycharmdiscord.pyroles

What's wrong with my AddRole + RemoveRole command?


So I recently tried to make some AddRole and RemoveRole commands for my Discord Bot. I used a video from James S on YT (episode 15) and almost copied almost exactly. I work in PyCharm, with the Python language.

This is the code:

@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def addRole(self, ctx, user : discord.Member, *, role : discord.Role):

    if role in user.roles:
        await ctx.send(f"{user.mention} already has that role! ({role})")
    else:
        await user.add_roles(role)
        await ctx.send(f"Added {role} to {user.mention}!")

@addRole.error
async def role_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")


@commands.command(pass_context = True)
@commands.has_permissions(manage_roles = True)
async def removeRole(self, ctx, user : discord.Member, *, role : discord.Role):

    if role in user.roles:
        await user.remove_roles(role)
        await ctx.send(f"{user.mention} doesn't have that role! ({role})")
    else:

        await ctx.send(f"Removed {role} from {user.ention}!")

@removeRole.error
async def removeRole_error(self, ctx, error):
    if isinstance(error, commands.MissingPermissions):
        await ctx.send("You need to have permissions to do this!")

This is the error I get; Ignoring exception in command None: discord.ext.commands.errors.CommandNotFound: Command "addRole" is not found


Solution

  • I could not even run your code, because it was stating I'm missing parameters, so I refactored and corrected it a bit and it works fine with granting/revoking roles for me. I could not really recreate the problem you have.

    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix="!")
    
    
    @bot.command(pass_context=True)
    @commands.has_permissions(manage_roles=True)
    async def addRole(ctx, user: discord.Member, *, role: discord.Role):
        if role in user.roles:
            await ctx.send(f"{user.mention} already has that role! ({role})")
        else:
            await user.add_roles(role)
            await ctx.send(f"Added {role} to {user.mention}!")
    
    
    @addRole.error
    async def role_error(ctx, error):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send("You need to have permissions to do this!")
    
    
    @bot.command(pass_context=True)
    @commands.has_permissions(manage_roles=True)
    async def removeRole(ctx, user: discord.Member, *, role: discord.Role):
        if role in user.roles:
            await user.remove_roles(role)
            await ctx.send(f"{user.mention} doesn't have that role! ({role})")
        else:
    
            await ctx.send(f"Removed {role} from {user.ention}!")
    
    
    @removeRole.error
    async def removeRole_error(ctx, error):
        if isinstance(error, commands.MissingPermissions):
            await ctx.send("You need to have permissions to do this!")
    
    
    bot.run("your token here")
    

    What I've changed:

    1. I've defined the bot and prefix to use with commands (I'm not sure you did that since you pasted only a part of your code).
    2. I've fixed some pip errors (spaces, lines), that should not affect code at all, but now it looks clearer.
    3. I've removed self from required parameters since you were not using it anyway.

    ....and after I've done that everything worked fine. Check out my code and let me know if the problem still exists.