我正在制作一个不和谐的欢迎机器人,当有人加入服务器时,它会发送这个错误:
<块引用>TypeError: 无法读取未定义的属性 'toString'
这是源代码:
module.exports = (client) => {
const channelid = "865471665168580628";
client.on("guildMemberAdd", (member) => {
const serverid = member.guild.id
const guild = client.guilds.cache.get(serverid);
console.log("member");
const ruleschannel = guild.channels.cache.find(channel => channel.name === "rules");
const message = `welcome <@${member.id}> to music and chill! please read the ${member.guild.channels.cache.get(ruleschannel).toString()} before you start chatting.`;
const channel = member.guild.channels.cache.get(channelid);
channel.send(message);
})
}
有人可以帮我吗?
答案 0 :(得分:2)
这意味着 member.guild.channels.cache.get(ruleschannel)
是 undefined
。由于 ruleschannel
是通道对象,而 Collection#get()
方法需要雪花,因此您需要使用其 id
属性。
所以 member.guild.channels.cache.get(ruleschannel.id)
应该可以工作。
更好的方法是检查 ruleschannel
是否存在。此外,您只需在反引号内添加 rulesChannel
即可将其转换为频道链接。看看下面的代码:
client.on('guildMemberAdd', (member) => {
// not sure why not just: const { guild } = member
const guild = client.guilds.cache.get(member.guild.id);
const rulesChannel = guild.channels.cache.find((channel) => channel.name === 'rules');
if (!rulesChannel)
return console.log(`Can't find a channel named "rules"`);
const channel = guild.channels.cache.get(channelid);
const message = `Welcome <@${member.id}> to music and chill! Please, read the ${rulesChannel}.`;
channel.send(message);
});