【问题标题】:TypeError: role.setPermissions is not a function even though TypeScript says it isTypeError: role.setPermissions 不是一个函数,即使 TypeScript 说它是
【发布时间】:2021-11-06 04:13:27
【问题描述】:

我正在创建一个循环遍历服务器中所有角色的命令,并删除/添加选定的权限(如果可以的话)。将鼠标悬停在它上面会显示函数的描述。

async execute(client, msg, args, cooldowns, guildForPre, prefix) {
    if(msg.member.permissions.has("MANAGE_ROLES")) {
        let roles = (await msg.guild.roles.fetch()).cache;
        let highPos = getRoleByMention(args[1], msg.guild)?.position || msg.guild.roles.highest.position
        let lowPos = getRoleByMention(args[2], msg.guild)?.position || msg.guild.roles.everyone.position
        roles = roles.filter(r => r.permissions.has(args[0]))
        roles = roles.filter(r => r.position <= highPos && r.position >= lowPos)
        let replaced = ''
        for await (const [, role] of roles) {
            if(role.position >= msg.guild.me.roles.highest.position || role.position >= msg.member.roles.highest.position) continue;
            await role.setPermissions(role.permissions.remove(args[0]))
            (replaced.length) ? replaced += `, ${role.name}` : replaced += `${role.name}`
        }
        msg.channel.send("Replaced roles: " + replaced)
    }
}

getRoleByMention函数:

function getRoleByMention(mention, guild) {
    const id = mention.slice(3, mention.length-1)
    return guild.roles.cache.get(id)
}

我的命令处理程序参数输入正确,输入的命令如下:

m.removeRolePerm MANAGE_MESSAGES @Dyno @Dyno

是不是因为他们是同一个角色? docs 说这是一个有效的功能。我正在使用 v12。

【问题讨论】:

  • 您是否尝试打印/记录 role 对象以验证它是您认为的那样?
  • 它显示了角色对象(控制台说Role {roleinfo}
  • 记录role.setPermissions怎么样?
  • 显示函数([Function: setPermissions])
  • 听起来好像当你记录它时它就在那里,但它并不总是在那里(或者并不总是一个函数)。我将该行更改为const p = role.setPermissions(/*...*/); await p;,然后在其中的第一个上设置一个条件断点,使条件为typeof role.setPermissions !== "function"。查看是否到达断点。我的猜测是它会是,然后你就会知道(或者更接近于知道)为什么它有时不是一个函数。

标签: javascript discord.js roles


【解决方案1】:

问题实际上是后面的语句。这是因为缺少分号。添加分号应该可以正常工作。将其更改为 this 将消除错误并使其正常工作

await role.setPermissions(role.permissions.remove(args[0]))
           if (replaced.length) replaced += `, ${role.name}`
           else replaced += `${role.name}`

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2019-08-05
    • 2021-10-16
    • 2020-10-07
    • 2021-07-26
    • 2016-09-12
    相关资源
    最近更新 更多