从这段代码我得到错误/home/container/welcome.js:9 message.channel.send(消息) ^
类型错误:无法读取未定义的属性“发送”
const channelId = "812458294085550121";
bot.on("guildMemberAdd", (member) => {
console.log(member);
const message = [`Welcome <@${member.id}> to our server`]
const channel = member.guild.channels.cache.get(channelId)
message.channel.send(message)
})
}```
答案 0 :(得分:2)
您似乎遇到的问题是,找不到该频道。因此can not find property 'send' of undefined
。
试试这个:
const channelId = "812458294085550121";
bot.on("guildMemberAdd", (member) => {
console.log(member);
const welcomeMessage = `Welcome <@${member.id}> to our server`;
const channel = client.channels.cache.get(channelId);
channel.send(welcomeMessage);
});
}
答案 1 :(得分:0)
您在代码中定义了“消息”以接收通道,而将“消息”定义为要发送的消息而不是对象。此外,您将消息放入数组中,这有点令人困惑,因此您还需要做的是使用索引位置访问数组中的消息。这应该有效:
const channelId = "812458294085550121"; // Define the channel id
bot.on("guildMemberAdd", (member) => { // Event listener when someone joined a server
console.log(member); // Sends the member object in the console
const message = [`Welcome <@${member.id}> to our server`] // Message in an array
const channel = member.guild.channels.cache.get(channelId) // Find the channel
if(!channel) return console.log(`Channel doesn't exists`); // If the channel doesn't exists
channel.send(message[0]); // Send the message in the channel
});