愿所有的美好和期待都能如约而至

TypeError:无法读取未定义的属性 读取'频道'

发布时间:  来源:互联网  作者:匿名  标签:discord discord.js error TypeError: Cannot read properties of undefined (reading  热度:37.5℃

本文介绍了TypeError:无法读取未定义的属性(读取'频道')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个小问题,我收到了未定义错误的Cannot Read属性,但我不知道问题出在哪里。有人能帮我搬这个吗?

代码如下:

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);
        });
    });
};

下面是我收到的错误:

TypeError: Cannot read properties of undefined (reading 'channels')

推荐答案

可能并非所有渠道和行会都添加到缓存中。先试着把它们拿来,然后再看看。例如,下面这样的代码应该可以工作:

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);
        });
    });
};

不是先获取行会然后获取渠道,而是直接从客户端获取渠道,如下所示:

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);
        });
    });
};

或者,如@Malik Lahlou所说,可能是打字错误。

资源:

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

希望这能有所帮助!

这篇关于TypeError:无法读取未定义的属性(读取&#39;频道&#39;)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

勇敢去编程!

勇敢的热爱编程,未来的你一定会大放异彩,未来的生活一定会因编程更好!

TOP