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

163
Views
Discord.js - Add role to server member based on user tag

I am attempting to use Discord.js to add a role to a member given their Discord tag.

There is an error being returned from member.roles.add , although the role is successfully being added when I check in the Discord server.

Here is my code (test.js):

const { Client, Intents } = require('discord.js');

// Discord client & server details
let intents = new Intents(Intents.NON_PRIVILEGED);
intents.add('GUILD_MEMBERS');

const discord_client = new Client({ intents: intents })

discord_client.once('ready', async () => {
    console.log('Ready!');
})

const addRole = async () => {
    const guild = discord_client.guilds.cache.get('<guild-id>')
    const members = await guild.members.fetch()
    const member = members.find((m) => m.user.tag === 'Example#1234')
    return await member.roles.add('<role-id>')
}

discord_client.login('<token>').then(
    () => {
        console.log("Discord Bot connected!")
        addRole().then((res) => console.log(res))
    }
)

Here is the error:

/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:36
    return this.guild.roles.cache.filter(role => this.member._roles.includes(role.id)).set(everyone.id, everyone);
                                                                                                    ^

TypeError: Cannot read properties of undefined (reading 'id')
    at GuildMemberRoleManager.get cache [as cache] (/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:36:101)
    at GuildMemberRoleManager.add (/node_modules/discord.js/src/managers/GuildMemberRoleManager.js:127:31)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async addRole (/test.js:17:12)
about 4 years ago · Juan Pablo Isaza
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!