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

304
Views
discord.js can't add a role to member when they join my server

I want my bot to give a certain role to people when they join the server. But I get some weird error I don't really understand.

This is the code:

const { GuildMember, MessageEmbed } = require("discord.js");

module.exports = {
    name: "guildMemberAdd",
    /**
     * @param {GuildMember} member
     */
    async execute(member){
        let role = member.guild.roles.cache.some(role => role.name === 'Member')
        member.roles.add(role)

        member.guild.channels.cache.get(process.env.WELCOME_MESSAGE_CHANNEL_ID).send({ 
            embeds: [
                new MessageEmbed()
                .setTitle("Welcome! :smiley:")
                .setDescription(`${member.toString()} has joined the server!\n
                                Thanks for joining. Head over to <#${process.env.RULE_CHANNEL_ID}> and verify yourself in <#${process.env.VERIFY_CHANNEL_ID}> to get access to all other channels.`)
                .setThumbnail(member.user.displayAvatarURL())
                .setColor("GREEN")
            ]
        }) 
    }
}

And when someone joins I get this error message:

TypeError [INVALID_TYPE]: Supplied roles is not a Role, Snowflake or Array or Collection of Roles or Snowflakes.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem is that some() returns a boolean (either true or false) and when you try to add a role to the member you pass this boolean value instead of a role ID. You can use the find() method instead that returns the first item where the given function returns a truthy value (i.e. where role.name is equal to "Member"):

  async execute(member) {
    let role = member.guild.roles.cache.find((role) => role.name === 'Member');

    if (!role)
      return console.log('Cannot find the role with the name "Member"');

    member.roles.add(role);
    member.guild.channels.cache
      .get(process.env.WELCOME_MESSAGE_CHANNEL_ID)
      .send({
        embeds: [
          new MessageEmbed()
            .setTitle('Welcome! :smiley:')
            .setDescription(
              `${member.toString()} has joined the server!\n Thanks for joining. Head over to <#${process.env.RULE_CHANNEL_ID}> and verify yourself in <#${process.env.VERIFY_CHANNEL_ID}> to get access to all other channels.`,
            )
            .setThumbnail(member.user.displayAvatarURL())
            .setColor('GREEN'),
        ],
      });
  }
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!