Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

128
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!