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

179
Views
discord.js - Cannot read property 'roles' of undefined

I want to try to fix this error, but I have no clue on how the code im using:


const rainPerms = [
    "MANAGE_ROLES",
    "ADMINISTRATOR"
  ];

    let place = 0;
    const servers = ["111111111111111111", "222222222222222222"] //fake ID
    const Guilds = client.guilds.cache.map(guild => guild.id);
    const Server = Guilds
    function changeColor() {
        for (let index = 0; index < servers.length; ++index) {
            let server = client.guilds.cache.get(servers[index]);
            let role = server.roles.cache.find(r => r.name === 'Rainbow');
            role.setColor(rainbow[place]).catch;
            if (!rainPerms) {
            }
            continue;
            }
        }
        if (place == (size - 1)) {
            place = 0;
        } else {
            place++;
        }
    client.on('ready', () => {
        setInterval(changeColor, 60000);
        changeColor();
    });

and my error is:

let role = server.roles.cache.find(r => r.name === 'Rainbow');
                  ^
                    
TypeError: Cannot read property 'roles' of undefined

I have searched all over and I could not find much that I could understand, could someone please explain what exactly is going on here? thanks!

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

0

I would not recommend using roles cache if you're using v12/v13, instead use an async function.

To get guild:

let guild = await client.guilds.fetch('id')

And to get a role:

let role = await guild.roles.fetch('id')
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!