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

查看:97
本文介绍了如何发出邀请命令(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网站,并且有放置它的地方,但我从未理解.

推荐答案

以下是我在它会使用公会获取所有邀请链接

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循环,它会添加用户的邀请数量,直到与 user 的邀请匹配为止.

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 语句中,如果未找到该用户或用户发出的邀请,该语句的使用量将为 0 .strong>尚未邀请任何人加入服务器.

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 ),因此邀请用户的数量有.

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天全站免登陆