Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获得;TypeError:无法读取属性';用户名';“未定义”的定义;尝试在“中获取用户的用户名时”;guildMemberAdd";_Javascript_Discord.js - Fatal编程技术网

Javascript 获得;TypeError:无法读取属性';用户名';“未定义”的定义;尝试在“中获取用户的用户名时”;guildMemberAdd";

Javascript 获得;TypeError:无法读取属性';用户名';“未定义”的定义;尝试在“中获取用户的用户名时”;guildMemberAdd";,javascript,discord.js,Javascript,Discord.js,我正在尝试获取discord服务器中新成员的用户名。但是,我发现成员实际上是对象类型,而不是GuildMember,因此我在尝试访问成员.user.username时出错 bot.on('guildMemberAdd', (member) => { print(typeof member); const username = member.user.username; var channel = member.guild.channels.get('59866811

我正在尝试获取discord服务器中新成员的用户名。但是,我发现
成员
实际上是
对象
类型,而不是
GuildMember
,因此我在尝试访问
成员.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

我希望它只是做它应该做的事情。

试试这样的事情

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.`);
]);

您使用的是Discord.js的哪个版本?您能否确认
Discord.GuildMember的成员instanceof Discord.GuildMember
返回
true
(假设
Discord
是您需要Discord.js包的变量)
typeof
member
是一个对象是正确的,即使它是一个GuildMember。设置一个名为userName的变量,在我的频道中如何调用用户名。发送并检查用户名是否包含potato,或者您正在查找的任何内容。a。如果在作者的案例中没有定义,他们将无法读取用户名。这段代码只是避免使用属性,而不是解决问题。B.将GuildMember或User转换为字符串将导致他们提到的模板(例如,
),而不是他们的实际用户名。