如何发出邀请命令(Discord.js)

时间:2020-01-04 19:39:46

标签: node.js discord discord.js

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

它将如何工作,我将使用n!invites并附带 您有(num)个邀请。

我希望这是用户获得的邀请数量。

如果我能在这里得到一些帮助,将不胜感激。谢谢。

请注意,我已经浏览了discord.js网站,并且有放置它的地方,但我从未理解。

3 个答案:

答案 0 :(得分:0)

我相信您要查找的命令看起来像这样:

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

答案 1 :(得分:0)

这是我在another question上找到的代码

    var clickCount = [0,0,0,0,0,0,0,0,0,0]

它会使用公会获取所有邀请链接

[
{
"id": 1,
"name": "Module 1",
"topics": [
{
"id": 1,
"name": "Iterators and Helpers",
"course_id": 1
},
{
"id": 2,
"name": "Object Oriented Ruby",
"course_id": 1
},
{
"id": 3,
"name": "One to Many Relationships",
"course_id": 1
},
{
"id": 4,
"name": "Many to Many Relationships",
"course_id": 1
},
{
"id": 5,
"name": "Active Record Basics",
"course_id": 1
},
{
"id": 2,
"name": "Module 2",
"topics": [
{
"id": 9,
"name": "Introduction to Sinatra",
"course_id": 2
},
{
"id": 10,
"name": "Sinatra CRUD",
"course_id": 2
},
{
"id": 11,
"name": "Sinatra Associations",
"course_id": 2
},
{
"id": 12,
"name": "Introduction to Rails",
"course_id": 2
},
{
"id": 13,
"name": "Rails Forms + CRUD",
"course_id": 2
},
{
"id": 14,
"name": "Rails Associations + CRUD",
"course_id": 2
},
{
"id": 15,
"name": "Rails Sessions and Cookies",
"course_id": 2
},
{
"id": 16,
"name": "Rails Authentication and Authorization",
"course_id": 2
},
{
"id": 17,
"name": "Module 2 Project Guidelines",
"course_id": 2
}
]
{
"id": 3,
"name": "Module 3",
"topics": [
{
"id": 18,
"name": "Introduction to JavaScript",
"course_id": 3
},
{
"id": 19,
"name": "Introduction to Document Object Model",
"course_id": 3
},
{
"id": 20,
"name": "JavaScript Events",
"course_id": 3
},
{
"id": 21,
"name": "JavaScript Asynchronous Functionality + Fetch & CRUD",
"course_id": 3

,并找到由用户创建的邀请链接,在这种情况下,该用户是作者

render(){
      console.log(this.props.courseNames)
       return(
        <FlatList style={styles.flatlist} keyExtractor={(item)=> item.id} data={this.props.courseNames} ItemSeparatorComponent = { this.FlatListItemSeparator } 
        renderItem={({item}) => { 
         return <TouchableOpacity><Button title={item.name} onPress={() => this.props.navigation.navigate('Topics')} style={styles.listitem}/></TouchableOpacity>
        }}
        ListHeaderComponent = {this.header}/>
       )
     }
  }

使用for循环,它会添加用户的邀请数量,直到与 user 的邀请匹配为止。

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

然后将使用量添加到 userInviteCount 语句中,如果未找到该用户或用户发出的邀请,该使用量将为 0 。 strong>尚未邀请任何人加入服务器。

最后是发送一封回复,其中包含用户的邀请数量

message.guild.fetchInvites()

它在模板字符串中,因此您不必输入+,而只需输入$ {expression}(即 userInviteCount ),因此邀请用户的数量有。

答案 2 :(得分:0)

您可以通过获取用户创建的所有公会邀请和过滤邀请来做到这一点。检查这个例子!

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

    }

});