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.
Related
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.
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
I've been programming a bot with discord.py (the rewrite branch) and I want to add a ban command. The bot still doesn't ban the member, it just shows an error:
#client.command(aliases=['Ban'])
async def ban(ctx,member: discord.Member, days: int = 1):
if "548841535223889923" in (ctx.author.roles):
await client.ban(member, days)
await ctx.send("Banned".format(ctx.author))
else:
await ctx.send("You don't have permission to use this command.".format(ctx.author))
await ctx.send(ctx.author.roles)
It'll ban the pinged user and confirm that it did ban
Member.roles is a list of Role objects, not strings. You can use discord.utils.get to search through that list using the id (as an int).
from discord.utils import get
#client.command(aliases=['Ban'])
async def ban(ctx, member: discord.Member, days: int = 1):
if get(ctx.author.roles, id=548841535223889923):
await member.ban(delete_message_days=days)
await ctx.send("Banned {}".format(ctx.author))
else:
await ctx.send("{}, you don't have permission to use this command.".format(ctx.author))
await ctx.send(ctx.author.roles)
There also is no longer a Client.ban coroutine, and the additional arguments to Member.ban must be passed as keyword arguments.
async def ban(ctx, member: discord.Member=None, *, reason=None):
if reason:
await member.send(f'You got banned from {ctx.guild.name} by {ctx.author}, reason: ```{reason}```')
await member.ban()
await ctx.send(f'{member.mention} got banned by {ctx.author.mention} with reason: ```{reason}```')
if reason is None:
await ctx.send("please specify a reason")
I am setting up a mute command for my new discord bot, I am fairly new to the discord.py stuff, and do not understand what is going wrong.
I keep getting the error that a member is not being specified, when it clearly is.
I have tried many tutorials on youtube etc, but it always skims over a detail or two so I can't fully figure it out. I would appreciate it if someone could correct my code, because I am still learning discord.py.
#client.command()
async def mute(context, member: discord.Member=None):
if not member:
await client.say('Please specify a member')
return
role = get(member.server.roles, name="Muted")
await client.add_roles(member, role)
await client.say('{member.mention} was muted.')
It is just supposed to add the muted role to someone, and be done with it. I am having the same issue with specifying a member when using my ban and kick commands too, which are done in the same fashion.
I am open to all suggestions, thank you!
You need to change the decorator to #client.command(pass_context=True). The member name is being assigned to context, leaving member to take the default value.
#client.command(pass_context=True)
async def mute(context, member: discord.Member=None):
if not member:
await client.say('Please specify a member')
return
role = get(member.server.roles, name="Muted")
await client.add_roles(member, role)
await client.say(f'{member.mention} was muted.') # You forgot the f
Also, I would probably just let the conversion fail and then handle the error:
#client.command(pass_context=True)
async def mute(ctx, member: discord.Member):
role = get(member.server.roles, name="Muted")
await client.add_roles(member, role)
await client.say(f'{member.mention} was muted.')
#mute.error:
async def mute_error(error, ctx):
if isinstance(error, ConversionError):
await client.send_message(ctx.message.channel, 'Please specify a member')
else:
raise error
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')