How to make a invites command (Discord.js)

六月ゝ 毕业季﹏ 提交于 2021-01-01 13:33:08

问题


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.


回答1:


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`);
        }
    }



回答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.




回答3:


Client.on('message', message => {

    if (message.content == "!invs") {

        // Fetch all guild invites
        let invites = await message.guild.fetchInvites();

        // Get array of invites made by message author user
        const userInvites = invites.array().filter(e => e.inviter.id == message.author.id);

        // Make a var to save count of the user invites
        let inviteCount = 0;

        // Loop through each invite and add the uses of the invite to the "inviteCount" variable.
        userInvites.forEach(invite => inviteCount += invite.uses);

        message.channel.send(`You have ${userInvites.length} active invite links, and a total of ${inviteCount} users joined with them.`);

    }

});


来源:https://stackoverflow.com/questions/59594196/how-to-make-a-invites-command-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!