将用户添加到数组

时间:2018-12-20 20:25:17

标签: javascript node.js discord.js

我确信解决问题的方法很容易被忽略。

我要做的是,当用户输入!add不和谐时,机器人会将它们添加到一个数组中(供6个人拾起游戏)。

当前,当第一个用户键入!add时,它将其添加到数组中。当第二个用户执行!add时,数组不会添加第二个用户,而是将第一个用户保留在数组中。

它的工作方式是用户1和用户2都在数组中。

const pugSize = 6; // Maximum amount of players in the PUG
const pugMembers = []; // Array to hold the members in the PUG

function checkPugSize(){
    if (pugMembers.length == 6){
        //TODO Create the two teams
        console.log(`PUG IS FULL: ${pugMembers.length}`);
    }else{
        console.log(`THE PUG IS NOT FULL: ${pugMembers.length}`);
    }
}

function addUserPug(msg){
    // console.log(msg.author);
    // Add user to the pugMembers Array if the array is not full
    if (pugMembers<=6){
        pugMembers.push(msg.author.username);
    }else{ // Create a new pug and pass the user into the array
        console.log("TODO: Create a new pug when current array is filled");
        // createNewPug(msg.author.username);
    }
    msg.channel.send(`${msg.author} added to queue ${pugMembers.length}/6.`); // Mention the user that they are added into the queue
    // msg.reply(' added to queue. ' + `${pugMembers.length}/6`);
    msg.delete()
    .then(msg => console.log(pugMembers))
    .catch(console.error);
}

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', msg => {
    if (msg.content == '!size'){
        msg.channel.send(`Current PUG size: ${pugMembers.length}`);
    }
    if (msg.content === '!add'){
        // console.log(msg.author);
        checkPugSize();
        addUserPug(msg);
    }
});

1 个答案:

答案 0 :(得分:1)

使用

pugMembers.length <= 6

您当前的陈述在行为方面未定义。