我如何处理不同数量的论点?

时间:2017-05-30 21:46:52

标签: javascript node.js

所以我有这个Discord bot,它带有一个添加链接/密码组合的命令。目前它只能接受两个参数,但是我有用户希望粘贴一整个链接/密码列表并将它们全部输入。

这是我现在的代码:

if (command === "add"){
message.delete();
let link = message.content.split(" ")[1];
let answer = message.content.split(" ")[2];
  if (!link || !answer){
  user.send('Please be sure to enter a link **and** its corresponding answer.');
  }
lockers.push({"link":link,"answer":answer});
fs.writeFile('./serverLockers.json', JSON.stringify(serverLockers), (err) => {if(err) console.error(err)});
return;
}

在这种情况下的消息是字符串“[command] [link] [answer]”。

我希望能够做一些事情,比如“[command] [link] [answer],[link] [answer],[link] [answer]”

如何更改此设置以一次添加多个组合? 它必须知道输入了多少组合 输入的组合数量总是不同的

2 个答案:

答案 0 :(得分:0)

如果内容中没有嵌入的逗号,并且您在message.content中有这样的格式:

"command1 link1 answer1, link2 answer2, link3 answer3"

然后你可以先解析命令,然后在逗号上拆分,然后在空格上拆分它们。



let message = {};
message.content =  "command1 link1 answer1, link2 answer2, link3 answer3";

let lockers = [];

// get command off the beginning
let index = message.content.indexOf(" ");
let command = message.content.slice(0, index);

// get remaining content and split it into link answer combos
let args = message.content.slice(index + 1).split(/\s*,\s*/);
// args.length is the number of combos entered
args.forEach(function(item) {
    let splits = item.split(/\s+/);
    let link = splits[0];
    let answer = splits[1];

    // code here for processing each link/answer combo (not sure what you want to do with them)
    lockers.push({"link":link,"answer":answer});
});
// final code to process lockers array
console.log("command = ", command);
console.log(lockers);




答案 1 :(得分:0)

以下是3个示例,关键是arguments是传递给函数的所有参数,即使它们未在函数定义中明确列出。

function doSomethingString(){
  for(var i=0; i<arguments.length; i+=2){
    var userName = arguments[i];
    var password = arguments[i+1];
    console.log("new user, name:"+userName+" pass:"+password);
  }
}

console.log("test1\n");
doSomethingString("user1", "pass1", "user2", "pass2");

console.log("\ntest2\n");
doSomethingString("user1", "pass1");

console.log("\ntest3\n");
doSomethingString("user1", "pass1", "user2", "pass2", "user3", "pass3");

function doSomethingObjects(){
  for(var i=0; i<arguments.length; i++){
    var userName = arguments[i].name;
    var password = arguments[i].password;
    console.log("new user, name:"+userName+" pass:"+password);
  }
}

console.log("test1\n");
doSomethingObjects({name: "user1", password: "pass1"},{name: "user2", password: "pass2"});

console.log("\ntest2\n");
doSomethingObjects({name: "user1", password: "pass1"});

console.log("\ntest3\n");
doSomethingObjects({name: "user1", password: "pass1"},{name: "user2", password: "pass2"},{name: "user3", password: "pass3"});

function doSomethingStringPairs(){
  for(var i=0; i<arguments.length; i++){
    var userName = arguments[i].split(" ")[0];
    var password = arguments[i].split(" ")[1];
    console.log("new user, name:"+userName+" pass:"+password);
  }
}

console.log("test1\n");
doSomethingStringPairs("user1 pass1", "user2 pass2");

console.log("\ntest2\n");
doSomethingStringPairs("user1 pass1");

console.log("\ntest3\n");
doSomethingStringPairs("user1 pass1", "user2 pass2", "user3 pass3");