How do I check if member have role - bots

How do I check if the message.author has admin role?
Or is there a way I can check if a member have a higher role than another?

You can use discord.utils.get to get a role if it exists in the list of a Members roles.
from discord.utils import get
if get(member.roles, name="admin"):
# has role
else:
# does not have role
To determine position in the hierarchy, you can compare the highest roles of the Members
if member1.top_role > member2.top_role:
# member 1 is higher than member 2

To get role(s) of user
You can either comparing with the role's name
message.member.roles is a collection. It mean you just look for the role directly in the collection. Like this:
if(message.member.roles.find(r => r.name === "Admin") || message.member.roles.find(r => rname === "AnotherRole"))
Or you can check directly if a member have certain role with the Map.has method. This will check for keys, not values, so you'll have to use the roles' ids instead.
message.member.roles.has(adminRole.id)
message.member.roles.has(modRole.id)
To compare a role to another
You can simply use this kind of code (has described in the official documentation)
if(role_of_author > role_of_user_to_be_banned)
But take care that one user can have multiple roles, so adapt this condition based on your use case.
You can refer to the documentation for more information.

Related

Discord.js - Command to Remove All Users Containing Role

I know that you can remove certain roles from users, and remove all roles from a user, but I was thinking of doing the reverse. I looked at this guide, which provides a way to retrieve all of the people who have a specific role. It seems like you could manipulate the collection/map to go through each member and remove the role. However, I cannot seem to achieve this.
I've hard-coded the one specific role that I am targeting as well as the message that should trigger the command.
Current code that I've been trying out (only seems to be working if there's just one person assigned the role):
if (msg.startsWith('!new round')) {
//check for everyone and remove the role
//roleID is just the roleID number string; I've stated it outside the if loop, for other command use cases as well
let membersWithRole = message.guild.roles.cache.get(roleID).members;
console.log(membersWithRole);
for (let member of membersWithRole) {
let mem = member[1]
mem.roles.remove(role).catch(console.error);
message.reply("Everyone with the jail role is now back in the game!");
}
}
Bottom line: Given a collection of the list of "guild" members that have the specified role (provided in the guide), could I iterate through a list* in order to remove the role from each member?
*I haven't found said list containing the members, but it's probably the objects themselves, so the whole collection
you need to learn documentation of discord.js
and yes you can do it by looping through all members.
if(msg.startsWith('!new round')){
console.log('command used by '+msg.author);
let role =msg.guild.roles.cache.get(roleId);
role.members.each(member=>{
member.roles.remove(role);
});
console.log('removed role from all members');
}
and also if you want to remove role from all members, so why you are not just deleting the role?
delete role:
msg.guild.roles.cache.get(roleId).delete();

Get highest role of a member with mentions

How can I get the highest role name of a mentioned member? I tried something like this but it doesn't work. Thanks! :) btw this is a ban command and I need this bc my bot is crashing when someone is trying to ban a user with a higher rank than bot.
if(message.member.hasPermission('BAN_MEMBERS')){
const user = message.mentions.users.first()
console.log(user.roles.highest.name)
if(!user) return console.log("test1")
if(!args[2]) return console.log("test2")
const ddays = args[1]
What you could do is:
Getting a user's highest role:
Get the UserId from the Mention inside of the Message
Get the Cache from the RoleManager of the Guild in which the Message was sent in
(I don't know if the roles in RoleCache are sorted by position, so sort if needed)
Iterate through the roles in RoleCache and check if the UserId is contained inside a specific role
Get the position of the role
Getting the bot's highest role:
Repeat steps 2-5 for your bot (or integrate them within the previous iteration of the RoleCache)
Then compare both numbers and find out if the bot's "role number" is higher than the one of the user's.

How to get the bot to recognize when a role was mentioned, and then respond back?

I would like the bot to recognize when the #Community Manager, #Admin, #Moderator roles are tagged, either single, or multiple roles tagged in same message, then send a message to the channel mentioning the user name.
I can get the bot to recognize when it's been tagged using this code:
if client.user.mentioned_in(message) and message.mention_everyone is False:
await message.delete()
I cannot for the life of me figure out how to see if other roles were tagged.
I've tried
if message.role_mentions.name('Admin'):
#do stuff
but get this error:
AttributeError: 'list' object has no attribute 'name'
message.role_mentions gives back a list of Role.
And then you can fetch roles from the guild using message.guild.get_role(id) in order to compare against the list of roles you gotten from the message.
Should result in something along the lines of this:
# Create a list of roles to check against
rolesToCheckAgainst [
# Fetches the role from the guild which the message was sent from
message.guild.get_role("The ID of the role"),
message.guild.get_role("The ID of the second role")
#etc...
]
# 'rolesWerePinged' will be true if any of the roles were pinged
rolesWerePinged = any(item in rolesToCheckAgainst for item in message.role_mentions)
if rolesWerePinged:
# Do something, some of the roles was pinged.
Also, I used any() to check if any of the roles mentioned contained any of the roles that needed to be check against. You can use a double-loop instead if you need different actions to be done depending on the type of roles mentioned.

editing roles with edit_role discord.py

I have the following line in my script:
await client.edit_role(server='547874634978789398', role='' ,colour=0x008000)
However I do not understand what parameter discord.py is expecting for role=. Can anyone point me in the right direction to better understand this parameter?
First, you should use Server class not server's id. There is get_server() function that returns Server object with the given server's id.
server = client.get_server('547874634978789398')
Then, you can access to all roles belonging to the server by server.roles. It is list of Role object. So if you have a name of role and want to edit one's role into that, try it.
for role in server.roles:
if role.name == 'role_name':
# What you want to do.
await client.edit_role(server=server, role=role, colour=0x0080000)
break
Also there is server.role_hierarchy property which returns roles in order of the hierarchy. It contains same elements with server.roles but it is the sorted version.
role = discord.utils.get(ctx.guild.roles, name="Name")
# This will get the role you want to edit
await role.edit(color=0x008000, reason="The reason")
# This will edit the role with the desired color
For more information on this please see the docs: discord.Role.edit | discord.utils.get

DB2 how to get user privilege information when privilege is granted to its group

I grant schema CREATEIN privilege for schema 'test' to user group 'test-group', then add a user 'test-user' into this 'test-group' in Windows OS.
I would like to know what DB2 function or SQL statement can be used to retrieve privilege information for user 'test-user'. I am aware of that the user-group relationship is not defined in DB2, but there must be some ways to look up such relationship data.
As an example, I can create a table successfully in schema 'test' after log in database by user 'test-user' which means the DB2 engine can get 'test-user' CREATEIN privilege inherited from 'test-group'.
I tried to check syscat.schemaauth view by SQL statment
select * from syscat.schemaauth but cannot find user 'test-user' privilege definition only group 'test-group' privilege definition:
GRANTOR GRANTORTYPE GRANTEE GRANTEETYPE SCHEMANAME ALTERINAUTH CREATEAUTH DROPINAUTH
... ....
SYSIBM S PUBLIC G ADMINISTRATOR N Y N
ADMINISTRATOR U TEST G TEST N Y Y
.. ...
You could use the table function AUTH_LIST_GROUPS_FOR_AUTHID to look up the groups for "test-user". That would list ALL groups the user belongs to, including OS groups not used for the database.
SELECT * FROM TABLE (SYSPROC.AUTH_LIST_GROUPS_FOR_AUTHID('TEST-USER')) AS T
There is another view, SYSIBMADM.AUTHORIZATIONIDS, which lists all authorization IDs, i.e., groups, users and roles:
SELECT * FROM SYSIBMADM.AUTHORIZATIONIDS
The last view you need is SYSIBMADM.PRIVILEGES which lists the privileges. Depending on what you need you would combine the three views/table functions
look up the user's groups for groups known to the database
list the privileges for the user AND
list the privileges for all the groups the user belongs to
UPDATE:
I got interested and quickly typed up and tested a query. This should answer it directly. The "or a.authid='PUBLIC'" is needed to include those privileges coming from PUBLIC.
SELECT distinct p.AUTHID, p.PRIVILEGE, p.OBJECTNAME, p.OBJECTSCHEMA, p.OBJECTTYPE
FROM SYSIBMADM.PRIVILEGES P, SYSIBMADM.AUTHORIZATIONIDS A,
TABLE (SYSPROC.AUTH_LIST_GROUPS_FOR_AUTHID('userID')) as U
WHERE p.privilege='CREATEIN' and a.authidtype='G' and a.authid=p.authid
AND (u.group=a.authid or a.authid='PUBLIC')

Resources