Files
NitoriBot/src/modules/chat.js
T

289 lines
8.2 KiB
JavaScript
Raw Normal View History

2025-06-25 14:31:14 -04:00
const convertNbtComponentToJson = require('../util/nbt_parser')
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.player_chat', (data) => {
2025-06-25 14:31:14 -04:00
const unsigned = convertNbtComponentToJson(null, data.unsignedChatContent)
2024-12-17 20:10:39 -05:00
try {
2025-06-25 14:31:14 -04:00
switch (data.type.registryIndex) {
case 4:
2024-12-17 20:10:39 -05:00
bot.emit('playerChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Player Chat', color: 'dark_purple' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: data.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: "commands.message.display.outgoing",
with: [
2025-06-25 14:31:14 -04:00
data.networkName.insertion,
2024-12-17 20:10:39 -05:00
data.plainMessage
],
color: "gray",
italic: true
}
])
break
2025-06-25 14:31:14 -04:00
case 3:
2024-12-17 20:10:39 -05:00
bot.emit('playerChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Player Chat', color: 'dark_purple' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: data.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: "commands.message.display.incoming",
with: [
2025-06-25 14:31:14 -04:00
data.networkName.insertion,
2024-12-17 20:10:39 -05:00
data.plainMessage
],
color: "gray",
italic: true
}
])
break
2025-06-25 14:31:14 -04:00
case 5:
2024-12-17 20:10:39 -05:00
bot.emit('playerChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Player Chat', color: 'dark_purple' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: data.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
2025-06-25 14:31:14 -04:00
unsigned
2024-12-17 20:10:39 -05:00
])
break;
2025-06-25 14:31:14 -04:00
case 6:
2024-12-17 20:10:39 -05:00
bot.emit('playerChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Player Chat', color: 'dark_purple' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: data.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: "chat.type.announcement",
with: [
2025-06-25 14:31:14 -04:00
data.networkName.insertion,
2024-12-17 20:10:39 -05:00
data.plainMessage
]
}
]);
break;
default:
console.warn('invalid chat type');
}
} catch (e) {
console.log(e.stack);
}
})
bot.on('packet.profileless_chat', (packet) => {
2025-06-25 14:31:14 -04:00
const message = convertNbtComponentToJson(null, packet.message)
const sender = convertNbtComponentToJson(null, packet.name)
try {
switch (packet.type.registryIndex) {
case 2:
2024-12-17 20:10:39 -05:00
bot?.emit('profilelessChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Profileless Chat', color: 'dark_aqua' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: packet.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: "chat.type.emote",
with: [
sender,
message
]
}
])
break
2025-06-25 14:31:14 -04:00
case 3:
2024-12-17 20:10:39 -05:00
bot.emit('profilelessChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Profileless Chat', color: 'dark_aqua' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: packet.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: "commands.message.display.incoming",
with: [
sender,
message
],
color: "gray",
italic: true
}
])
break
2025-06-25 14:31:14 -04:00
case 4:
2024-12-17 20:10:39 -05:00
bot.emit('profilelessChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Profileless Chat', color: 'dark_aqua' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: packet.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: "commands.message.display.outgoing",
with: [
sender,
message
],
color: "gray",
italic: true
}
])
break
2025-06-25 14:31:14 -04:00
case 5:
2024-12-17 20:10:39 -05:00
bot.emit('profilelessChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Profileless Chat', color: 'dark_aqua' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: packet.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
message
])
break
2025-06-25 14:31:14 -04:00
case 6:
2024-12-17 20:10:39 -05:00
bot.emit('profilelessChat', [
{
translate: "[%s %s %s%s %s] ",
color: "dark_gray",
with: [
{ text: 'Profileless Chat', color: 'dark_aqua' },
{ text: '|' },
{ text: 'Packet Type', color: "aqua" },
{ text: ':' },
2025-06-25 14:31:14 -04:00
{ text: packet.type.registryIndex, color: 'gold' },
2024-12-17 20:10:39 -05:00
],
},
{ translate: 'chat.type.announcement',
with: [
sender,
message
]
}
])
break
}
2025-06-25 14:31:14 -04:00
} catch (e) {
console.log(e.stack)
}
2024-12-17 20:10:39 -05:00
});
bot.on('packet.system_chat', packet => {
2025-06-25 14:31:14 -04:00
try {
// console.log(packet)
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;
// bot.emit('system_chat', { message, actionbar: packet.isActionBar })
2024-12-17 20:10:39 -05:00
if (packet.isActionBar) {
return
}
bot.emit('systemChat', [
{
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
}
}