检查用户是否具有 x 角色之一,然后从他们那里删除该角色

Check if a user has one of x roles, then remove that role from them

我正在尝试构建一个功能,允许我检查我的 Discord 服务器中的所有成员,看看他们是否属于一组特定的角色,然后从他们中删除该角色。

        function StripRoles(){ //will get the players who still have roles assigned to them, and remove those roles
        const RolesArray = ['737531569165565982','737533396422754355','737533710798290974','737532883539066971','737533050925350932','737533864544567296','737534067188301854']; //here's the list of roles
        var i;
        for (i = 0; i < RolesArray.length; i++) { //loop that checks each of the roles in RolesArray
            message.guild.members.cache.forEach(member => { //for each member of the server
                if (member.roles.has(RolesArray[i])){ //if they have the role currently being checked
                    member.roles.remove(i).catch(console.error); //then remove it from them
                }
           })
        }
    }

当我运行它时,它returns错误:

TypeError: member.roles.has is not a function

如有任何帮助,我们将不胜感激!

编辑:为我自己的个人新手参考添加了更多标签

您可以使用 .includes:

if (member.roles.includes(RolesArray[i])){

编辑:为了帮助调试,您可以这样做吗:

for (i = 0; i < RolesArray.length; i++) { //loop that checks each of the roles in RolesArray
    message.guild.members.cache.forEach(member => { //for each member of the server
        console.log({
            roles: member.roles,
            type: typeof member.roles,
            array: Array.isArray(member.roles)
        });
    })
}

并告诉我它记录了什么?

好的,看着discords docs,我认为你只需要做:

if (member.roles.cache.has(RolesArray[i])){

如果 member.roles 是一个列表,请使用 includes 而不是 has