How do you add roles in discord.py? - python

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

Related

Discord py Add role on command

Hey Im trying to make a command where If someone types ">verify" it adds the role called "Member" to them.
#client.command(pass_context=True)
async def verify(ctx):
member = ctx.message.author
role = get(member.guild.roles, name="Member")
await client.add_roles(member, role)
That is the code I currently have.
I have already looked here for answers but none have worked so far.
You're using the wrong function. Try using discord.Member.add_roles() instead.
#client.command(pass_context=True)
async def verify(ctx):
member = ctx.message.author
role = get(member.guild.roles, name="Member")
await member.add_roles(role)
I'm also guessing you explicitly imported get from discord.utils? Otherwise you would need to call the function via
role = discord.utils.get(member.guild.roles, name="Member")

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.

Discord.py: How to change role color?

So i created this role for my discord bot and i want to change his color to yellow.
I don't know why it won't change the color. This is my code, can anyone help me with it?
#bot.command(pass_context=True)
async def add_role(ctx):
member = ctx.author
role = discord.utils.get(member.guild.roles, name="Spike")
await member.add_roles(role)
await role.edit(colour=discord.colour(0xFFFF00))
print("done")
edit: It doesn't print the "done"
On the following line await role.edit(colour=discord.colour(0xFFFF00)), it looks like you missed the capitalisation of Colour in the discord class. This should be correct, unless the API already handles this.
await role.edit(colour=discord.Colour(0xFFFF00))
Can you try this:
#bot.command(pass_context=True)
async def add_role(ctx):
member = ctx.author
role = discord.utils.get(member.guild.roles, name="Spike")
await member.add_roles(role)
await role.edit(colour=0xFFFF00)
print("done")
Ok so, my problem was that my bot.command wasn't working because I was using bot and client method at the same time. I wasn't aware that they can't work togheter, so I decided to only use bot, and now everything is working.

Problems creating the mute command for a disk bot written in Python

#client.command()
#commands.has_permissions( administrator = True )
async def mute(ctx,member:discord.Member,time:int,reason):
muterole = discord.utils.get(ctx.guild.roles,id=726429210012549121)
emb = discord.Embed(title='ΠœΡƒΡ‚',color=0xff0000)
emb.add_field(name='ΠœΠΎΠ΄Π΅Ρ€Π°Ρ‚ΠΎΡ€',value=ctx.message.author.mention,inline=False)
emb.add_field(name='ΠΠ°Ρ€ΡƒΡˆΠΈΡ‚Π΅Π»ΡŒ',value=member.mention)
emb.add_field(name='ΠŸΡ€ΠΈΡ‡ΠΈΠ½Π°',value=reason,inline=False)
emb.add_field(name='ВрСмя',value=time,inline=False)
await member.add_roles(muterole)
await ctx.send(embed=emb)
await asyncio.sleep(time)
await member.remove_roles(muterole)
Hello! I created the mute command for my bot in the Python programming language The code is correct,but unfortunately after the last two lines, the bot does not issue the Muted role Please help!
I think you are trying to do this:
#client.command()
#commands.has_permissions( administrator = True )
async def mute(ctx,member:discord.Member,time,reason):
guild = ctx.guild
for role in guild.roles:
if role.name == "(Your Muted Role Name"):
"""""""""
Add role to member here
time sleep(int(time))
Remove role here
"""""
You have to check role name with if statement and then you have to do whatever you want.

discord.py | Making a mute command, can't get member name

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

Categories