我正在尝试获取不和谐服务器中新成员的用户名。但是,我发现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
我希望它会像应该做的那样简单。
答案 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.`);
]);