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)
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
)