How do I remove all discord roles? - python

I have several servers where I would like to delete all the roles that I previously created, but their number leaves much to be desired .. I created a command that removes one role. Please help create a command that will remove all roles from the server discord
#client.command(pass_context=True)
async def delrole(ctx, *,role_name):
role = discord.utils.get(ctx.message.server.roles, name=role_name)
if role:
try:
await client.delete_role(ctx.message.server, role)
await client.say("The role {} has been deleted!".format(role.name))
except discord.Forbidden:
await client.say("Missing Permissions to delete this role!")
else:
await client.say("The role doesn't exist!")

As Lukas Thaler said you could just cycle through all of the roles in the server, but if you want to just remove a few roles, you can try this.
#client.command()
async def delrole(ctx, *, roles: discord.Role):
for role in roles:
try:
await client.delete_role(ctx.message.guild, role)
except discord.Forbidden:
await client.say("Missing Permissions to delete this role!")
The format to use this command would be: !delrole #role1 #role2 #role3, etc. With however many roles you want.

Related

Removing every role from a user discord.py

I wanna do a ban appeal system on my server. I wanna remove every single role of someone and give them the [Banned] role. I cannot just do something like I did for the member role for all of them since there are a lot of roles, even some custom ones that are made and deleted every day.
member_role = get(user.guild.roles, name="γ€Žβœ…γ€Β· π™ΌπšŽπš–πš‹πš›πšž")
await user.remove_roles(member_role, reason=None, atomic=True)
I tried this: discord.py trying to remove all roles from a user but it didn't work. Also tried this:
for role in user.roles:
if role.name == '[Banned]':
pass
else:
await user.remove_roles(role)
but couldn't get it to work. (I have no experience in python or discord.py)
So. How can I remove every role from a user instead of only the member_role ?
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = get(user.guild.roles, name="[Banned]")
await user.add_roles(banned_role, reason=None, atomic=True)
member_role = get(user.guild.roles, name="γ€Žβœ…γ€Β· π™ΌπšŽπš–πš‹πš›πšž")
await user.remove_roles(member_role, reason=None, atomic=True)
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ", colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
You can't get the list of roles that way.
To remove all the roles of the user you have to apply an edit to it and provide an empty list of roles.
It also changes the order of your code as you first have to remove and then re-assign a role to the user.
Have a look at the following code:
#bot.command()
#commands.has_permissions(ban_members=True)
async def ban(ctx, user: discord.Member, *, reason=None):
await asyncio.sleep(1)
banned_role = discord.utils.get(user.guild.roles, name="[Banned]")
await user.edit(roles=[]) # Remove all roles
await user.add_roles(banned_role, reason=None) # Assign the new role
banemb = discord.Embed(title="Ban", description=f"{user.mention} a fost banat/a. ",
colour=discord.Colour.dark_red())
banemb.add_field(name="Motiv:", value=reason, inline=False)
await ctx.send(embed=banemb)
I have changed/removed a few things from the code. Of course you have to add them again, according to your wishes.

How to whitelist roles from being banned

I asked this question before, but due to my next to zero experience with stack-overflow it messed it up. So i will rewrite the question with the anwser so people looking for this can have it right away.
So my question was, how can i whitelist role from being banned. Now later, with more experience with discord.py i can tell you the anwser listed down below is the way to do it.
The working code: Credit to CaptAngryEyes
#bot.command()
async def ban(ctx, member : discord.Member):
whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
for role in member.roles:
if role.id in whitelisted_roles:
await ctx.send("You can't ban this user! He is a moderator!")
return
else:
# Ban code goes here...
pass
You can iterate through the roles of the user and check if he has a role in a list. I use the role IDs since it can't be changed.
#bot.command()
async def ban(ctx, member : discord.Member):
whitelisted_roles = [728345678923456, 923478569378456, 8923475627893456] # Put the role IDs here
for role in member.roles:
if role.id in whitelisted_roles:
await ctx.send("You can't ban this user! He is a moderator!")
return
else:
# Ban code goes here...
pass
If he has a "whitelisted" role we just return nothing and the command ends.

discord.py check if a user has a particular role

I'm trying to get this to check if the user has the role that's tagged or not.
discord.utils.get(ctx.guild.roles, name=rolename)
and
discord.utils.get(user.roles, name=rolename)
#bot.command()
#commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member, rolename: discord.Role):
role = discord.utils.get(ctx.guild.roles, name=rolename)
if(not role in user.roles):
await user.add_roles(rolename)
embed=discord.Embed(title=f"{user.name} Has been added to a role called: {rolename.name}", color=discord.Color.dark_purple())
await ctx.send(embed=embed)
else:
await ctx.send(f"Hey {ctx.author.name}, {user.name} already has the role called: {rolename.name}")
No errors just keeps giving the role instead of checking if the user has the role or not.
#bot.command()
#commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
if rolename in user.roles:
await ctx.send("Person already has role")
else:
await user.add_roles(rolename)
await ctx.send("Person doesn't have the role and it has been given to him/her")
Make sure the bot's role is higher than the role you're trying to give the user though because if It's not then you'll get error 50013 Missing Permissions.
This is the fix
#commands.has_permissions(manage_roles=True)
async def giverole(ctx, user: discord.Member=None, rolename:discord.Role=None):
if rolename not in user.roles:
await user.add_roles(rolename)
await ctx.send("Person doesn't have the role and it has been given to him/her")
else:
await ctx.send("Person already has role")```

How do you add roles in discord.py?

I am trying to add a role to someone but when I do the
client.add_roles(member, role)
I have tried everything I could think of with the code it just gives me that message, I have looked at several other questions around looking for the answer but everyone says to do that command that I have tried but it wont work.
#client.command(pass_context=True)
#commands.has_role('Unverified')
async def verify(ctx, nickname):
gmember = ctx.message.author #This is the looking for the memeber
role = discord.utils.get(gmember.server.roles, name='Guild Member')
channel = client.get_channel(401160140864094209)
await gmember.edit(nick=f"{nickname}")
await ctx.channel.purge(limit=1)
r = requests.get("This is the link to the API but it shows my key and everything so not going to put it here but it works in other commands")
#if nickname in role:
# await ctx.send(f"You have already verified your account.")
if nickname.encode() in r.content:
await channel.send(f'#here ``{nickname}`` is in the guild.')
await client.add_roles(gmember, role)
else:
await gmember.kick()
Instance of Bot has no add_roles member pylint (no-member)
await gmember.add_roles(role)
You can read more here

How do you remove a role from a user?

I'm trying to remove a role from a user with a command but I'm not quite sure how the command works.
Here is the code:
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
await bot.remove_roles(user, 'member')
await bot.say("{} has been muted from chat".format(user.name))
It looks like remove_roles needs a Role object, not just the name of the role. We can use discord.utils.get to get the role
from discord.utils import get
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = get(ctx.message.server.roles, name='member')
await bot.remove_roles(user, role)
await bot.say("{} has been muted from chat".format(user.name))
I don't know what happens if you try to remove a role that the target member doesn't have, so you might want to throw in some checks. This might also fail if you try to remove roles from an server admin, so you might want to check for that too.
To remove a role, you'll need to use the remove_roles() method. It works like this:
member.remove_roles(role)
The "role" is a role object, not the (role's) name, so it should be:
role_get = get(member.guild.roles, id=role_id)
await member.remove_roles(role_get)
With your code:
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role_get = get(member.guild.roles, id=role_id) #Returns a role object.
await member.remove_roles(role_get) #Remove the role (object) from the user.
await bot.say("{} has been muted from chat".format(user.name))
The remove_roles() function takes two parameters, the first of which is the user from which the role and the role itself are removed. It is possible not to write user: discord.Member in the function parameters, leave only ctx. You also need the role you want to delete. This can be done by:
role = get (ctx.author.guild.roles, name = "the name of the role you want to remove")
#also include 'from discord.utils import get'
And then to remove the obtained role (object), call the function:
await ctx.author.remove_roles (role)
So, the final code should look something like this:
from discord.utils import get
#bot.command(pass_context=True)
#commands.has_role('staff')
async def mute(ctx):
role = get (ctx.author.guild.roles, name = "the name of the role you want to remove")
await ctx.author..remove_roles(role)
await ctx.send("{} has been muted from chat".format(user.name))
Try that:
import discord
from discord.ext import commands
#bot.command()
#commands.has_role('staff')
async def mute(ctx, user: discord.Member):
role = discord.utils.get(ctx.guild, name='member')
await user.remove_roles(role)
await ctx.send(f'{user.name} has been muted from chat')

Categories