Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
How can I add a role to a user on DiscordJS v13?

I am having problems adding a role to a user with DiscordJS v13. Is there any way to do it? Whenever I add them, they're not defined. Here's the code to where I want to add the add role. I'm a beginner on discord.js so bear with me. Thanks!

    } else if (commandName === 'verification'){
        let code1 = 'A96B2UUH'
        const code = options.getString('code')

        if(code === code1){
            interaction.reply({
                content: 'The code has been accepted. Welcome to the hub!',
                ephemeral: true,
            })
            let role = message.guild.roles.cache.some(role => role.name === 'Equitability')
            message.member.roles.add(role)
        }else{
            interaction.reply({
                content: 'You entered the wrong code. Please contact the administrators to gain access.',
                ephemeral: true,
            })
        }
    }
})

Edit: I fixed it! I changed some things and somehow it worked. Here's the code.

bot.on('interactionCreate', async interaction => {
    if(!interaction.isCommand()) return

    const { options } = interaction

    if(interaction.commandName === 'verification'){
        let code1 = 'A96B2UUH'
        const code = options.getString('code')

        if(code === code1){
            await interaction.reply({
                content: 'You entered the correct code. Welcome to the hub!',
                ephemeral: true,
            })
            var guild = bot.guilds.cache.get('907259574342537236')
            const member = await guild.members.fetch(interaction.user.id)
            const role = await guild.roles.fetch('907261775622328352')
            member.roles.add(role)
        }else{
            await interaction.reply({
                content: 'The code you entered is incorrect. Contact the administrators to gain access the server.',
                ephemeral: true,
            })
        }
    }
})
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are using .some() which returns a Boolean (true/false). You want the role object, so you use .find() which takes the same arguments, but returns the role object instead. Also, if the role is not cached, you can use RoleManager.fetch() to get it from the API

await message.guild.roles.fetch() //optional - put it if the role is valid, but is not cached
let role = message.guild.roles.cache.find(role => role.name === 'Equitability')
message.member.roles.add(role)
about 4 years ago · Juan Pablo Isaza Relatório

0

Getting roles in Discord.js v13 requires them to be cached, so the best way to add a role you know exists is simply by ID.

To get an ID for a role open Server Settings > Roles > Right click > "Copy ID"

Then you can simply do message.member.roles.add("roleId");

If you're not completely certain whether a role exists (i.e through user input) you can fetch all server roles (different to .cache.get), then user .filter, however this doesn't seem to be what you're doing so I won't include it

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda