NodeJS、DiscordJS。按 {} 拆分参数
NodeJS, DiscordJS. Split arguments by {}
我怎样才能让它工作,我不确定我做错了什么。
let args = message.content.substring(PREFIX.length).split(" ");
const a = args;
const items = a.slice(a.indexOf('{') + 1, a.lastIndexOf('}')).split('}{')
switch(args[0]) {
case 'status':
message.channel.send("**Current Status:**");
con.query("SELECT * FROM games", function(err, result, fields) {
if(err) throw err;
Object.keys(result).forEach(function(key) {
var row = result[key];
message.channel.send('**' + row.name + '**' + ' - ' + '(' + row.description + ')' + ' - ' + '**' + row.status + '**');
});
});
break;
case 'add':
let name = items[1];
let desc = items[2];
let status = items[3];
console.log(items);
break;
我正在尝试通过 {} 拆分 !ADD 命令参数,以便该系统知道 {} 内的每个其他字符串都是下一个命令
!add {this is a argument}{another argument}{another argument sitting here}
我认为问题在于您正在拆分消息以解析出初始命令(添加),但在进行下一次拆分之前没有将其重新组合在一起。我认为您想将第二行更改为:
const a = args.slice(1).join(' ');
这应该使项目数组 ['this is a argument', 'another argument', 'another argument sitting here']
当您访问项目数组时,请确保您也使用了正确的索引。在此示例中,只有 3 个项目,因此有效索引为 (0, 1, 2)。 (在您的代码中,您正在访问 3)
一个简单的 regexp-using 方法可以是:
let line="!add {this is a argument}{another argument}{another argument sitting here}"
let [command,argumentlist]=line.match(/!([^\s]+)\s+\{(.*)\}/).splice(1);
let arguments=argumentlist.split("}{");
console.log(command);
console.log(arguments);
match()
从开头和最外层的 {}
对中剥离 !
,然后 split()
与代码中的相同。
我怎样才能让它工作,我不确定我做错了什么。
let args = message.content.substring(PREFIX.length).split(" ");
const a = args;
const items = a.slice(a.indexOf('{') + 1, a.lastIndexOf('}')).split('}{')
switch(args[0]) {
case 'status':
message.channel.send("**Current Status:**");
con.query("SELECT * FROM games", function(err, result, fields) {
if(err) throw err;
Object.keys(result).forEach(function(key) {
var row = result[key];
message.channel.send('**' + row.name + '**' + ' - ' + '(' + row.description + ')' + ' - ' + '**' + row.status + '**');
});
});
break;
case 'add':
let name = items[1];
let desc = items[2];
let status = items[3];
console.log(items);
break;
我正在尝试通过 {} 拆分 !ADD 命令参数,以便该系统知道 {} 内的每个其他字符串都是下一个命令
!add {this is a argument}{another argument}{another argument sitting here}
我认为问题在于您正在拆分消息以解析出初始命令(添加),但在进行下一次拆分之前没有将其重新组合在一起。我认为您想将第二行更改为:
const a = args.slice(1).join(' ');
这应该使项目数组 ['this is a argument', 'another argument', 'another argument sitting here']
当您访问项目数组时,请确保您也使用了正确的索引。在此示例中,只有 3 个项目,因此有效索引为 (0, 1, 2)。 (在您的代码中,您正在访问 3)
一个简单的 regexp-using 方法可以是:
let line="!add {this is a argument}{another argument}{another argument sitting here}"
let [command,argumentlist]=line.match(/!([^\s]+)\s+\{(.*)\}/).splice(1);
let arguments=argumentlist.split("}{");
console.log(command);
console.log(arguments);
match()
从开头和最外层的 {}
对中剥离 !
,然后 split()
与代码中的相同。