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

122
Visualizações
My code for autorole doesn't work and I can't figure out why
const { Client,  Intents } = require("discord.js");

const client = new Client({
    intents: ["DIRECT_MESSAGES", "GUILDS", "GUILD_MEMBERS"],
    presence: {
        status: "online",
        activities: [{
            name: "markets",
            type: "WATCHING"
        }]
    },
});

client.on('ready', () => { 
    console.log(`Launched as a bot: ${client.user.tag}!`);
});


client.on('guildMemberAdd', member => {
    function roleAdd() {
        member.roles.add(member.guild.roles.cache.get(process.env.SERVER_ROLE_ID));
    }
    roleAdd();
});

client.login(process.env.DJS_TOKEN);

Guys I wrote this code but it is not working. I am not getting any errors. I want it to add a role whenever a user joins to my server. the status of the bot is working. I think the issue is on the guildMemberAdd part.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Im not quite sure why you're using the approach of nested functions for this. If you want the event to call the function move the function outside of the event and then create the GuildMember in a parameter.

Or discard the event entirely and leave the plain #<GuildMemberRoleManager>.add method by itself.

I suggest the latter as I assume the nested function declaration wont work.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you wanted to use auto role add for your server, try the easiest method to do the role add, same way as giving role to a member

When you giving a role to specific member should look like this:

const role = message.guild.roles.cache.find(role => role.id === process.env.SERVER_ROLE_ID)
const target = message.mentions.members.first() || await message.guild.members.cache.get(args[0])

If a bot giving a role higher than the bot or equal to bot. You will get an error such as Missing Permissions To prevent this you need to put a code to return it

if(!target.moderatable) return message.reply("I can't mute this person!")

This command is a same way for auto give role, just a few code lines added/edited/removed.

client.on('guildMemberAdd', member => {
function roleAdd() {
    member.roles.add(member.guild.roles.cache.get(process.env.SERVER_ROLE_ID));
}
roleAdd();
});
- function roleAdd() {}
+ let role = member.guild.roles.cache.find(role => role.id == process.env.SERVER_ROLE_ID)
+ member.roles.add(role).catch()

The .catch() line is to prevent on giving error and shutdown your bot.

client.on('guildMemberAdd', member => {
let role = member.guild.roles.cache.find(role => role.id == process.env.SERVER_ROLE_ID)
member.roles.add(role).catch()
});
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