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

228
Views
Discord JS TypeError: Cannot read properties of undefined (reading 'roles')

I'm creating an event that checks a monogodatabase on a set interval to see if they're any expired keys and then to remove a role from that member.

const mongoose = require("mongoose")
const { Discord, GuildMemberRoleManager, GuildMember, Guild, Client, Message } = require("discord.js");
const { Database } = require("../../Structures/config.json");
const { count, key } = require("../../Structures/Schemas/keys");
const keys = require('../../Structures/Schemas/keys');

module.exports = {
    name: "ready",
    permission: "ADMINISTRATOR",
    /**
     * @param {Message} oldMessage
    * @param {GuildMemberRoleManager} 
    * @param {GuildMember} member
    * @param {Guild} guild
    * @param {Client} client
    */
    async execute(client, member, guild) {
        setInterval(() => {
                keys.count({ redeemed: true }, async function( err, results ){
                console.log(results)
                console.log("Number of keys:", results);
                const counts = results;
                const keyfind = await keys.findOne({ redeemed: true });
                    for(let i = 0; i < counts; i++){
                        keys.findOne({ redeemed: true }, 'redeemedAt expiryAt userid', async function (err, keys){
                            if (err) return handleError(err);
                            const currentTime = new Date(Date.now()) 
                            const expirationDate = keys.expiryAt;
                            const timeout = currentTime - expirationDate;
                            const role = guild.roles.cache.get('924064535663480922');
                            const member = await guild.members.fetch(keys.userid);
                            console.log(timeout)
                            if (timeout < -1){
                                await keys.then(keyfind.delete());
                                member.roles.remove(role);
                                
                            }
                        })
                    }
                });
                
            }, 3500); 
}
}

I keep getting this error whenever the event runs "TypeError: Cannot read properties of undefined (reading 'roles')".

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

0

Unfortunately, it's quite hard to give you a conclusive answer with the details you provided.

We know that you are trying to access something that's undefined. Are you sure guild is properly passed to your function and have you checked if your fetching of member works properly?

Best regards

about 4 years ago · Juan Pablo Isaza Report

0

I would do something like this (if you have message defined):

const role = message.guild.roles.cache.get(r => r.I’d ==='924064535663480922');

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!