我正在使用 node.js 的 discord.js 模块制作一个不和谐的机器人,我有一个基本的参数检测系统,它使用空格作为分隔符并将不同的参数放在一个数组中,这是我的代码的重要部分.(我使用 module.exports
使用单独的命令模块).
const args = message.content.slice(prefix.length).split(/+/);const commandName = args.shift().toLowerCase();常量命令 = client.commands.get(commandName) ||client.commands.find(cmd => cmd.alias && cmd.alias.includes(commandName));
例如当有人写 !give foo 50 bar
时.args 数组是 ['foo','50','bar']
我希望 args
在命令中不分割命令的引用部分,这意味着如果有人写 !give "foo1 bar1" 50 "foo2 bar2"
.我希望 args
返回这样的数组 ['foo1 bar1', '50','foo2 bar2']
而不是 ['foo1','bar1','50','foo2','bar1',]
这可能不是最有效的方法,但我认为它是可以理解的:我们可以扫描每个字符并跟踪我们是否在里面或在几个引号之外,如果是这样,我们忽略空格.
function parseQuotes(str = '') {让当前 = '',arr = [],inQuotes = 假for (let char of str.trim()) {if (char == '"') {//切换 inQuotes 的值inQuotes = !inQuotes} else if (char == ' ' && !inQuotes) {//如果引号之间有空格并且where'不在,则推入一个新单词arr.push(当前)当前=''} 别的 {//将字符添加到当前单词当前 += 字符}}//压入最后一个词arr.push(当前)返回 arr}//例子//运行代码片段以在控制台中查看结果//!give "foo1 bar1" 50 "foo2 bar2"让 args1 = parseQuotes('!give "foo1 bar1" 50 "foo2 bar2"')console.log(`测试 1: !give "foo1 bar1" 50 "foo2 bar2"
-> [ "${args1.join('", "')}" ]`)//!cmd foo bar baz让 args2 = parseQuotes('!cmd foo bar baz')console.log(`测试 2: !cmd foo bar baz
-> [ "${args2.join('", "')}" ]`)//!cmd foo1 weir"d quot"es "未关闭let args3 = parseQuotes('!cmd foo1 weir"d quot"es "not closed')console.log(`测试 3: !cmd foo1 weir"d quot"es "未关闭
-> [ "${args3.join('", "')}" ]`)
上一页>
I'm making a discord bot with node.js's discord.js module, I have a basic arguments detection system that uses the whitespace as the separator and puts the different arguments in an array, here's the important part of my code. (I'm using separate command modules using module.exports
).
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName) || client.commands.find(cmd => cmd.alias && cmd.alias.includes(commandName));
So for example when someone writes !give foo 50 bar
. the args array is ['foo','50','bar']
I want the args
to not divide quoted parts of the command in the command, meaning if somenoe writes !give "foo1 bar1" 50 "foo2 bar2"
. I want the args
to return an array like this ['foo1 bar1', '50','foo2 bar2']
instead of ['foo1','bar1','50','foo2','bar1',]
This may not be the most efficient way to do it, but I think it's comprehensible: we can scan every character and we keep track of whether we're inside or outside a couple of quotes and, if so, we ignore the spaces.
function parseQuotes(str = '') {
let current = '',
arr = [],
inQuotes = false
for (let char of str.trim()) {
if (char == '"') {
// Switch the value of inQuotes
inQuotes = !inQuotes
} else if (char == ' ' && !inQuotes) {
// If there's a space and where're not between quotes, push a new word
arr.push(current)
current = ''
} else {
// Add the character to the current word
current += char
}
}
// Push the last word
arr.push(current)
return arr
}
// EXAMPLES
// Run the snippet to see the results in the console
// !give "foo1 bar1" 50 "foo2 bar2"
let args1 = parseQuotes('!give "foo1 bar1" 50 "foo2 bar2"')
console.log(`Test 1: !give "foo1 bar1" 50 "foo2 bar2"
-> [ "${args1.join('", "')}" ]`)
// !cmd foo bar baz
let args2 = parseQuotes('!cmd foo bar baz')
console.log(`Test 2: !cmd foo bar baz
-> [ "${args2.join('", "')}" ]`)
// !cmd foo1 weir"d quot"es "not closed
let args3 = parseQuotes('!cmd foo1 weir"d quot"es "not closed')
console.log(`Test 3: !cmd foo1 weir"d quot"es "not closed
-> [ "${args3.join('", "')}" ]`)
这篇关于将引号内的内容计算为一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!