Discord.js 带空格的参数
Discord.js Arguments With Spaces
所以我正在尝试创建我的机器人,我希望有一个命令可以将信息添加到 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
}
我真正想知道的是如何向参数添加 space 键,这样我就可以做
>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
好的,以你的示例命令为例,假设有人输入了这个,你想知道如何处理它:
>addplayer jimmybenoit, Example Crew, Captain, Example Longsword, Example Fruit, Gainer, Cook, Other Information Goes here
我假设您已经知道如何检查命令前缀,并将命令从参数中分离出来,但我将在下面展示我将如何做到这一点。
// 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
}
这里的关键是,您使用 .split(',')
将 args 字符串转换为数组,拆分为 ','
字符(并使用 .trim()
清除每个元素中的空格,然后将所有变量分配给 args 数组。
最后的 ...other]
用于捕获任何剩余的参数 - 因此如果您的文本 'Other information Goes here'
有逗号,所有这些额外的参数将被存储(作为数组)在 other
变量。如果你想把它们全部放回去,你可以用.join(',')
用逗号分隔。
如果您有任何问题,请告诉我!
我刚刚决定使用多个命令,例如
>edit EXAMPLE
然后做类似
的事情
>editname EXAMPLE1
将名称设置为 EXAMPLE1 而不是 EXAMPLE
您也可以这样做:
var arguments = args.toString();
arguments = arguments.split(",,").join(";")
arguments = arguments.split(",").join(" ")
arguments = arguments.split(";").join(",")
arguments = arguments.split(',');
所以我正在尝试创建我的机器人,我希望有一个命令可以将信息添加到 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
}
我真正想知道的是如何向参数添加 space 键,这样我就可以做
>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
好的,以你的示例命令为例,假设有人输入了这个,你想知道如何处理它:
>addplayer jimmybenoit, Example Crew, Captain, Example Longsword, Example Fruit, Gainer, Cook, Other Information Goes here
我假设您已经知道如何检查命令前缀,并将命令从参数中分离出来,但我将在下面展示我将如何做到这一点。
// 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
}
这里的关键是,您使用 .split(',')
将 args 字符串转换为数组,拆分为 ','
字符(并使用 .trim()
清除每个元素中的空格,然后将所有变量分配给 args 数组。
最后的 ...other]
用于捕获任何剩余的参数 - 因此如果您的文本 'Other information Goes here'
有逗号,所有这些额外的参数将被存储(作为数组)在 other
变量。如果你想把它们全部放回去,你可以用.join(',')
用逗号分隔。
如果您有任何问题,请告诉我!
我刚刚决定使用多个命令,例如
>edit EXAMPLE
然后做类似
>editname EXAMPLE1
将名称设置为 EXAMPLE1 而不是 EXAMPLE
您也可以这样做:
var arguments = args.toString();
arguments = arguments.split(",,").join(";")
arguments = arguments.split(",").join(" ")
arguments = arguments.split(";").join(",")
arguments = arguments.split(',');