Files
NitoriBot/src/modules/chat.js
T

146 lines
4.0 KiB
JavaScript
Raw Normal View History

2025-06-25 14:31:14 -04:00
const convertNbtComponentToJson = require('../util/nbt_parser')
/*
please note that the code here for 1.21.8
was ported from the FNFBoyfriendBot Reignite 1.21.8 port
and had SkiBot's console format added to it
*/
function parse (chatTypes, type, sender, target, message) {
const chatType = chatTypes[type]
const parsed = convertNbtComponentToJson(null, chatType.value.value.chat)
const json = { translate: parsed.translation_key, with: [] }
for (const parameter of parsed.parameters) {
switch (parameter) {
case "sender": json.with.push(sender)
break;
case "target": json.with.push(target)
case "content": json.with.push(message)
break;
}
}
return json
}
2024-12-17 20:10:39 -05:00
module.exports = {
data: {
description: "parses chat",
2024-12-17 20:10:39 -05:00
enabled: true,
name: "chat",
type: "logging"
},
inject (context) {
const bot = context.bot;
const config = context.config;
bot.on('packet.registry_data', (packet) => {
if (packet.id !== 'minecraft:chat_type') return // taken from FNFBoyfriendBot Reignite 1.21.8 port
chatTypes = packet.entries
2024-12-17 20:10:39 -05:00
})
bot.on('packet.player_chat', (packet) => {
const unsigned = convertNbtComponentToJson(null, packet.unsignedChatContent);
bot.emit('message', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Player Chat', color: 'dark_purple' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
{ text: packet.type.chatType, color: 'gold' },
],
},
unsigned
])
});
2024-12-17 20:10:39 -05:00
bot.on('packet.profileless_chat', (packet) => {
const sender = convertNbtComponentToJson(null, packet.name);
const message = convertNbtComponentToJson(null, packet.message);
const target = convertNbtComponentToJson(null, packet.target)
const type = packet.type.chatType;
const parsed = parse(chatTypes, type, sender, target, message)
bot.emit('message', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Profileless Chat', color: 'dark_aqua' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
{ text: packet.type.chatType, color: 'gold' },
],
},
parsed
])
2024-12-17 20:10:39 -05:00
});
bot.on('packet.system_chat', packet => {
2025-06-25 14:31:14 -04:00
try {
const message = convertNbtComponentToJson(null, packet.content)
2024-12-17 20:10:39 -05:00
if (message.translate === 'advMode.setCommand.success') return // Ignores command set message
if (message.translate === 'multiplayer.message_not_delivered') return;
if (packet.isActionBar) {
return
}
bot.emit('message', [
2024-12-17 20:10:39 -05:00
{
translate: "[%s] ",
color: "dark_gray",
with: [
{ text: 'System Chat', color: 'dark_blue' },
],
},
message
]);
bot.emit('system_chat_selfcare', message); // this is for selfcare
2025-06-25 14:31:14 -04:00
} catch (e) {
console.log(e.stack)
}
2024-12-17 20:10:39 -05:00
});
bot.chat = (args) => {
if (args.startsWith('/')) {
bot._client.write('chat_command', {
command: args?.substring(1).substring(0, 256),
timestamp: BigInt(Date.now()),
salt: 0n,
argumentSignatures: [],
signedPreview: false,
messageCount: 0,
acknowledged: Buffer.alloc(3),
previousMessages: []
})
return;
}
const acc = 0;
const bitset = Buffer.allocUnsafe(3);
bitset[0] = acc & 0xFF;
bitset[1] = (acc >> 8) & 0xFF;
bitset[2] = (acc >> 16) & 0xFF;
bot._client.write('chat_message', {
message: args?.substring(0, 256),
timestamp: BigInt(Date.now()),
salt: 0n,
offset: 0,
acknowledged: bitset
})
}
2024-12-17 20:30:34 -05:00
bot.tellraw = (selector, message) => {
bot.core.run(`minecraft:tellraw ${selector} ${JSON.stringify(message)}`);
}
2024-12-17 20:10:39 -05:00
}
}