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

804
Visualizações
(discord.js) How can I make this command only work for certain roles/users?

I'm still fairly inept when it comes to software development, and need some help with making this command work for only certain roles on my discord server, or only work for certain users

client.on('message', async message => {
    if (message.content === '-fruits') {
        try {
            await message.react('🍎');
            await message.react('🍊');
            await message.react('🍇');
        } catch (error) {
            console.error('One of the emojis failed to react:', error);
        }
    }
});
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You can restrict a command to specific users by creating an array of user IDs, and if the message authors ID is in the list check if it matches any commands.

// Create an array of user ids
const adminIDs = [/* List of user IDs */]

client.on('message', async message => {
    // Check if the user who sent the message is in the list 
    if (adminIDs.include(message.author.id)) {
        // If user is in list, then check if message matches a command
        if (message.content === 'foo') {
            // do stuff...
        } else if (message.content === 'bar') {
           // do more stuff..
        }
    }
});

As for roles, you can check if a user has a specific role.

client.on('message', async message => {
    // Check if the user has a role with the name "Admin"
    if (message.member.roles.find(r => r.name === 'Admin')){
        if (message.content === 'foo') {
            // do stuff...
        } else if (message.content === 'bar') {
           // do more stuff..
        }
    }
});
over 4 years ago · Santiago Trujillo Relatório

0

Comment on @KyleRifqi's answer (I don't have 50 rep.)

Roles also have IDs, so you can do message.member.roles.find(r => r.id === "Role ID string") instead, if you happen to have multiple roles with the same name

over 4 years ago · Santiago Trujillo 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