DiscordJS v13 无法读取未定义的属性(读取 'channels')
DiscordJS v13 Cannot read properties of undefined (reading 'channels')
代码:
const config = require('../../config.json');
const Discord = require('discord.js');
module.exports = async(guild, bannerURL, client) => {
const logChannel = await client.channels.cache.get(config.log_channel_id);
if (!logChannel) return;
const embed = new Discord.MessageEmbed()
.setAuthor({ name: guild.name, iconURL: guild.iconURL() })
.setDescription(`**${guild.name} has banner now!**`)
.setImage(bannerURL)
.setColor(config.colour)
.setTimestamp()
return logChannel.send({ embeds: [embed] })
}
向服务器添加横幅时出现错误 Unhandled Rejection: TypeError: Cannot read properties of undefined (reading 'channels')
其他日志文件具有完全相同的代码行const logChannel = await client.channels.cache.get(config.log_channel_id);
并且工作正常
在const logChannel
之前添加console.log(client)
returns undefined
在我们解决问题后,下面的代码有效。
banner.js
module.exports = {
name: 'banner',
description: 'emit update',
devs: true,
run: async (guild, message) => {
guild.emit('guildBannerAdd', message.member);
message.channel.send('Done ...');
},
};
guildBannerAdd.js
const config = require('../../config.json')
const Discord = require('discord.js')
module.exports = async (client, member) => {
const guild = client.guilds.cache.get(config.serverID)
const logChannel = client.channels.cache.get(config.log_channel_id);
if (!logChannel) return;
const embed = new Discord.MessageEmbed()
.setAuthor({
name: guild.name,
iconURL: guild.iconURL()
})
.setDescription(`**${guild.name} has banner now!**`)
.setImage(guild.bannerURL())
.setColor(config.colour)
.setTimestamp();
return logChannel.send({
embeds: [embed]
});
}
代码:
const config = require('../../config.json');
const Discord = require('discord.js');
module.exports = async(guild, bannerURL, client) => {
const logChannel = await client.channels.cache.get(config.log_channel_id);
if (!logChannel) return;
const embed = new Discord.MessageEmbed()
.setAuthor({ name: guild.name, iconURL: guild.iconURL() })
.setDescription(`**${guild.name} has banner now!**`)
.setImage(bannerURL)
.setColor(config.colour)
.setTimestamp()
return logChannel.send({ embeds: [embed] })
}
向服务器添加横幅时出现错误 Unhandled Rejection: TypeError: Cannot read properties of undefined (reading 'channels')
其他日志文件具有完全相同的代码行const logChannel = await client.channels.cache.get(config.log_channel_id);
并且工作正常
在const logChannel
之前添加console.log(client)
returns undefined
在我们解决问题后,下面的代码有效。
banner.js
module.exports = {
name: 'banner',
description: 'emit update',
devs: true,
run: async (guild, message) => {
guild.emit('guildBannerAdd', message.member);
message.channel.send('Done ...');
},
};
guildBannerAdd.js
const config = require('../../config.json')
const Discord = require('discord.js')
module.exports = async (client, member) => {
const guild = client.guilds.cache.get(config.serverID)
const logChannel = client.channels.cache.get(config.log_channel_id);
if (!logChannel) return;
const embed = new Discord.MessageEmbed()
.setAuthor({
name: guild.name,
iconURL: guild.iconURL()
})
.setDescription(`**${guild.name} has banner now!**`)
.setImage(guild.bannerURL())
.setColor(config.colour)
.setTimestamp();
return logChannel.send({
embeds: [embed]
});
}