Search code examples
pythondiscorddiscord.py

Discord getting user roles from exact guild and removing a specific role if conditions match


I'm currently programming a discord bot in python that automatically removes a specific role called 'Onboarding' if a user profile is updated by choosing one or more roles other than this role.

When I run the code I don'tget any bugs but still it doesn't remove the role called 'Onboarding' if a user picks one or more of the other roles.

Can someone please give advice? This is the code:

import discord
from discord.ext import commands
from discord.ext.commands import Bot
import os

my_secret = os.environ['DISCORD_TOKEN']
from discord.ext import commands
from keep_alive import keep_alive


bot = discord.Client(intents=discord.Intents.default())

@bot.event
async def on_member_update(before, after):
    travel_role = discord.utils.get(after.guild.roles, name="๐Ÿ›ซ | travel") 
    onboarding_role = discord.utils.get(after.guild.roles, name="Onboarding")
    art_role = discord.utils.get(after.guild.roles, name="๐ŸŽจ | art")
    awareness_role = discord.utils.get(after.guild.roles, name="๐Ÿ”ฎ | awareness")
    blogging_role = discord.utils.get(after.guild.roles, name="๐Ÿ“ฐ | blogging-vlogging")
    books_role = discord.utils.get(after.guild.roles, name="๐Ÿ“š | books")
    business_role = discord.utils.get(after.guild.roles, name="๐Ÿ’ผ | business-career")
    comedy_role = discord.utils.get(after.guild.roles, name="๐Ÿ˜น | comedy-entertainment")
    education_role = discord.utils.get(after.guild.roles, name="๐ŸŽ“ | education")
    fashion_role = discord.utils.get(after.guild.roles, name="๐Ÿ’„ | fashion-beauty")
    family_role = discord.utils.get(after.guild.roles, name="๐Ÿค— | family-relationships")
    food_role = discord.utils.get(after.guild.roles, name="๐Ÿ” | food")
    gaming_role = discord.utils.get(after.guild.roles, name="๐Ÿ•น | gaming")
    health_role = discord.utils.get(after.guild.roles, name="๐Ÿ‹๏ธโ€โ™€๏ธ | health-fitness")
    it_role = discord.utils.get(after.guild.roles, name="๐Ÿ–ฅ | it-electronics")
    luxury_role = discord.utils.get(after.guild.roles, name="๐Ÿ‘ธ | luxury-lifestyle")
    motorsports_role = discord.utils.get(after.guild.roles, name="๐ŸŽ | motorsports")
    movies_role = discord.utils.get(after.guild.roles, name="๐Ÿฟ | movies-series")
    music_role = discord.utils.get(after.guild.roles, name="๐ŸŽน | music")
    poetry_role = discord.utils.get(after.guild.roles, name="๐Ÿงš | poetry")
    sustainability_role = discord.utils.get(after.guild.roles, name="๐Ÿƒ | sustainability-environment")

    
    if onboarding_role in after.guild.roles and (art_role in after.guild.roles or awareness_role in after.guild.roles or blogging_role in after.guild.roles or books_role in after.guild.roles or business_role in after.guild.roles or comedy_role in after.guild.roles or education_role in after.guild.roles or fashion_role in after.guild.roles or family_role in after.guild.roles or food_role in after.guild.roles or gaming_role in after.guild.roles or health_role in after.guild.roles or it_role in after.guild.roles or luxury_role in after.guild.roles or motorsports_role in after.guild.roles or movies_role in after.guild.roles or music_role in after.guild.roles or poetry_role in after.guild.roles or sustainability_role in after.guild.roles or travel_role in after.guild.roles):
       await after.remove_roles(onboarding_role)

keep_alive()

bot.run(my_secret)
  • changing after.guild.roles to after.roles --> no result
  • otherwise cluelessโ€ฆ :(

Solution

  • It seems to be an Intents problem.

    You are setting default intents: bot = discord.Client(intents=discord.Intents.default()), which doesn't include the members intent (needed so on_member_update() event get enabled).

    The change should be as simple as:

    intents = discord.Intents.default()
    intents.members = True
    bot = discord.Client(intents=intents)
    

    Also, with your current code (if onboarding_role in after.guild.roles and ...) you are checking if the role exists in the guild (Guild.roles), not if the member has the role (Member.roles). You should change after.guild.roles by after.roles in the if statement (same after the and)