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

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

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! :)

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 Can I get a user ID using just the user's name?

discord.py, Can I get a user ID using just the user's name?
I want to know the syntax
utils.find? MemberConverter?
discord.Guild.get_member_named?
If you have a guild object then you can
member = discord.utils.get(guild.members, name="name")
member_id = member.id

How to delete all roles of a list of users using membership. -MVC

I have a list of username's in a string array . l wanna give the all selected users a new role .I can easily give that using membership
But before doing that i want remove all roles assigned to selected users. How can i do that ..
or at least how can i remove all roles assigned to a particular user?
Roles.RemoveUserFromRoles(user.UserName, Roles.GetRolesForUser(user.UserName));
Try this for one user
Roles.RemoveUserFromRoles
or
Roles.RemoveUsersFromRoles
for many.
Here is a good tutorial about Roles, http://www.asp.net/security/tutorials/assigning-roles-to-users-cs
I know its old...but I was just looking for this myself:
MembershipUser mUser = Membership.GetUser((Guid)User.UserID);
foreach (var role in Roles.GetAllRoles())
{
Roles.RemoveUserFromRole(mUser.UserName, role);
}

Resources