Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

127
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda