尝试在“ guildMemberAdd”中获取用户名时,出现“ TypeError:无法读取未定义的属性'username'”

时间:2019-08-04 17:10:55

标签: javascript discord.js

我正在尝试获取不和谐服务器中新成员的用户名。但是,我发现member实际上是object类型,而不是GuildMember类型,因此尝试访问member.user.username时出现错误。

bot.on('guildMemberAdd', (member) => {
    print(typeof member);
    const username = member.user.username;
    var channel = member.guild.channels.get('598668119849959426');//get('589517896749940739')
    if (username.toLowerCase().includes("potato")) {
      channel.send("The evil spuds have sent " + username + " to continue the Potato Invasion!");
    } else if (username.toLowerCase().includes("berry")) {
      channel.send(username + " is a berry good fellow");
    } else {
      // idk some random stuff
    }
});

错误:

    const username = member.user.username;
                                 ^

TypeError: Cannot read property 'username' of undefined

我希望它会像应该做的那样简单。

1 个答案:

答案 0 :(得分:-1)

尝试这样的事情

client.on('guildMemberAdd', member => {
  // Send the message to a designated channel on a server:
    console.log('A New Member has joined the Discord');
  const channel = client.channels.get(welcomeChat);
  // Do nothing if the channel wasn't found on this server
  if (!channel) return;
  // Send the message, mentioning the member
  channel.send(`Welcome to the server, ${member} Please read the following 
for access to the Server.`);
]);