Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord.js Throwing "Unknown Role" Error Even Though the Role Exists

I’m using discord.js v13, and I’m getting a "DiscordAPIError: Unknown Role" upon trying to add a role to a member.

async execute(client, interaction, params){
        await interaction.guild.roles.create({
            name: "Alive Town Member",
            color: "ORANGE"
        });
    
        let aliveRole = interaction.guild.roles.cache.find(r => r.name = "Alive Town Member");
        console.log(aliveRole);
        await interaction.member.roles.add(aliveRole);
}

I tried printf debugging, and aliveRole does indeed seem to be a Role object.

Does anyone know what’s going on?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Edit: The role is showing up on the Discord guild.

>Solution :

I tried your code, and everything seemed to be working fine. The only time the code got the same error as yours was when there were duplicates of the "Alive Town Member" role in the guild. What I suggest you do is check if you have duplicate roles in your guild. If it still doesn’t work, try this:

let role = await interaction.guild.roles.create({
        name: "Alive Town Member",
        color: "ORANGE"
    });

    let aliveRole = await interaction.guild.roles.fetch(role.id);
    console.log(aliveRole);
    await interaction.member.roles.add(aliveRole);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading