0
votes

When running a Discord bot using discord.py, if a user's roles are edited, the information can't be see by the bot. For example:

1: User A (not in role R) uses the "join" command. Bot adds user to R.

User A (now in role R) uses the "leave" command. Bot thinks that the user isn't in R and complains.

2: User A (in role R) uses the "leave" command. Bot removes user from R

User A (not in role R) uses the "join" command. Bot thinks the user is in R already and complains.

If I use a debugger and inspect the given user, it appears that the role never gets added/removed. The role change does appear within Discord itself though. Restarting the bot allows one update again before it breaks, so it makes me think that something isn't updating when the user's roles change.

The code is pretty straightforward too:

async def giveRole(user, wantedRole, message):
    role = getRoleToAdd(wantedRole) # some logic to get the role to leave/join, verified to work

    if role in user.roles:
        await message.channel.send("You've already joined that role")

    else:
        await user.add_roles(role)

The "leave" logic is just flipped, using user.remove_roles(role) instead.

1
Have you upgraded discord.py to 1.5 and added intents? - hardestchat
It occurred to me to use intents, but not that I'm a dummy and have an out-of-date Discord.py. I'll try updating and starting from there, thanks! - The Ploob

1 Answers

0
votes

While you do have to update and add the default intents, I don't think you need intents.members for your usecase.

I did have a similar Issue and while I had Intents.members enabled all along, my issue was that I reused the same member object to check roles before and after assigning new ones.

roles_before = [role.name for role in updated_roles_member.roles]
await assignRolesToUser(roleList, mentioned_user, message.guild)
updated_roles_member = await mentioned_user.guild.fetch_member(mentioned_user.id)
roles_now = [role.name for role in updated_roles_member.roles]

This gave me the new list of roles