为什么我的机器人不断崩溃(无法读取未定义的属性“发送”)

时间:2020-10-26 14:53:15

标签: javascript discord discord.js

bot.on(`guildMemberAdd`, (member) => {
 const embed = new Discord.MessageEmbed()
  .setColor(`#ffffff`)
  .setAuthor(member.user.username)
  .setDescription(
   `Please read the <#762600414183948308> and get Free roles in <#763749111286464562>.`
  )
  .setTitle(`Welcome to **Fahad Kinq's Club!**`)
  .setImage(
   `https://media.discordapp.net/attachments/766990205931880480/769939170642362368/6826b0508f5b88a53774c7f574bd18dd.png`
  );

 member.guild.channels.cache.get(`762601972255162428`).send(embed);
});

无法读取未定义的属性“发送”

所有代码正确,并且通道ID是正确的。我在做什么错?!

1 个答案:

答案 0 :(得分:0)

就像在评论中说的那样,很可能是该频道不存在,或者您的ID无效。放置在return语句中有时对确保没有找到通道会有所帮助,这将停止代码,从而防止漫游器崩溃。此外,您尝试使用的方法可能是问题本身。因此,考虑到这些要点,下面的代码是

bot.on(`guildMemberAdd`, (member) => {
// Define the channel
 const welcome = member.guild.channels.cache.find(c => c.id === `762601972255162428`)
// What if it doesn't exist, or isn't found?
 if(!welcome) return;

// Create the embed
 const embed = new Discord.MessageEmbed()
  .setColor(`#ffffff`)
  .setAuthor(member.user.username)
  .setDescription(
   `Please read the <#762600414183948308> and get Free roles in <#763749111286464562>.`
  )
  .setTitle(`Welcome to **Fahad Kinq's Club!**`)
  .setImage(`https://media.discordapp.net/attachments/766990205931880480/769939170642362368/6826b0508f5b88a53774c7f574bd18dd.png`);

// Send the message
 welcome.send(embed)
});