Home > Software engineering >  Discord.js Throwing "Unknown Role" Error Even Though the Role Exists
Discord.js Throwing "Unknown Role" Error Even Though the Role Exists

Time:08-01

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?

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

CodePudding user response:

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);
  • Related