Adding roles when level up - discord.py

I wanted to add a role every time a user levels up.
But, I am getting an error: TypeError: add_roles() got an unexpected keyword argument 'role'. This is the code i have right now:
if users[f'{user.id}']['level'] == 2:
role_id = 833924720617062430
await user.add_roles(user, role = role_id)

member.add_roles(), takes just the roles as the parameter, not role ids or member instances.
# getting the role
role = user.guild.get_role(role_id)
if role is None:
print('invalid role id')
await user.add_roles(role)
Note: this only works if user is a discord.Member instance
References:
get_role
add_roles

Related

How do I add multiple roles form a list to a user?

I have a command that adds all the current roles of a user to a Database (MongoDB).
The code:
def add_roles_to_db(self):
check = cursor.find_one({"_id": self.ctx.author.id})
if check is None:
cursor.insert_one({"_id": self.ctx.author.id, "roles": [str(r) for r in self.ctx.author.roles[1:]]})
else:
cursor.update_one({"_id": self.ctx.author.id}, {"$set": {"roles": [str(r) for r in self.ctx.author.roles[1:]]}})
The code to get the roles:
def get_roles_from_db(self):
return cursor.find_one({"_id": self.ctx.author.id})["roles"]
When I get the roles from the DB I get a list, everything I've tried led to an error. Error: "AttributeError: 'str' object has no attribute 'id'"
if len(roles) != 0:
await author.add_roles(*roles)
I saw a other post where someone added roles via a list but that didn't work
You're passing a list of strings, not a list of Roles. Turn them into discord.Role instances using the id's first, and then pass them to add_roles.
You can get them using Guild.get_role, or Guild.roles.
await author.add_roles(*[discord.Object(role_id) for role_id in roles])
One good way of adding multiple roles to a user is to have the list of role IDs into a list. You would have to look at your code and figure out how to do that bit, as I don't know either but I reckon just append it into a list. Then interate through each item in that list (each ID) and append it.
Example code:
guild = client.get_guild(1234) #replace 1234 with your guild ID
guild = ctx.guild #this is another way of doing it, chose the one above or this
role_ids = [] #your role ids would be in this list
for id in role_ids:
role = guild.get_role(id)
await author.add_roles(role)
await ctx.send("Given you all the roles!")
I haven't tried this myself, but I don't see why it wouldn't work.
If you need any more clarification, please ask me and if this has worked, please mark it as correct! :)

i want auto role in jda, (when member join, add role by id)

i want when user join some discord guild, bot will add role by id to newcomer.
i have already use this code block, but that isn't work!
`
if (((GuildMemberJoinEvent) event).getGuild().getName().equals("something name")){ System.out.println(((GuildMemberJoinEvent) event).getMember().getUser().getName() +((GuildMemberJoinEvent) event).getGuild()); Role r = ((GuildMemberJoinEvent) event).getGuild().getRoleById("1019811149602107402"); ((GuildMemberJoinEvent) event).getMember().getGuild().addRoleToMember(((GuildMemberJoinEvent) event).getMember().getUser(),r); }
`
This is how to grant roles by id:
event.getGuild().addRoleToMember(member, event.getGuild().getRoleById("1034143551568752682")).queue();
Just put this in onGuildMemberJoin and check for guild name like you have here.
Also please make your code readable

how to call data from database to view by user_role laratrust

I use laratrust as my user role, in the user role there are 1 = superadministrator, 2 = administrator, 3 = perusahaan, then I want to display the names of existing perusahaan in my view, which means calling user = role id 3, then how to call it? I also don't really understand eloquent relationships
You need to get users in this way
// This will return the users with 'superadministrator' role.
$users = User::whereRoleIs('superadministrator')->get();
check the documentation

Im trying to find if a member in the server has a specific role and then remove the role from him but i dont know how, here's the code:

#client.command()
async def find(ctx):
guild = ctx.message.guild
role = discord.utils.get(ctx.guild.roles,name="Palpatin")
for member in guild.members:
if role in member.roles :
await ctx.send(f'{member}')
await member.remove_roles(role)
When I try to use the code there are no errors but the bot doesn't send what member has the role either removes the role from that member
As #Weylyn said, your variable role might be None if there is no role named exactly Palpatin, if that is the case, your if will always be evaluated to False since None in any list will always be False, this might be why you do not get any message nor any error.
Also, your for might not work if you do not have the member intents enabled (check here https://discordpy.readthedocs.io/en/stable/intents.html )

discord:py highest role among a list of specific roles

How do I perform the highest role of a user among some roles?
For example I have this category of roles:
And I wish that when I go to see a user's information, the highest role among them comes out?
You can reverse Member.roles and find the first match:
#bot.command()
async def my_highest_role(ctx):
highest = discord.utils.find(lambda role: role in my_roles, reversed(ctx.author.roles))
if highest:
await ctx.send(highest.name)
else:
await ctx.send("No such role")

Resources