owobot/util/chatparse.js

151 lines
6.8 KiB
JavaScript
Raw Normal View History

2024-07-19 12:10:22 -04:00
const _lang = require("minecraft-data")("1.20.2").language;
let lang=Object.create(null); //Without constructor function
for(const i in _lang){
lang[i]=_lang[i];
}
2024-07-16 09:51:21 -04:00
const consoleColors={
2024-07-16 11:13:07 -04:00
"dark_red":"\x1B[0m\x1B[38;2;170;0;0m",
"red":"\x1B[0m\x1B[38;2;255;85;85m",
"dark_green":"\x1B[0m\x1B[38;2;0;170;0m",
"green":"\x1B[0m\x1B[38;2;85;255;85m",
"gold":"\x1B[0m\x1B[38;2;255;170;0m",
"yellow":"\x1B[0m\x1B[38;2;255;255;85m",
"dark_blue":"\x1B[0m\x1B[38;2;0;0;170m",
"blue":"\x1B[0m\x1B[38;2;85;85;255m",
"dark_purple":"\x1B[0m\x1B[38;2;170;0;170m",
"light_purple":"\x1B[0m\x1B[38;2;255;85;255m",
"dark_aqua":"\x1B[0m\x1B[38;2;0;170;170m",
"aqua":"\x1B[0m\x1B[38;2;85;255;255m",
"black":"\x1B[0m\x1B[48;2;220;220;220m\x1B[38;2;0;0;0m",
2024-07-16 11:13:07 -04:00
"gray":"\x1B[0m\x1B[38;2;170;170;170m",
"dark_gray":"\x1B[0m\x1B[38;2;85;85;85m",
"white":"\x1B[0m\x1B[38;2;255;255;255m",
"reset":"\x1B[0m\x1B[38;2;255;255;255m"
2024-07-16 09:51:21 -04:00
}
2024-07-18 01:42:42 -04:00
const hexColorParser=(color)=>{
let out="\x1B[0m";//\x1B[48;2;220;220;220m\x1B[38;2;0;0;0m
2024-07-18 01:42:42 -04:00
const redChannel=Number("0x"+color.slice(1,3));
const greenChannel=Number("0x"+color.slice(3,5));
const blueChannel=Number("0x"+color.slice(5,7));
if(redChannel < 96 && greenChannel < 96 && blueChannel < 96){
out+="\x1B[48;2;220;220;220m";
2024-07-18 01:42:42 -04:00
}
return out+`\x1B[38;2;${redChannel};${greenChannel};${blueChannel}m`
}
const parse=function(_data, l = 0, resetColor = [consoleColors.reset]){
2024-07-19 14:12:40 -04:00
if (l >= 12) {
return ['', '', '']
}
2024-07-16 12:14:07 -04:00
let data;
if(typeof _data == "string"){
2024-07-16 12:52:35 -04:00
try {
data=JSON.parse(_data);
} catch(e){
data={text:_data, color: "reset"}
}
2024-07-16 18:27:19 -04:00
} else if(typeof _data == "number"){
data={text:_data+"", color: "reset"}
2024-07-16 12:14:07 -04:00
} else {
data=_data;
}
2024-07-16 09:51:21 -04:00
let nkt=false;
2024-07-06 11:02:11 -04:00
const out=["","",""]; //console plain minecraft
2024-07-07 02:04:04 -04:00
if(data[""]){
data.text=data[""];
2024-07-16 09:51:21 -04:00
nkt=true;
}
if(data.color){
if(data.color=="reset"){
out[0]+=resetColor[0]
2024-07-16 11:14:33 -04:00
} else if (data.color.startsWith("#")){
2024-07-18 01:42:42 -04:00
out[0]+=hexColorParser(data.color);
2024-07-16 09:51:21 -04:00
} else {
out[0]+=consoleColors[data.color];
}
2024-07-16 12:52:35 -04:00
} else {
out[0]+=resetColor[0]
2024-07-07 02:04:04 -04:00
}
2024-07-06 11:02:11 -04:00
if(data.text){
2024-07-22 04:07:23 -04:00
let _text=data.text;
if(typeof _text=="number"){
_text=_text.toString()
}
2024-07-16 09:51:21 -04:00
if(nkt){
out[0]+=resetColor[0];
out[2]+=resetColor[1];
}
2024-07-22 04:07:23 -04:00
out[0]+=_text.replace(/\u001b/g,""); //Remove escape codes from console format
out[1]+=_text;
out[2]+=_text;
2024-07-06 11:02:11 -04:00
}
if (data.translate) {
let trans = data.translate.replace(/%%/g, '\ue123').replace(/\u001b/g,""); //Remove escape codes from console format
2024-07-06 11:02:11 -04:00
let trans2 = data.translate.replace(/%%/g, '\ue123')
let trans3 = data.translate.replace(/%%/g, '\ue123')
if (lang[trans] !== undefined) {
trans = lang[trans].replace(/%%/g, '\ue123')
trans2 = lang[trans2].replace(/%%/g, '\ue123')
trans3 = lang[trans3].replace(/%%/g, '\ue123')
}
2024-07-16 13:22:34 -04:00
for (const i in data.with) {
2024-07-16 09:51:21 -04:00
const j2 = parse(data.with[i], l + 1, data.color?[consoleColors[data.color],""]:resetColor)
2024-07-06 11:02:11 -04:00
trans = trans.replace(/%s/, j2[0].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%s/, j2[1].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans3 = trans3.replace(/%s/, j2[2].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
}
// %n$s only goes up to 4 normally
if (data.with) {
if (data.with[0]) {
2024-07-16 09:51:21 -04:00
const j2_1 = parse(data.with[0], l + 1, data.color?[consoleColors[data.color],""]:resetColor)
2024-07-06 11:02:11 -04:00
trans = trans.replace(/%1\$s/g, j2_1[0].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%1\$s/g, j2_1[1].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans3 = trans3.replace(/%1\$s/g, j2_1[2].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
}
if (data.with[1]) {
2024-07-16 09:51:21 -04:00
const j2_2 = parse(data.with[1], l + 1, data.color?[consoleColors[data.color],""]:resetColor)
2024-07-06 11:02:11 -04:00
trans = trans.replace(/%2\$s/g, j2_2[0].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%2\$s/g, j2_2[1].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans3 = trans3.replace(/%2\$s/g, j2_2[2].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
}
if (data.with[2]) {
2024-07-16 09:51:21 -04:00
const j2_3 = parse(data.with[2], l + 1, data.color?[consoleColors[data.color],""]:resetColor)
2024-07-06 11:02:11 -04:00
trans = trans.replace(/%3\$s/g, j2_3[0].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%3\$s/g, j2_3[1].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans3 = trans3.replace(/%3\$s/g, j2_3[2].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
}
if (data.with[3]) {
2024-07-16 09:51:21 -04:00
const j2_4 = parse(data.with[3], l + 1, data.color?[consoleColors[data.color],""]:resetColor)
2024-07-06 11:02:11 -04:00
trans = trans.replace(/%4\$s/g, j2_4[0].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans2 = trans2.replace(/%4\$s/g, j2_4[1].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
trans3 = trans3.replace(/%4\$s/g, j2_4[2].replace(/%s/g, '\ue124').replace(/\$s/g, '\ue125'))
}
}
2024-07-16 13:22:34 -04:00
out[0] += trans.replace(/%([0-9]*\$){0,1}s/g, '').replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
out[1] += trans2.replace(/%([0-9]*\$){0,1}s/g, '').replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
2024-07-06 11:02:11 -04:00
out[2] += trans3.replace(/%([0-9]*\$){0,1}s/g, '').replace(/\ue123/g, '%').replace(/\ue124/g, '%s').replace(/\ue125/g, '$s')
}
if(data.extra){
for(const i in data.extra){
2024-07-22 00:20:36 -04:00
parsed=parse(data.extra[i], l, data.color?[consoleColors[data.color],""]:resetColor)
2024-07-06 11:02:11 -04:00
out[0]+=parsed[0];
out[1]+=parsed[1];
out[2]+=parsed[2];
}
}
2024-07-16 18:23:48 -04:00
out[0]+=resetColor[0];
2024-07-06 11:02:11 -04:00
return out;
}
2024-07-19 14:12:40 -04:00
const parse2=function(_data, l, resetColor){
try{
return parse(_data)
} catch(e){
console.error(e)
return [
2024-07-22 04:07:23 -04:00
"\x1B[0m\x1B[38;2;255;85;85mAn error occured while parsing a message. See console for more information.\nJSON that caused the error: "+JSON.stringify(_data),
"An error occured while parsing a message. See console for more information. JSON that caused the error: "+JSON.stringify(_data),
"§cAn error occured while parsing a message. See console for more information. JSON that caused the error: "+JSON.stringify(_data)
2024-07-19 14:12:40 -04:00
]
}
}
module.exports = parse2