0

Hello, I am currently wondering how to make a invites command for my discord bot.

How it would work will be I use n!invites and it comes with You have (num) invites.

I would like it to be the amount of invites the user has gained.

If I could get some help here that would be appreciated. Thanks.

Note I have looked at the the discord.js website and there is a place for it but I never understood.

JordanC101
  • 21
  • 1
  • 2
  • 4
  • Maybe you'd want to specify what exactly these invites that you're referring to are? Amount of custom invite links they've created for the server, or? – 0x464e Jan 04 '20 at 19:43
  • I would like it to be the amount of invites the user has gained. – JordanC101 Jan 05 '20 at 05:30
  • I think I know Discord pretty well, and I haven't got the slightest clue what you're talking about. How does one "gain invites" in a Discord server? – 0x464e Jan 05 '20 at 06:06
  • So basically. A user invites users and after the user joined when the user would use !invites it would come up with how many joins they have. Take invite manager for an example. – JordanC101 Jan 05 '20 at 06:51
  • Ok, so how many times a specific user's invite link has been used is what you're asking. I see. Well what have you tried? Anything? The stuff on the Discord.js [documentation on it](https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=fetchInvites) seems pretty clear to me. Get all the invites for the guild and then pull [`inviter`](https://discord.js.org/#/docs/main/stable/class/Invite?scrollTo=inviter) and [`uses`](https://discord.js.org/#/docs/main/stable/class/Invite?scrollTo=uses) from each one, if it's available. – 0x464e Jan 05 '20 at 07:06
  • To me i don’t understand, by any chance could you give an example? – JordanC101 Jan 05 '20 at 07:56
  • I don't know javascript that well. I've been writing Discord bots in C# for a few years, but in js. I'm not trying to be rude like "no I wont help you". I would give you an example if I could. But I think your problem here mostly is just not yet being very experienced with javascript. I recommend maybe learning it a bit more. If you can't be asked, Google for some discord.js bot source codes and have a look what other people have been doing. There's something to be learned from copying other people as well. – 0x464e Jan 05 '20 at 18:17

2 Answers2

2

Here is a code I found on another question

client.on('message', message => {
    if(message.content === "n!invites"){
        var 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 ${userInviteCount} invites.`);
            }
        )
    }
});

It gets every invite links from the guild using

message.guild.fetchInvites()

and finds an invite link that was created by the user which is the author, in this case, using a filter

const userInvites = invites.array().filter(o => o.inviter.id === user.id);

Using a for loop, it adds the amount of invites the user has until the invites the user has matches it.

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

Then it adds the amount of uses to the userInviteCount statement which will be 0 if there is no invite found made by the user or the user has not invited anybody to the server.

Finally is sends a reply with the amount of invites the user has

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

It is in a Template String so you don't have to put + but instead put ${expression} which is userInviteCount, so the amount of invites the user has.

Power Teddy
  • 31
  • 1
  • 6
0

I believe the command you're looking for would look something like this:

    if (message.content === 'n!invites') {

        var userId = message.author.id;

        var userInvites = message.guild.fetchInvites().then(invites => invites.find(invite => invite.inviter.id === userId));

        var useAmount = userInvites.uses;

        if (useAmount === undefined) {

            message.channel.send(`${message.author.username} has 0 invites`);
        }

        else {

            message.channel.send(`${message.author.username} has ${useAmount} invites`);
        }
    }
iGl1tch_
  • 126
  • 4