Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
TypeError: Cannot read properties of undefined (reading 'channels')

I have this small problem where I get the Cannot read properties of undefined error, but I don't know where the issue is. Can someone help me with this one?

Here is the code:

const { Client } = require("discord.js");
const DB = require("../Structures/Schemas/LockDown");
/**
 * @param {Client} client
 */
module.exports = async(client) => {
    DB.find().then(async (documentsArray) => {
        documentsArray.forEach(async (d) => {
            const Channel = client.guilds.cache
            .get(d.GuildId)
            .channels.cache.get(d.ChannelID);
            if(!Channel) return;

            const TimeNow = Date.now();
            if(d.Time < TimeNow){
            Channel.permissionOverwrites.edit(d.GuildID, {
                SEND_MESSAGES: null,
            });
            return await DB.deleteOne({ ChannelID: Channel.id });
        }

            const ExpireDate = d.time - Date.now();

            setTimeout(async () => {
                Channel.permissionOverwrites.edit(d.GuildID, {
                    SEND_MESSAGES: null,
                });
                await DB.deleteOne({ ChannelID: Channel.id });
            }, ExpireDate);
        });
    });
};

and here is the error I get:

TypeError: Cannot read properties of undefined (reading 'channels')
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is a chance that not all channels and guilds are added to the cache. Try fetching them first and then see. For example, something like this should work:

const { Client } = require('discord.js');
const DB = require('../Structures/Schemas/LockDown');
/**
 * @param {Client} client
 */
module.exports = async(client) => {
    DB.find().then(async(documentsArray) => {
        documentsArray.forEach(async(d) => {
            const GuildChannel = await client.guilds.fetch(d.GuildId);
            const Channel = await GuildChannel.channels.fetch(d.ChannelID);
            if (!Channel) return;

            const TimeNow = Date.now();
            if (d.Time < TimeNow) {
                Channel.permissionOverwrites.edit(d.GuildID, {
                    SEND_MESSAGES: null,
                });
                return await DB.deleteOne({
                    ChannelID: Channel.id
                });
            }

            const ExpireDate = d.time - Date.now();

            setTimeout(async() => {
                Channel.permissionOverwrites.edit(d.GuildID, {
                    SEND_MESSAGES: null,
                });
                await DB.deleteOne({
                    ChannelID: Channel.id
                });
            }, ExpireDate);
        });
    });
};

Instead of fetching the guild and then the channel, directly fetch the channel from the client like so:

const { Client } = require('discord.js');
const DB = require('../Structures/Schemas/LockDown');
/**
 * @param {Client} client
 */
module.exports = async(client) => {
    DB.find().then(async(documentsArray) => {
        documentsArray.forEach(async(d) => {
            const Channel = await client.channels.fetch(d.ChannelID);
            if (!Channel) return;

            const TimeNow = Date.now();
            if (d.Time < TimeNow) {
                Channel.permissionOverwrites.edit(d.GuildID, {
                    SEND_MESSAGES: null,
                });
                return await DB.deleteOne({
                    ChannelID: Channel.id
                });
            }

            const ExpireDate = d.time - Date.now();

            setTimeout(async() => {
                Channel.permissionOverwrites.edit(d.GuildID, {
                    SEND_MESSAGES: null,
                });
                await DB.deleteOne({
                    ChannelID: Channel.id
                });
            }, ExpireDate);
        });
    });
};

Or, as @Malik Lahlou said, it could be a typo.

Resources:

  • ChannelManager.fetch()
  • GuildManager.fetch()


Hoped this helped!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda