Discord.js带空格的参数

时间:2018-02-28 03:31:45

标签: javascript bots discord discord.js

所以我试图创建我的机器人,我希望有一个命令可以将信息添加到sqlite数据库

https://gyazo.com/6961a05dc2d6aeca6683b59f888c2e82

if (command === "addplayer") {
  message.delete()
  let [name, crew, rank, weapon, df, talent, profession, other] = args;
  if(!name) return message.author.send("Name argument is required!");
  let id = name.toLowerCase();
  if(!crew) {let crew = "Blank";}
  if(!rank) {let rank = "Blank";}
  if(!weapon) {let weapon = "Blank";}
  if(!df) {let df = "Blank";}
  if(!talent) {let talent = "Blank";}
  if(!profession) {let profession = "Blank";}
  if(!other) {let other = "Blank";}

  sql.run("INSERT INTO players (id, Name, Crew, Rank, Weapon, DF, 
Talent, Profession, Other) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)", [id, 
name, crew, rank, weapon, df, talent, profession, other]);
  return
}

我真想知道的是如何像参数中的空格键一样添加

>addplayer info with space, more info, even more, and so on

当我执行另一个命令时,它会像这样列出来:

Info1: info with space Info2: more info Info3: even more Info4: and so on

3 个答案:

答案 0 :(得分:0)

好的,所以使用你的示例命令作为一个例子,让我们说有人输入这个,你想知道如何处理它:

  

> addplayer jimmybenoit,示例船员,船长,示例长剑,示例水果,获得者,厨师,其他信息到这里

我假设您已经找到了如何检查命令前缀,并从args中拆分命令,但我将在下面展示如何执行此操作。

Cipher cipher = Cipher.getInstance("RSA/NONE/OAEPPadding", "BC");

此处的关键是,您使用// get message content from user let input = message.content; // get command prefix ">" // -> probably check that this is valid, if you haven't done that already let prefix = input[0]; // get command (1 word) let command = input.substr(1).split(' ')[0]; // get args - looks like you've already done this let args = command.substr( command.indexOf(' ') + 1 ); // Check if command is valid, as you have in your code if (command === "addplayer") { // split args into an array, comma delimited // -> also map .trim() function to remove beginning / ending spaces args = args.split(',').map(elem => elem.trim()); // split args into vars let [name, crew, rank, weapon, df, talent, profession, ...other] = args; // .. do the rest of your code } 将args字符串转换为数组,在.split(',')字符上拆分(并使用','清除每个元素的空白,然后您将所有变量分配给args数组。

最后的.trim()用于捕获任何剩余的参数 - 所以如果您的文本...other]有逗号,那么所有这些额外的参数将被存储(作为数组)在'Other information Goes here'中变量。如果您想将它们全部重新组合在一起,可以使用other以逗号分隔它们。

如果您有任何疑问,请与我联系!

答案 1 :(得分:0)

我刚决定使用多个命令,例如

>edit EXAMPLE 然后做类似

的事情

>editname EXAMPLE1将名称设置为EXAMPLE1而不是EXAMPLE

答案 2 :(得分:0)

您也可以这样做:

var arguments = args.toString();
arguments = arguments.split(",,").join(";")
arguments = arguments.split(",").join(" ")
arguments = arguments.split(";").join(",")
arguments = arguments.split(',');