owobot/util/chatparse_plain.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

2024-08-12 04:33:43 -04:00
const lang = require('./mc_lang.js')
const parse = function (_data, l = 0) {
if (l >= 4) {
return ''
}
let data
if (typeof _data === 'string') {
data = { text: _data }
} else if (typeof _data === 'number') {
data = { text: _data + '' }
2024-08-17 06:19:40 -04:00
} else if (_data.constructor === Array) {
2024-08-12 04:33:43 -04:00
data = { extra: _data }
} else {
data = _data
}
let out = ''
if (data['']) {
data.text = data['']
}
if (data.text) {
let _text = data.text
if (typeof _text === 'number') {
_text = _text.toString()
}
out += _text
}
if (data.translate) {
let trans = data.translate.replace(/%%/g, '\ue123')
if (lang[trans] !== undefined) {
trans = lang[trans].replace(/%%/g, '\ue123')
}
for (const i in data.with) {
const j2 = parse(data.with[i], l + 1)
2024-08-19 01:56:31 -04:00
trans = trans.replace(/%s/, j2.replaceAll('%s', '\ud900\ud804').replaceAll('$s', '\ud900\ud805'))
trans = trans.replaceAll(`%${+i + 1}$s`, j2.replaceAll('%s', '\ud900\ud804').replaceAll('$s', '\ud900\ud805'))
2024-08-12 04:33:43 -04:00
}
2024-08-19 01:56:31 -04:00
out += trans.replaceAll('\ud900\ud801', '%').replaceAll('\ud900\ud804', '%s').replaceAll('\ud900\ud805', '$s')
2024-08-12 04:33:43 -04:00
}
if (data.extra) {
for (const i in data.extra) {
const parsed = parse(data.extra[i], l)
out += parsed
}
}
return out
}
const parse2 = function (_data, l) {
try {
return parse(_data)
} catch (e) {
console.error(e)
return `An error occured while parsing a message. See console for more information. JSON that caused the error: ${JSON.stringify(_data)}`
}
}
module.exports = parse2