将用户添加到数组

Add user to array

我确信我的问题的解决方案很简单,但我忽略了。

我想做的是当用户在 discord 中输入 !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);
    }
});

使用

pugMembers.length <= 6

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