Deleting a role in discord.py - python

So, I came up with this command similar to my createrole command. Everything works except the last line of code. I just don’t understand why it won’t delete. Every time I send the code, this error comes up: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: delete() got an unexpected keyword argument ‘name’. I am using the latest version of python and discord.py.
#client.command(aliases=['delrole'])
#commands.has_permissions(manage_roles=True)
async def deleterole(ctx, *,role, reason=None):
delrole = role
guild = ctx.guild
for role in guild.roles:
if role.name == delrole:
await ctx.send(delrole)
await role.delete(name=delrole)

Get rid of name=delrole and just leave it as role.delete(), This should delete the role! Add ctx.send(‘The role has been deleted’) to the end so you know when if the role was deleted. You can also deleted ctx.send(delrole) to make it more clean but, I left it.
#commands.has_permissions(manage_roles=True)
async def deleterole(ctx, *,role, reason=None):
delrole = role
guild = ctx.guild
for role in guild.roles:
if role.name == delrole:
await ctx.send(delrole)
await role.delete()
await ctx.send(f’The role {delrole} has been deleted!’)```

Related

How do I remove roles from a user?

I'm currently trying to add reaction roles to my bot, but I'm having trouble getting the bot to remove roles when the reactions are removed
#client.event
async def on_raw_reaction_add(payload):
print("Reaction Added")
if ((payload.message_id == 874372383736205343) and (payload.emoji.name == '🎓')):
guild = client.get_guild(payload.guild_id)
role = guild.get_role(874364790099836948)
await payload.member.add_roles(role)
#client.event
async def on_raw_reaction_remove(payload):
print("Reaction Removed")
if ((payload.message_id == 874372383736205343) and (payload.emoji.name == '🎓')):
guild = client.get_guild(payload.guild_id)
role = guild.get_role(874364790099836948)
await payload.remove_roles(role)
Adding roles works perfectly as intended but whenever I try removing a reaction I always get some kind of error. I've tried a few different things, but the code that I provided returns "Attribute Error: "RawReactionAddEvent" object has no attribute "remove_roles"
As I said, I've tried things other than remove_roles but none of them worked.
I'm pretty new to discord.py and python as a whole so this is probably a really obvious mistake but I just can't seem to find anything that works.
You'll need to retrieve a member object in order to remove the roles. You're currently trying to remove a role from payload instead of a valid member.
guild = client.get_guild(payload.guild_id)
member = await guild.fetch_member(payload.user_id)
role = guild.get_role(874364790099836948)
await member.remove_roles(role)
As Lukasz points out, you can't use payload.member when a reaction is removed because it is only available when a reaction is added. Docs.

Discord.py guild invite bot

If the code is correct, I want to make a bot where the bot invites me to a specific server.
but it has error.
Here's my code:
#client.command()
async def invite(ctx, *, code):
if code == "12320001":
guild = "851841115896152084"
invite = await ctx.guild.create_invite(max_uses=1)
await ctx.send(f"Here's your invite : {invite}")
else:
await ctx.send(f"Code is wrong!")
and error:
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'create_invite'
ctx.guild is None, that is why you are getting this Exception.
You can check the value of ctx.guild before passing ctx as a parameter while calling invite function.
As the error suggests, ctx.guild is None. This usually occurs when invoking a command in a DM rather than in a server, as there obviously is no server in DMs.
Judging by the fact that you have a guild variable that you never used I assume that you're trying to invite people to that guild instead.
# Make sure the id is an int, NOT a string
guild = client.get_guild(85184111589615208)
# Invite users to the guild with the above id instead
invite = await guild.create_invite(max_uses=1)
You need a Channel object to create an invite as the Guild class doesn't have a create_invite() method. You can the code given below. Note that the server should have at least one channel.
#client.command()
async def invite(ctx, code):
if code == "12320001":
guild = client.get_guild(851841115896152084)
invite = await guild.channels[0].create_invite(max_uses = 1)
await ctx.send(f"Here's your invite: {invite}")
else:
await ctx.send("Invalid code!")

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

How to delete a role with discord.py?

Using the following lines of code:
#bot.command(pass_context=True)
async def ban_role(ctx, *,role_name):
role = discord.utils.get(ctx.message.server.roles, name=role_name)
if role:
try:
await ctx.delete_role(ctx.message.server, role)
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!")
I get the error:
You're using outdated syntax from v0.16 of discord.py that isn't supported anymore.
See the migration guide for v1, specifically how Server is now Guild.
You can also just get the guild directly from the Context object's guild attribute, instead of going through its message attribute.
Aditionally, unless you've overridden your Context, it's not going to have a delete_role method.
You'll want to use the Role.delete method instead.
For help with markdown, see https://stackoverflow.com/editing-help.

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

Categories