From c6e31979a633ea556b7e7f7c58c9bcb1e999ede9 Mon Sep 17 00:00:00 2001 From: Yaode_owo Date: Wed, 24 Jul 2024 12:50:18 -0400 Subject: [PATCH] add cspy --- main/emit.js | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++-- main/main.js | 6 ++++- 2 files changed, 72 insertions(+), 3 deletions(-) diff --git a/main/emit.js b/main/emit.js index f31b14b..4f3521b 100644 --- a/main/emit.js +++ b/main/emit.js @@ -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; \ No newline at end of file diff --git a/main/main.js b/main/main.js index 7355805..7633288 100644 --- a/main/main.js +++ b/main/main.js @@ -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); });