Deleting a role (discord.py rewrite) - python

right now I'm trying to make a command to delete a role and I'm trying to go off of what I used to create a role
#client.command()
async def createrole(ctx):
guild = ctx.guild
await guild.create_role(name="role")
and I can't figure out how to.

In order to delete a role, you need to do a bit more work than with creating a role. You would need to use the delete function in the Role object. So if you wanted to create a command named "delete role", you would need to first get the role object before you delete it. You could do that with the following:
#client.command(name="delete_role", pass_context=True)
async def delete_role(ctx, role_name):
#find role object
role_object = discord.utils.get(ctx.message.guild.roles, name=role_name)
#delete role
await role_object.delete()

Related

Call Channel From Specific Guild

My bot is on multiple servers, and I´m trying to setup a member list for each server. The code is supposed to check for a role update (All servers use the same role as a member role) and then post a list of members in a specific channel. Right now the code works up until updatemsg() is called. I get an error saying the guild can't be an integer. How would I fix this error?
def updatemsg():
role = "OfficialMember"
guild = 754778020639932516
channel = guild.get_channel(754778311573635213)
channel.send("\n".join(str(member) for member in role.members))
#bot.event
async def on_member_update(before, after):
if len(before.roles) < len(after.roles):
if newRole.name == "OfficialMember":
print("MemList Updated")
updatemsg()
else:
print("Not OfficialMember Role")

How to add a role to a specific member when he joins a discord server

I'm new to python, so can someone help me with my code, because it is not working properly. When the user that I want joins it doesn't give it a role. I want to make it work for a specific user only to give a role when joining discord server.
#client.event
async def on_member_join(member):
member = get(member.id, id=member_id)
role = get(member.guild.roles, id=role_id)
await member.id(member)
await member.add_roles(role)
I don't even know why you're making this so complicated.
Since you already have the member as an "argument" you can work with it and don't have to define member again.
We can get the ID of member very easily with member.id. If we want to compare this with a real ID, we do the following:
if member.id = TheIDHere:
# Do what you want to do
The function for the role is correct, yet I cannot find a use for await member.id(member). What is the point of this/has it any use?
How you add the role at the end is also correct, but the code must be indented properly and best you work with an if / else statement, otherwise the bot will still give an error at the end in the console if always the wrong member joins.
The whole code:
#client.event
async def on_member_join(member):
role = discord.utils.get(member.guild.roles, id=IDOfTheRole) # Get the role from member.guild and the id
if member.id == TheIDHere: # If the member.id matches
await member.add_roles(role) # Add the role for the specific user
else: # If it does not match
return # Do nothing
You may also need to enable the members Intent, here are some good posts on that:
https://discordpy.readthedocs.io/en/stable/intents.html
How do I get the discord.py intents to work?

Discord.py text to assign role

I am already building a discord.py bot. However, I want to make it assign roles without the use of prefix in a specific channel - I mean in #roles channel. If a member writes "Mage", and if Mage role is already created, bot will assign the role to member. I looked for a reaction role but I need something more specific. I am waiting for your ideas and help. Have a great day!
#client.event
async def on_message(message): # Using on_message event reference you dont need to have a prefix
guild = message.author.guild
if message.content.lower() == "what the message name should be": # Checks if the WHOLE message is that one word, so not if that one word was part of a message
role = discord.utils.get(guild.roles, name="role name") # Gets the role
if role is not None: # makes sure role exists
await message.author.add_roles(role) # Assigns role to the message author

Discord bot fails to add a role to a user using discord.py

I simply want my bot to add a role to a user in discord. Although the syntax seems simply, apparently I'm doing something wrong.I'm new to python, so I'd appreciate some pointers in the right direction!
bot = commands.Bot(command_prefix='!')
def getdiscordid(discordname):
for guild in bot.guilds:
for member in guild.members:
if member.name == discordname:
return member.id
#bot.command(name='role')
async def role(ctx):
await ctx.message.channel.send("Testing roles")
discordid = getdiscordid("Waldstein")
print ("id: " , discordid)
member = bot.get_user(discordid)
print ("member: ", member)
role = get(ctx.message.guild.roles, name="Egg")
print("role: ", role.name)
await member.add_roles(role)
print("done")
# error handler
#bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.errors.CheckFailure):
await ctx.send(error)
bot.run(TOKEN)
In this example he successfully retrieves the member, he can't find the Egg role, and doesn't add the role. [Edit: I corrected the line to retrieve the role, that works but still no added role. Added the error handler]
The key issue is that add_roles() adds roles to a Member object not a user.
Made a couple of tweaks...
Changed the get id to get member and return the member object.
changed the name of the command to add_role() to avoid using role as the command and a variable.
changed to await member.add_roles(role)
Try:
def get_member(discordname):
for guild in bot.guilds:
for member in guild.members:
if member.name == discordname:
return member
#bot.command(name='add_role')
async def add_role(ctx):
await ctx.message.channel.send("Testing roles")
member = get_member("Waldstein")
print(f'member is {member} type {type(member)}')
role = get(ctx.guild.roles, name="Egg")
print("role: ", role.name)
await member.add_roles(role)
print("done")
For the answer's sake, I'm writing the whole discord.utils.get instead of just get. Here's your command rewritten:
import discord
#bot.command()
async def role(ctx):
await ctx.send("Testing roles!")
member = discord.utils.get(bot.get_all_members(), name="Waldstein")
# be careful when getting objects via their name, as if there are duplicates,
# then it might not return the one you expect
print(f"id: {member.id}")
print(f"member: {member}")
role = discord.utils.get(ctx.guild.roles, name="Egg") # you can do it by ID as well
print(f"role: {role.name}")
await member.add_roles(role) # adding to a member object, not a user
print("Done!")
If this doesn't work, try printing out something like so:
print(ctx.guild.roles)
and it should return each role that the bot can see. This way you can manually debug it.
One thing that might cause this issue is that if the bot doesn't have the necessary permissions, or if its role is below the role you're attempting to get i.e. Egg is in position 1 in the hierarchy, and the bot's highest role is in position 2.
References:
Guild.roles
Client.get_all_members()
Member.add_roles()
utils.get()
commands.Context - I noticed you were using some superfluous code, take a look at this to see all the attributes of ctx

Give and remove roles with a bot, Discord.py

How do I make a bot in Discord.py that will assign roles present in a role.json file, while using the same command to both remove and add the same role. For example, ?role <rolename> will both add and remove a role, depending on if the user has the role assigned. I'm a bit confused on how to achieve this.
My current bot uses ?roleadd <rolename> ?roleremove <rolename>.
I'm not sure where your role.json file comes into play, but here's how I would implement such a command
#bot.command(name="role")
async def _role(ctx, role: discord.Role):
if role in ctx.author.roles:
await ctx.author.remove_roles(role)
else:
await ctx.author.add_roles(role)
This uses the Role converter to automatically resolve the role object from its name, id, or mention.
This code basically just checks that if the command raiser is the owner of the server or not and then assigns the specified role to him.
#bot.command()
#commands.is_owner()
async def role(ctx, role:discord.Role):
"""Add a role to someone"""
user = ctx.message.mentions[0]
await user.add_roles(role)
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
Let me break the code down:
#bot.command()
#commands.is_owner()
These are just plain function decorators. They are pretty much self-explanatory. But still let me tell.
#bot.command() just defines that it is a command.
#commands.is_owner() checks that the person who has raised that command is the owner.
async def role(ctx, role:discord.Role): > This line defines the function.
user = ctx.message.mentions[0] #I don't know about this line.
await user.add_roles(role) #This line adds the roles to the user.
await ctx.send(f"{user.name} has been assigned the role:{role.name}")
#It just sends a kind of notification that the role has been assigned

Categories

Resources