Glitchy Brick
Glitchy Brick

Reputation: 126

How Do i Check Who Invited Who Discord.js

Hey So Im Making a Invite Logger and i Need help i dont know how to find the user who invited a user

client.on('guildMemberAdd', async (member) => {
    const channel = member.guild.channels.cache.get('CHANNEL_ID');
    channel.send(`Welcome ${member.user.tag} Invited By ${inviter}`)
})

Upvotes: 2

Views: 3922

Answers (1)

Yankue
Yankue

Reputation: 378

There is no easy built in way to do this, so instead you'll have to store the number of uses of every invite, then when somebody joins, see which invite's uses doesn't match the last known number, ie seeing which invite's use count changes.

client.invites = {}

client.on('ready', () => {
    client.guilds.cache.each(guild => { //on bot start, fetch all guilds and fetch all invites to store
        guild.fetchInvites().then(guildInvites => {
            guildInvites.each(guildInvite => {
                client.invites[guildInvite.code] = guildInvite.uses
            })
        })
    })
})

client.on('inviteCreate', (invite) => { //if someone creates an invite while bot is running, update store
    client.invites[invite.code] = invite.uses
})

client.on('guildMemberAdd', async (member) => {
    const channel = member.guild.channels.cache.get('CHANNEL_ID');
    member.guild.fetchInvites().then(guildInvites => { //get all guild invites
        guildInvites.each(invite => { //basically a for loop over the invites
            if(invite.uses != client.invites[invite.code] { //if it doesn't match what we stored:
                channel.send(`Welcome ${member.user.tag} Invited By ${invite.inviter.tag}`)
                client.invites[invite.code] = invite.uses
            }
        })
    })
})

You should add some sort of check (which I admittedly should have done), as they could have used a vanity URL or server discovery or some other strange way of joining, in which case it would probably error.

Upvotes: 4

Related Questions