[英]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.