I have tried and researched a lot of times but still could not find it. I want to make a command that deletes a selected role in the server. Here is what I came up with (Don't currently care about permissions):
#bot.command(pass_context=True)
async def delrole(ctx, role: discord.Role):
await bot.delete_role(role)
await bot.say("The role {} has been deleted!".format(role.name))
If you could help that would be awesome. I used role: discord.Role and delete_role(). Thank you for reading. If you have a solution, feel free to comment it.
NOTICE: This post was for the old version of discord.py and will no longer work. If you are looking for an equivalent solution for the rewrite (v1) version of discord.py, you can use the following code:
#bot.command(pass_context=True)
async def delrole(ctx, *, role_name):
role = discord.utils.get(ctx.message.guild.roles, name=role_name)
if role:
try:
await role.delete()
await ctx.send("The role {} has been deleted!".format(role.name))
except discord.Forbidden:
await ctx.send("Missing Permissions to delete this role!")
else:
await ctx.send("The role doesn't exist!")
You could do something like this to avoid being able to only delete roles that are mentionable
#bot.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 bot.delete_role(ctx.message.server, role)
await bot.say("The role {} has been deleted!".format(role.name))
except discord.Forbidden:
await bot.say("Missing Permissions to delete this role!")
else:
await bot.say("The role doesn't exist!")
where you do !delrole name_of_role and use discord.utils.get to find the role by its name from the list of roles on the server.
Then if it's found you can delete it with bot.delete_role which takes 2 arguments, the server you want to delete the role from and the role itself
All you're missing is the server argument to delete_role, (which it shouldn't need, as every Role knows what Server it is from)
#bot.command(pass_context=True)
async def delrole(ctx, role: discord.Role):
await bot.delete_role(role.server, role)
await bot.say("The role {} has been deleted!".format(role.name))
You were already on the right track using converters
The above solutions will not work server has to be guild and the actual role deletion can be a lot easier.
#bot.command(pass_context=True)
async def delrole(ctx, *, role_name):
role = discord.utils.get(ctx.message.guild.roles, name=f"{role_name}")
await role.delete()
await ctx.send(f"[{role_name}] Has been deleted!")
This solution is working as of 28/08/2021.
Related
My Goals:
When the user tried to use the command but mentioning a roles, it will sends an errors to the users.
My Current Codes:
#bot.command()
async def say(ctx, *, message: str):
try:
take_roles = ctx.guild
roles = [role.mention for role in take_roles.roles]
if message in roles:
await ctx.send(
f"{ctx.author.mention}, Hm? Trying to Abusing Me? Mention `#roles` is **NOT** Allowed on Say Command!"
)
else:
await ctx.message.delete()
await ctx.send(message)
You were trying to find the whole message in roles. A better idea is to look for role mentions inside of the message.
#bot.command()
async def say(ctx, *, message: str):
for role in ctx.guild.roles:
if role.mention in message:
await ctx.send(f"{ctx.author.mention}, Hm? Trying to Abusing Me? Mention `#roles` is **NOT** Allowed on Say Command!")
return # stops function if the role mention is in message
# if it didn't match any roles:
await ctx.message.delete()
await ctx.send(message)
So the entirety of this command works but I keep getting an error that says:
NotFound: 404 Not Found (error code: 10011): Unknown Role.
I have tried using so many things from the role id to discord.guild.get_role and I keep getting the same error. Any help is appreciated!
#bot.command(pass_context = True)
async def mute(ctx, member: discord.Member):
if ctx.message.author.guild_permissions.administrator:
role = discord.utils.get(ctx.guild.roles, name="Muted")
await member.add_roles(member, role)
embed=discord.Embed(title="User Muted!", description="**{0}** was muted by **{1}**!".format(member, ctx.message.author), color=0xff00f6)
await bot.say(embed=embed)
else:
embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
await bot.say(embed=embed)
Your code is partly outdated and the reason you have this error is coming from:
await member.add_roles(member, role)
Before v1.0, you needed to pass member in the method's argument but it's no longer necessary. So here, you're trying to add a discord.Role that is a discord.Member.
Other parts that are outdated:
pass_context=True is also no longer necessary, you can remove it.
bot.say doesn't exist, you can use ctx.send(embed=embed).
You can read more about the v1.0 migration here : Migrating to v1.0
await member.add_roles(role)
That is what you are looking for, as MrSpaar just told you.
Another suggestion I would like to make is doing the following.
Change this
async def mute(ctx, member: discord.Member):
To this:
async def mute(ctx, member: discord.Member = None, *, reason=None):
if not member:
await ctx.send("You must specify a user.")
return
if not reason:
await ctx.send("You must specify a reason.")
return
If you have any further questions let me know.
I start creating a Discord bot with the Python API of Discord. So, I tried to make a command that give roles on a discord server. Here is my code :
client = commands.Bot(command_prefix = '/')
#client.command()
async def addrole(ctx, role: discord.Role, user: discord.Member):
if ctx.author.guild_permissions.administrator:
await user.add_roles(role)
await ctx.send(f"Successfully given {role.mention} to {user.mention}.")
When I try to use the command, any error happear. I try to make like the websearch I've done but still don't work. Can someone help me ?
I don't believe basic things like these would require Discords intents, especially the "member intent". What I have done is I've cleaned up your code and required a member to be passed before a multiple string role, if needed.
#client.command()
#commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member = None, *, role: discord.Role = None):
if role == None:
await ctx.send(f'Provide a role to add')
return
if member == None:
await ctx.send(f'Provide a member to add a role')
return
await member.add_roles(role)
await ctx.send(f"Successfully added role, {role} to {member.name}")
I have been looking for this all day, and I can't seem to find a proper way of assigning roles to members. I tried a couple ways of assigning roles:
#client.command(pass_context=True)
async def claimrank(ctx, role: discord.Role):
user = ctx.message.author
await user.add_roles(role='Rookie')
and:
#client.command()
async def claimrank(member):
role = get(member.guild.roles, name="Rookie")
await member.add_roles(role)
What's worse, is that with both of these attempts, I don't get any errors, but the code doesn't do anything.
Please help!
Thanks in advance.
I got this from a post recently today. I'll write the code and look for the original post.
#client.command(pass_context=True)
async def addrole(ctx):
user = ctx.message.author
role = 'role' #change the role here
try:
await user.add_roles(discord.utils.get(user.guild.roles, name=role))
except Exception as e:
await ctx.send('Cannot assign role. Error: ' + str(e))
Found the original post by #Patrick Haugh: Give and remove roles with a bot, Discord.py
So I have been trying to make a verify command (.verify), where it needs an argument names and is supposed to make a role with their name and assign it to them and also is in supposed to be sent in a specific channel so the people aren't allowed to make roles themselves I thought it was the channel.id at first but I put print(ctx.message.channel.id) and it was the same so I knew that wasn't the error, but it doesn't make the role and doesn't give any role, and even no error! Please help me. Here is my code so far.
#client.command(pass_context=True)
async def verify(ctx, name):
print(ctx.message.channel.id)
if ctx.message.channel.id == 521645091098722305:
await client.create_role(author.server, name=name)
await client.say('Done! Welcome!')
If done thanks,
Sincerely,
Bread
Use strings instead of integers for ids in the async branch. You also need to capture the Role that create_role makes, and pass that to add_roles
#client.command(pass_context=True)
async def verify(ctx, name):
print(ctx.message.channel.id)
if ctx.message.channel.id == '521645091098722305:
role = await client.create_role(ctx.message.author.server, name=name)
await client.add_roles(ctx.message.author, role)
await client.say('Done! Welcome!')
The equivalent rewrite command would be
#client.command()
async def verify(ctx, name):
print(ctx.channel.id)
if ctx.channel.id == 521645091098722305:
role = await ctx.guild.create_role(name=name)
await ctx.message.author.add_roles(role)
await ctx.send('Done! Welcome!')