This commit is contained in:
Yaode_owo 2024-07-24 12:50:18 -04:00
parent a62368bd9f
commit c6e31979a6
2 changed files with 72 additions and 3 deletions

View file

@ -1,10 +1,25 @@
const lang = require("./en_us.json"); // translate message
const regex = /^(.+):\s*\/(.+)$/;
function parseCommand(message) {
const match = message.match(regex);
if (match) {
const username = match[1];
const command = match[2];
return { username, command };
} else {
return undefined;
}
}
function inject(bot) {
bot.on('systemChat', (packet) => {
//console.log(packet);
msg = parseMinecraftMessage(packet.formattedMessage)
if (msg !== undefined) bot.emit('custom_systemChat', msg);
msg = parseMinecraftMessage(packet.formattedMessage);
nocolormsg = parseMinecraftMessageNoColor(packet.formattedMessage);
cspy = parseCommand(nocolormsg)
if (msg !== undefined) bot.emit('custom_systemChat', msg, nocolormsg);
if (cspy !== undefined) bot.emit('custom_commandspy', cspy);
});
bot.on('playerChat', (packet) => {
@ -137,6 +152,56 @@ function parseMinecraftMessage(component) {
return extractText(jsonComponent) + ansiCodes['reset'];
}
function parseMinecraftMessageNoColor(component) {
let jsonComponent;
try {
jsonComponent = JSON.parse(component);
} catch (e) {
console.error("Invalid JSON format:", component);
}
function extractText(comp) {
let text = '';
if (comp.text) {
text += comp.text;
}
if (comp[""]) {
text += extractText(comp[""]);
}
if (typeof comp === 'string' || typeof comp === 'number') {
text += comp;
}
if (comp.extra) {
comp.extra.forEach(subComp => {
text += extractText(subComp);
});
}
if (comp.translate) {
let translateString = lang[comp.translate] || comp.translate;
if (comp.with) {
const withArgs = comp.with.map(arg => extractText(arg));
withArgs.forEach((arg, index) => {
if (arg.length > 10000) return translateString = ''; // anti tellraw translate crash
translateString = translateString.replace('%s', arg);
const placeholder = new RegExp(`%${index + 1}\\$s`, 'g');
translateString = translateString.replace(placeholder, arg);
});
}
text += translateString;
}
return text;
}
return extractText(jsonComponent) + ansiCodes['reset'];
}
}
module.exports = inject;

View file

@ -15,10 +15,14 @@ bot.on('custom_playerChat', (msg) => {
console.log(`PlayerChat: ${msg}`);
});
bot.on('custom_systemChat', (msg) => { // SystemChat maybe is PlayerChat use sudo or vanish
bot.on('custom_systemChat', (msg, nocolormsg, cspy) => { // SystemChat maybe is PlayerChat use sudo or vanish
console.log(`SystemChat: ${msg}`);
});
bot.on('custom_commandspy', (cspy) => {
console.log(`CSPY: ${cspy.username}: /${cspy.command}`);
})
bot.on('error', (err) => {
console.error('Bot Error:', err);
});