【问题标题】:TypeError: member is not a function when using `${member}`TypeError: member is not a function when using `${member}`
【发布时间】:2021-04-22 01:24:12
【问题描述】:

我有一个功能,可以在用户加入时在 Discord 上发送随机选择的欢迎消息,并向其添加角色并且它曾经可以工作,但在更新到 Discord.js v12 后,我收到错误

  • Welcome ${member}, that will be 6,99 $. Wanna pay cash or credit card? ^

TypeError: member is not a function*

当用户加入时。有人可以帮我吗?这是我的代码。

// When a member joins, this function executes.
bot.on('guildMemberAdd', (member) => {
    console.log('User ' + member.id + ' has joined the guild.');
    // Welcome messages possible options.
    let welcomeMessages = [`${member}, welcome to our server :wave:!\n\nNow you're **one of us** :new_moon_with_face:! \n\n`, 
    `Welcome, ${member}. Hope you enjoy your stay.`, `Look who showed up... ${member}`, `Welcome ${member}, have a pleasant time.`, 
    `So... ${member} just arrived.`, `Great... ${member} is here!`, `Everyone stand up! ${member} is here.`, `Hello daddy ${member}...`
    `Welcome ${member}, that will be 6,99 $. Wanna pay cash or credit card?`
    ];

    let welcomeChannel = member.guild.channels.cache.get(serverData.welcomeChannelId);
    let defaultRole = member.guild.roles.cache.get(serverData.defaultRoleId);
  
    // Both the welcome message and GIFs will be chosen randomly.
    let welcomeGif = welcomeGifs[Math.floor(Math.random() * welcomeGifs.length)];
    let welcomeMessage = welcomeMessages[Math.floor(Math.random() * welcomeMessages.length)];
  
    // This is the variable that stores the user profile picture for later use.
    let newUserIcon = member.user.displayAvatarURL;
  
    welcomeChannel.send(`${member}`);
  
    // This is my embedded message, which allows loads of customization.
    let welcomeMessageEmbed = new Discord.MessageEmbed()
      .setColor(Math.floor(Math.random() * 16777214) + 1) // The color will be random.
      .setDescription(welcomeMessage) // A message will be chosen randomly from the welcome messages tab.
      .setImage(welcomeGif) // Same with the GIF.
      .setThumbnail(newUserIcon) // The icon of the embedded message will be the user profile picture.
  
    // Send in the embed.
    welcomeChannel.send(welcomeMessageEmbed);
  
    // This is the default role.
    member.roles.add(defaultRole);
});

【问题讨论】:

  • 它必须是 member.name 或类似的东西。从您的代码来看,成员似乎是一个对象。
  • 不起作用:(

标签: javascript node.js discord discord.js bots


【解决方案1】:

member 是一个对象,你需要一个字符串。尝试在模板文字中使用member.displayName

【讨论】: