如何制作邀请命令(Discord.js) [英] How to make a invites command (Discord.js)

查看:14
本文介绍了如何制作邀请命令(Discord.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我目前想知道如何为我的不和谐机器人发出邀请命令.

它的工作原理是我使用 n!invites 并且它附带您有 (num) 个邀请.

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.

请注意,我查看了 discord.js 网站,有一个地方,但我一直不明白.

推荐答案

这是我在 另一个问题

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

使用 for 循环,它会添加 用户 拥有的邀请数量,直到 用户 拥有的邀请与之匹配.

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'];
}

然后它将使用量添加到 userInviteCount 语句中,如果没有找到用户或 用户 的邀请,则该语句将为 0strong> 没有邀请任何人加入服务器.

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

它在一个模板字符串中,所以你不必放 + 而是放 ${expression} 即 userInviteCount,所以邀请的数量 user有.

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.

这篇关于如何制作邀请命令(Discord.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆