简体   繁体   English

从角色ID列表中检查命令作者是否具有特定角色

[英]Check if the command author has a specific role from a list of role IDs

Currently, I'm only able to check the command author for a specific role ID as a string variable, but I can't get it to check a list if any of the IDs in the list match an ID of a role assigned to them. 目前,我只能以字符串变量的形式检查命令作者的特定角色ID,但是如果列表中的任何ID与分配给他们的角色的ID相匹配,我都无法通过它来检查列表。 I've tried numerous things for a few days now, but It always either lets through every ID or none. 我已经尝试了很多天了几天,但是它总是让每个ID都通过或者没有。

This is the current code for checking a single variable: 这是检查单个变量的当前代码:

if (allowed_role_id in [roles.id for roles in ctx.message.author.roles]): 
    # TODO here
else:
    # Other TODO here

I want It to check from a list like this: 我希望它从这样的列表中进行检查:

listOfAllowedRoles = ['11111111111111111', '22222222222222222', '3333333333333333']

And If the command author has a role of which ID matches an ID from the list above to execute the TODO. 并且如果命令作者具有其ID与上面列表中的ID匹配的角色,则可以执行TODO。

You can use any() to check if a list contains at least one element of the other list: 您可以使用any()检查一个列表是否至少包含另一个列表中的一个元素:

# Whatever role ID
listOfAllowedRoles = [213183812831, 1238183283183813 ]

# Wont work if message is not in a guild
rolesUserHas = message.author.roles

# Or some other better way to get the list of IDs of roles the user has.
rolesIDuserHas = []
for role in rolesUserHas:
  rolesIDuserHas.append(role.id)

if any(item in listOfAllowedRoles for item in rolesIDuserHas):
  # User has at least one of the roles
else:
  #user do not have any of the roles.

Edit 编辑

Basically, listOfAllowedRoles are the list of roles which you... want to check against, aka the roles that the user needs (at least the IDs of it). 基本上, listOfAllowedRoles是您要检查的角色列表 ,也就是用户需要的角色(至少是其ID)。

Then first, we get the roles that the user currently has by doing message.author.roles , this returns a list of roles which we store it in rolesUserHas . 然后,首先,通过执行message.author.roles获得用户当前具有的角色,这将返回一个角色列表 ,并将其存储在rolesUserHas
However, we still cant compare both lists, since one of is a list of IDs (numbers), and the other is a list of roles (not numbers, aka you cant compare a number against a non-number) 但是,我们仍然无法比较两个列表,因为其中一个是ID(数字)列表,另一个是角色列表(不是数字,也就是您不能将数字与非数字进行比较)

So what I did was to "convert" the list of roles into a list of role-IDs that the user has, which is stored into rolesIDuserhas 因此,我要做的就是将角色列表“转换”为用户拥有的角色ID列表,并将其存储在rolesIDuserhas
(How I "converted" it was by simply creating a new list, and adding all the IDs of each role into the list) (我是如何“转换”的,只需创建一个新列表,然后将每个角色的所有ID添加到列表中)

So now we got a list of IDs ( listOfAllowedRoles , numbers) to compare against another list of IDs ( rolesIDuserhas , numbers). 所以,现在我们得到的标识(名单listOfAllowedRoles ,数字)来比较的ID(另一个列表rolesIDuserhas ,数字)。 But all we want is to check if the user has at least any of the roles in that first list 但是我们要做的就是检查用户是否至少具有第一个列表中的任何角色
The any() function in python does exactly what we want. python中的any()函数正是我们想要的。

Hence, any(item in listOfAllowedRoles for item in rolesIDuserHas) will return true if the user has at least one of the roles in the list of listOfAllowedRoles . 因此,如果用户具有listOfAllowedRoles列表中的至少一个角色,则any(item in listOfAllowedRoles for item in rolesIDuserHas)将返回true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM