1

I am trying to make a discord bot that will help me with my server invite management like InviteManager bot which really sucks. I wanna make my own bot to do that. So further I made code to get invites of Command Author but I don't know how to make it fetch and display mentioned user invites also.

Thanks in advance

client.on('message', message => {
        if(message.content === "-invites"){
        var user = null;
        user = 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'];
                    }
                        message.reply(`You have invited ${userInviteCount} user(s) to this server. Keep up the good work!`);
            }
        )
}
});
theduck
  • 2,589
  • 13
  • 17
  • 23
SKITTER
  • 11
  • 1
  • 2

2 Answers2

1

You can add an or operator when you declare the user - Discord.js v12

    client.on('message', message => {
        if(message.content === "-invites"){
        var user = null;
        user = message.author || message.mentions.users.first()

        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'];
                    }
                        message.reply(`You have invited ${userInviteCount} user(s) to this server. Keep up the good work!`);
            }
        )
}
});
Joshua Varghese
  • 5,082
  • 1
  • 13
  • 34
user12062676
  • 29
  • 1
  • 4
1

Try this.

client.on('message', message => {
    if(message.content === "-invites"){
        var user = null;
        user = message.author;
        
        message.guild.fetchInvites()
        .then(invites =>
            {
                //filter through the invites
                const userInvites = invites.array().filter(i => i.inviter.id === user.id);
                //get the invites using a for loop
                var userInviteLinks = "";
                for(var i=0; i < userInvites.length; i++) {
                    var invite = userInvites[i];
                    userInviteLinks += `\n https://discord.gg/${invite['code']}`;
                }
                //Sends the message
                message.reply(`Here are your invites: ${userInviteLinks}`);
            }
        )
    }
});
Power Teddy
  • 31
  • 1
  • 6