[Python] Can add roles, but can't remove them.

I created a simple bot that responds to messages by adding or removing roles appropriately. It is adding roles, but not removing them. Here is the removal part (I just copied the add part, which is working correctly, and changed to remove role and added a check for the resign command:

if str(message.channel) == "private" and message.content.startswith('!resign'):
    role = get(message.author.guild.roles, name="In Group")
    await message.author.remove_roles(role)
    return

Posted Dec 5, 2020, 11:19 AM UTC

13
Asczery•

I did it another way. With context instead of message. Working. with message.author.guild.roles and message.author.roles.Another thing could be, that the str(message.channel) is None instead of "private". So when this is None, the program will not continue with the if, because 1 of the 2 is not equal the value you put in. I recommend, using an aditional get function to get the channel.

@commands.Cog.listener()
    async def on_message(self, ctx):
        if ctx.content.startswith('!resign') and ctx.channel == get(ctx.guild.text_channels, name='CHANNEL'):
            role = get(ctx.author.roles, name='ROLE')
            await ctx.author.remove_roles(role)

Perfectly working.

I did it with ctx because my on_message function is using ctx instead of message. But you can use message too.

RegardsHave a good day

1
diogynOP•

This worked, using message. Thank you so much!

1
Asczery•

Np. have a good day :)

1