Pixeel
Pixeel

Reputation: 41

discord.js Check user invites without leaves

I am trying to get how many members a certain member has invited. The code below works, but it counts all invited members including members that have left. I want it to show only the amount of invited members which are still on the server.

var user = null;
user = message.mentions.members.first() || message.author;
        
message.guild.fetchInvites()
.then(invites =>
{
    const userInvites = invites.array().filter(o => o.inviter.id === user.id);
    var userInviteCount = 0;

    for(var i=0; i < userInvites.length; i++)
    {
        var invite = userInvites[i];
        userInviteCount += invite['uses'];
        userInviteCount - invite['left'];
    }

    message.reply(`You have ${userInviteCount} invites.`);
});

Upvotes: 4

Views: 690

Answers (1)

FredrikRafn
FredrikRafn

Reputation: 46

Idea 1: Get the userID of each userInviteCount and check if they are still in the server via a loop.

Idea 2: Discord.JS has no function to check what invite link a member came from, so you can sadly not just cycle through all members and check whether or not they came from your desired invite link. What bots like InviteManager does is, that they detect when a member joins, and then they check all valid invite links for an increase in uses. Then, the one that goes up by 1 will be detected as the used invite link.

Upvotes: 1

Related Questions