Add roles command not working (discord.py rewrite) - python

So I tried to make an add role command for my bot:
#bot.command()
async def addrole(ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)
await ctx.send(f'{member} now has the {role} role')
but it did not work, the bot didn't add the role or send the message, no errors either, is there something I did wrong?

Sorry for being late on this but the answer is simple. Your code is fine just when typing the command use
(prefix) #user mention #role
Ex. !addrole #huss_a #example
I would also recommended putting #commands.has_permissions reference at the beginning so only admins can use this and users can't give themselves any role they want like so:
#bot.command()
#commands.has_permissions(manage_roles=True)
async def addrole(ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)
await ctx.send(f"{member} now had the {role} role")
You can also use this code:
#bot.command()
#commands.has_permissions(manage_roles=True)
async def addrole(ctx, role: discord.Role, user: discord.Member):
await user.add_roles(role)
await ctx.send(f"{role.mention} was added to {user.mention})

Related

Im trying to make it where the bot will tell you to specify a member to mute if they dont specify it. How do i do that? I tried to but it doesnt work

Mute code below. If a mod doesnt specify what member to mute, how can the bot tell them that? Thanks!
#bot.command()
#commands.has_permissions(manage_roles=True)
async def mute(ctx, member: discord.Member, reason=None):
if member.id == ctx.author.id:
await ctx.send(f"{ctx.author.mention}, YOU HAVE BEEN STOPPED BY ME YAY, you can't mute yourself!")
return
if discord.Member == None:
await ctx.send(f"{ctx.author.mention}, HEY you nugget, you need to specify who you want to mute!")
return
role = discord.utils.get(ctx.guild.roles, name='muted')
if role in ctx.guild.roles:
await member.add_roles(role)
embed=discord.Embed(title="Muted", description=f"Offender: {member.mention} has been muted until further notice!", color=0x00FFFF)
embed.add_field(name="Reason", value=f'{reason}', inline=True)
embed.add_field(name="Moderator", value=f'{ctx.author.name}', inline=True)
await ctx.send(embed=embed)
else:
await ctx.send(f"{ctx.author.mention}, Make sure your muted role is called `muted` in all lowercase! Then try again.")
Your thought was right, but you need to approach it a little differently.
Take a look at the following code:
#bot.command()
#commands.has_permissions(manage_roles=True)
async def mute(ctx, member: discord.Member, reason=None):
if member.id == ctx.message.author.id:
await ctx.send("You can't mute yourself")
if member:
# Do what you want if the member was given
What we only did is work with if statements.
If you want you can also use return await so the bot will no longer proceed.
You can also build in an error handler for different things. Have a look at the following code:
#mute.error
async def mute_error(ctx, error):
if isinstance(error, commands.MissingRequiredArgument):
await ctx.send("No member given")
You have to put the code under the actual mute command code.

How could i get the name of a role in discord.py

I created an add role command. It works fine but my problem is that it works only with the id of the role. I was wondering how could I somehow convert the id of the role to the name of the role.
This is the code.
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, member: discord.Member,rolename):
if member is None:
member=ctx.author
role = discord.utils.get(ctx.guild.roles, id=rolename)
await member.add_roles(role)
await ctx.send(f"{member.mention}was added {rolename}")
Any help is appreciated.edit
solution
I made use of the 2 comments and created this command which works perfectly.
#commands.command()
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, role: discord.Role, member: discord.Member=None):
if member is None:
member=ctx.author
await member.add_roles(role)
await ctx.send(f"{member.mention} was added {role}")```
You could declare your rolename variable as a discord.Role and make use of the implicit role converter that will be applied, just like what happens to your member object. Your function would look something like this:
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, member: discord.Member, role: discord.Role):
await member.add_roles(role)
await ctx.send(f"{member.mention} was added {rolename}")
You can use role converter just like you used member converter. You can pass role: discord.Role in arguments. Then, you just have to mention the role.
Also, you have to assign a default value None to the member argument in order to check if it's None.
#commands.has_permissions(manage_messages=True)
async def addrole(self, ctx, role: discord.Role, member: discord.Member=None):
if not member:
member=ctx.author
await member.add_roles(role)
await ctx.send(f"{member.mention}was added {role.name}")
References
RoleConverter
discord.Role.name

How to add a role with a Discord Bot

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

yes/no confirmation after command in disord.py

I'm making a discord bot in python using the discord.py library. I need to know, how I can implement a yes/no confirmation after a command. For example, consider a 'ban' command that bans a user from the server, and can only be used by a moderator. After writing "!ban #user", I want the bot to reply "Ban #user?(y/n)" and if that very moderator replies with anything but a "y", the ban gets cancelled. How can I implement this? Something like this:
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
await ctx.send("Ban #member?(y/n)")
if get_confirmation():
await member.ban(reason = reason)
else:
await ctx.send("Ban Cancelled")
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, reason = None):
await ctx.send(f"Ban {member.mention}?(yes/no)")
#client.command()
#commands.is_owner()
async def yes(ctx, member : discord.member, reson=None):
await member.ban(reason = reason)
#client.command()
#commands.is_owner()
async def no(ctx):
await ctx.send("Ban Cancelled")
You need to use wait_for.
Below is the revised code:
#client.command()
#commands.is_owner()
async def ban(ctx, member : discord.Member, *, reason = None):
await ctx.send(f"Ban {member.mention}?(y/n)")
msg = await bot.wait_for("message", check=lambda m:m.author==ctx.author and m.channel.id==ctx.channel.id)
if msg.content.lower in ("y", "yes"):
await member.ban(reason = reason)
else:
await ctx.send("Ban Cancelled")
Okay after some digging i found this post. I hope it helps anyone with the same issue

How to add a role to multiple users with discord.py?

I have this command to give a role to a user ,
#commands.command(pass_context=True)
#commands.has_permissions(manage_roles=True)
async def role(self,ctx, user: discord.Member, role: discord.Role):
await user.add_roles(role)
await ctx.send(f"hey {ctx.author.name}, {role.name} Role have been given to {user.mention}")
await ctx.message.add_reaction(emoji="<a:tick:748476262640779276>")
what I is a command like role #role #user1 #user2 #user3.... to give a role to multiple users with a single command.
How do I do it?
You can do it as following
#commands.command(pass_context=True)
#commands.has_permissions(manage_roles=True)
async def role(self, ctx, role: discord.Role, *users: discord.Member):
for user in users:
await user.add_roles(role)
{prefix}role #role #user1 #user2

Categories

Resources