chipmunkbot3/plugins/chat.js

135 lines
4.1 KiB
JavaScript
Raw Normal View History

const { parseText } = require('../util/chat/utility')
const plainStringify = require('../util/chat/stringify/plain')
const colorCodeStringify = require('../util/chat/stringify/color_code')
const htmlStringify = require('../util/chat/stringify/html')
const ansiStringify = require('../util/chat/stringify/ansi')
const vanillaParser = require('../util/chat/message_parser/vanilla')
const kaboomParser = require('../util/chat/message_parser/kaboom')
const kaboomVanillaLikeParser = require('../util/chat/message_parser/kaboom_vanilla_like')
2024-02-11 21:23:41 -05:00
2024-02-29 20:39:21 -05:00
function inject (bot) {
2024-02-11 21:23:41 -05:00
bot.chat = {
queue: [],
parsers: [vanillaParser, kaboomParser, kaboomVanillaLikeParser],
2024-02-12 21:54:29 -05:00
message (message) {
if (!bot.supportFeature('signedChat')) {
bot._client.write('chat', { message })
return
}
2024-02-12 21:54:29 -05:00
bot._client.write('chat_message', {
message,
timestamp: BigInt(Date.now()),
salt: 0n,
offset: 0,
2024-03-01 22:41:57 -05:00
acknowledged: Buffer.allocUnsafe(3)
2024-02-12 21:54:29 -05:00
})
},
command (command) {
if (!bot.supportFeature('signedChat')) {
bot._client.write('chat', { message: '/' + command })
return
}
2024-02-12 21:54:29 -05:00
bot._client.write('chat_command', {
command,
timestamp: BigInt(Date.now()),
salt: 0n,
argumentSignatures: [],
signedPreview: false,
messageCount: 0,
acknowledged: Buffer.alloc(3),
previousMessages: []
})
}
2024-02-11 21:23:41 -05:00
}
2024-02-12 21:54:29 -05:00
2024-02-11 21:23:41 -05:00
setInterval(() => {
if (!bot.loggedIn) return
2024-02-11 21:23:41 -05:00
const message = bot.chat.queue.shift()
if (message != null) {
2024-02-12 21:54:29 -05:00
if (message[0] === '/') bot.chat.command(message.substring(1))
else bot.chat.message(message)
2024-02-11 21:23:41 -05:00
}
}, 200)
bot.on('packet.profileless_chat', (packet) => {
const message = parseText(packet.message)
const senderName = parseText(packet.name)
const type = bot.registry?.chatFormattingById[packet.type]
2024-02-11 21:23:41 -05:00
bot.emit('profileless_chat', { message, senderName, type })
bot.emit('chat', message)
tryParsingMessage(message, { senderName, players: bot.players, lang: bot.registry.language })
2024-02-11 21:23:41 -05:00
})
bot.on('packet.player_chat', (packet) => {
const plain = packet.plainMessage
const unsigned = parseText(packet.unsignedChatContent)
2024-04-02 17:53:10 -04:00
let sender = bot.players.find(player => player.uuid === packet.senderUuid)
const type = bot.registry?.chatFormattingById[packet.type]
2024-02-12 21:54:29 -05:00
2024-04-02 17:53:10 -04:00
if (!sender) {
bot.console.warn(`Unable to find player_chat sender in player list (uuid: ${packet.senderUuid})`)
sender = undefined
}
bot.emit('player_chat', { plain, unsigned, sender, type: type.name })
bot.emit('chat', unsigned)
2024-04-02 17:53:10 -04:00
tryParsingMessage(unsigned, { senderUuid: sender?.uuid, players: bot.players, lang: bot.registry.language, plain })
2024-02-12 21:54:29 -05:00
})
2024-02-29 20:39:21 -05:00
bot.on('packet.system_chat', (packet) => {
if (packet.isActionBar) return
const message = parseText(packet.content)
if (message?.translate === 'advMode.setCommand.success') return
2024-02-11 21:23:41 -05:00
bot.emit('system_chat', message)
bot.emit('chat', message)
2024-02-11 21:23:41 -05:00
})
// legacy (1.18.2 and older)
bot.on('packet.chat', packet => {
const message = parseText(packet.message)
bot.emit('chat', message) // if only it were this easy nowadays...
switch (packet.position) {
case 0: tryParsingMessage(message, { senderUuid: packet.sender, players: bot.players, lang: bot.registry.language }); break
case 1: bot.emit('system_chat', message); break
// TODO: 2
}
})
bot.on('chat', message => {
const stringifyOptions = { lang: bot.registry.language }
bot.emit('chat_plain', plainStringify(message, stringifyOptions))
bot.emit('chat_color_code', colorCodeStringify(message, stringifyOptions))
bot.emit('chat_ansi', ansiStringify(message, stringifyOptions))
bot.emit('chat_html', htmlStringify(message, stringifyOptions))
bot.console.log(message)
})
function tryParsingMessage (message, data) {
let parsed
for (const parser of bot.chat.parsers) {
parsed = parser(message, data)
if (parsed) break
}
if (!parsed) return
bot.emit('message', parsed)
}
2024-02-11 21:23:41 -05:00
}
2024-02-29 20:39:21 -05:00
module.exports = inject