【问题标题】:Check if a user has one of x roles, then remove that role from them检查用户是否具有 x 角色之一,然后从他们中删除该角色
【发布时间】:2020-11-20 18:00:55
【问题描述】:

我正在尝试构建一个函数,允许我检查我的 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
                }
           })
        }
    }

当我运行它时,它会返回错误:

TypeError: member.roles.has is not a function

任何帮助将不胜感激!

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

【问题讨论】:

    标签: javascript discord roles


    【解决方案1】:

    你可以使用.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])){
    

    【讨论】:

    • 这只是将 TypeError 更改为 'member.roles.includes is not a function'。
    • 哈哈,那么你需要告诉我们member.roles 是什么,我们都假设它是一个数组
    • 请记住,我是一个完全不知道自己在做什么的新手……据我所知……这部分应该是检索成员在不和谐服务器上的角色列表.不知道这个列表是数组还是别的什么...
    • type 出现了'object',array 出现了 false...角色提出了一堆信息:
    • link_italic_粗体'代码'
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2020-06-02
      • 2019-05-28
      • 2010-12-22
      • 2019-06-16
      • 2021-06-04
      • 2021-02-24
      • 2018-06-10
      • 2021-10-10
      相关资源
      最近更新 更多