Search code examples
javascriptdiscord.jsroles

TypeError: role.setPermissions is not a function even though TypeScript says it is


I was creating a command that loops through all roles in a server, and removes/adds a selected permission (if it can). Hovering over it shows the description of the function.

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:

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

My command handler arguments are put in correctly, and the command entered is the following:

m.removeRolePerm MANAGE_MESSAGES @Dyno @Dyno

Is it because they are the same role? The docs say it's a valid function. I am using v12.


Solution

  • The problem is actually the statement after. This is because of the lack of semi-colons. Adding a semi-colon should make it work. Changing it to this will remove the error and will make it work too

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