简体   繁体   English

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

[英]Getting “TypeError: Cannot read property 'username' of undefined” when trying to get the username of a user in “guildMemberAdd”

I'm trying to get the username of a new member in a discord server. 我正在尝试获取不和谐服务器中新成员的用户名。 However, I found out that member is actually of type object instead of GuildMember , so I am getting an error when trying to access member.user.username . 但是,我发现该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
    }
});

The error: 错误:

    const username = member.user.username;
                                 ^

TypeError: Cannot read property 'username' of undefined

I expect it to simply do as it should. 我希望它会按原样做。

Try something like this 试试这个

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Discord.js 中获取带有用户 ID 的用户名 TypeError:无法读取未定义的属性“标签” - Getting username with user id in Discord.js TypeError: Cannot read property 'tag' of undefined TypeError:无法读取password.js中未定义的属性“用户名” - TypeError: Cannot read property 'username' of undefined in passportjs 未捕获的类型错误:无法读取未定义的属性“用户名” - Uncaught TypeError: Cannot read property 'username' of undefined TypeError:无法读取未定义的属性“用户名”? - TypeError: Cannot read property 'username' of undefined? “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” 类型错误:无法读取未定义的属性“用户名”-- - TypeError: Cannot read property 'username' of undefined-- 每当我尝试访问 record.username 时,我都会收到“TypeError: Cannot read property 'username' of undefined” - I am getting "TypeError: Cannot read property 'username' of undefined" whenever I try to access record.username 无法读取未定义的属性“用户名” - Cannot read property 'username' of undefined FormComponent.html:8错误TypeError:无法读取未定义的属性'userName' - FormComponent.html:8 ERROR TypeError: Cannot read property 'userName' of undefined 类型错误:无法读取未定义的 discord.js 的属性“用户名” - TypeError: Cannot read property 'username' of undefined discord.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM