問題描述
我正在使用 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模板網!