owobot/util/chatparse_plain.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-08-04 01:41:32 -04:00
const lang = require('./mc_lang.js')
2024-08-01 20:11:59 -04:00
const parse = function (_data, l = 0) {
2024-07-30 05:56:23 -04:00
if (l >= 12) {
return ['', '', '']
}
let data
if (typeof _data === 'string') {
2024-08-01 20:11:59 -04:00
data = { text: _data }
2024-07-30 05:56:23 -04:00
} else if (typeof _data === 'number') {
2024-08-01 20:11:59 -04:00
data = { text: _data + '' }
2024-07-30 05:56:23 -04:00
} else if (_data.constructor === Array) {
2024-08-01 20:11:59 -04:00
data = { extra: _data }
2024-07-30 05:56:23 -04:00
} else {
data = _data
}
2024-08-04 01:41:32 -04:00
let out = ''
2024-07-30 05:56:23 -04:00
if (data['']) {
data.text = data['']
}
if (data.text) {
let _text = data.text
if (typeof _text === 'number') {
_text = _text.toString()
}
2024-08-01 20:11:59 -04:00
out += _text
2024-07-30 05:56:23 -04:00
}
if (data.translate) {
2024-08-01 20:32:14 -04:00
let trans = data.translate.replace(/%%/g, '\ue123')
2024-07-30 05:56:23 -04:00
if (lang[trans] !== undefined) {
trans = lang[trans].replace(/%%/g, '\ue123')
}
for (const i in data.with) {
2024-08-01 20:11:59 -04:00
const j2 = parse(data.with[i], l + 1)
2024-08-01 20:32:14 -04:00
trans = trans.replace(/%s/, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans = trans.replaceAll(`%${+i + 1}$s`, j2.replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
2024-07-30 05:56:23 -04:00
}
2024-08-01 20:32:14 -04:00
out += trans.replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
2024-07-30 05:56:23 -04:00
}
if (data.extra) {
for (const i in data.extra) {
2024-08-01 20:11:59 -04:00
const parsed = parse(data.extra[i], l)
out += parsed
2024-07-30 05:56:23 -04:00
}
}
return out
}
2024-08-01 20:11:59 -04:00
const parse2 = function (_data, l) {
2024-07-30 05:56:23 -04:00
try {
return parse(_data)
} catch (e) {
console.error(e)
2024-08-08 03:01:25 -04:00
return `An error occured while parsing a message. See console for more information. JSON that caused the error: ${JSON.stringify(_data)}`
2024-07-30 05:56:23 -04:00
}
}
module.exports = parse2