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()
希望这能有所帮助!
相关文章