Skip to content Skip to sidebar Skip to footer

Ban A User If They Leave The Server When They Have A Specific Role Discord.py

I want make it so if a user leaves the server while they have the Muted or [Banned] role they get permanently banned. This is the code that I tried: @bot.event async def on_member_

Solution 1:

member.roles returns a List of Role

You need to get the Role object which one way you can use is:

role = discord.utils.find(lambda r: r.name == '[Banned]', member.guild.roles)

on_member_remove takes in Member. You cannot have reason or Context(ctx)

@bot.eventasyncdefon_member_remove(member):
    role = discord.utils.find(lambda r: r.name == '[Banned]', member.guild.roles)
    guild = member.guild
    if role in member.roles:
        await guild.ban(discord.Object(id=member.id), reason="Leaved the server when soft banned")

Please also ensure you have the Members intent enabled. You can do this by going here then selecting Bot -> SERVER MEMBERS INTENT

You will need to do enable intents in your code by using:

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

bot = commands.Bot(command_prefix=".", intents=intents)

Solution 2:

Your code seems fine.

Check that you have enabled the Intents.members on your Discord Developper portal and in your bot code, if you're using Discord API v8 (as intents are mandatory in this API version), as explained here in Discord.py docs.

For your information, by default, all intents are enabled except members and presence intents.

On Discord Developer Portal, enable this in the "Bot" section of your app : enter image description here

Post a Comment for "Ban A User If They Leave The Server When They Have A Specific Role Discord.py"