node.jsdiscorddiscord.js

DISCORD.JS How to get the name of the person who invited the new member?


I'm currently trying to add the name of the user who invited the new member to my welcome message! could you help me? I'll leave my code below and thank you if you can help me with this!

client.on("guildMemberAdd", async (member) => { 

    let guild = await client.guilds.cache.get("SERVER ID"); // SERVER ID
    let channel = await client.channels.cache.get("CHANNEL ID"); // CHANNEL ID
    let emoji = await member.guild.emojis.cache.find(emoji => emoji.name === "eba"); // NOME DO EMOJI
    if (guild != member.guild) {
      return console.log("Sem boas-vindas pra você! Sai daqui saco pela."); // MENSAGEM EXIBIDA NO CONSOLE
     } else {
        let embed = await new Discord.MessageEmbed()
       .setColor("#fcfcfc")
        .setAuthor(member.user.tag, member.user.displayAvatarURL())
        .setTitle(`:boom: Boas-vindas :boom:`)
        .setImage("https://media.tenor.com/images/c001d9d78724152f00eca4d8ed2e2b9c/tenor.gif")
        .setDescription(`**Olá ${member.user}!**\nBem-vindo(a) ao servidor **${guild.name}**!\nVocê é o membro **#${member.guild.memberCount}\n**Compartilhe nosso servidor! :heart:`)
        .setFooter("Servidor Espalha Lixo") // Set footer
        .setTimestamp();
  
      channel.send(embed);
    }
  });

Solution

  • This is working; check out this code

    client.on("guildMemberAdd", member => {
    // To compare, we need to load the current invite list.
    member.guild.invites.fetch().then(newInvites => {
    // This is the *existing* invites for the guild.
    const oldInvites = invites.get(member.guild.id);
    // Look through the invites, find the one for which the uses went up.
    const invite = newInvites.find(i => i.uses > oldInvites.get(i.code));
    // This is just to simplify the message being sent below (inviter doesn't have a tag property)
    const inviter = client.users.cache.get(invite.inviter.id);
    // Get the log channel (change to your liking)
    const logChannel = member.guild.channels.cache.find(channel => channel.name === "join-logs");
    // A real basic message with the information we need. 
    inviter
      ? logChannel.send(`${member.user.tag} joined using invite code ${invite.code} from ${inviter.tag}. Invite was used ${invite.uses} times since its creation.`)
      : logChannel.send(`${member.user.tag} joined but I couldn't find through which invite.`);
    });
    });
    

    You can find more at discordjs-bot-guide from github. The answer - inviter.tag from code