chipmunkbot3/plugins/chat.js

98 lines
3.1 KiB
JavaScript

const parseText = require('./../util/text_parser.js')
const nbt = require('prismarine-nbt')
function inject (bot) {
bot.chat = {
queue: [],
patterns: [],
message (message) {
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,
timestamp: BigInt(Date.now()),
salt: 0n,
offset: 0,
acknowledged: bitset
})
},
command (command) {
bot._client.write('chat_command', {
command,
timestamp: BigInt(Date.now()),
salt: 0n,
argumentSignatures: [],
signedPreview: false,
messageCount: 0,
acknowledged: Buffer.alloc(3),
previousMessages: []
})
}
}
setInterval(() => {
if (!bot.loggedIn) { return }
const message = bot.chat.queue.shift()
if (message != null) {
if (message[0] === '/') bot.chat.command(message.substring(1))
else bot.chat.message(message)
}
}, 200)
bot.on('chat', (message, packet) => {
bot.console.log(`[${bot.host}] ${message.raw}`)
const msg = message.raw
if (msg.match(/<.*§r> §r.*/g)) {
if (packet.sender === '00000000-0000-0000-0000-000000000000') return
const player = bot.players.find(player => player.uuid === packet.sender)
const message = msg.split('§r> §r')[1]
bot.emit('message', player, message)
} else if (msg.match(/<.*> .*/g)) {
if (packet.sender === '00000000-0000-0000-0000-000000000000') return
const player = bot.players.find(player => player.uuid === packet.sender)
const message = msg.split('> ')[1]
bot.emit('message', player, message)
} else if (msg.match(/.* .*§r:§r §.*/g)) {
if (packet.sender === '00000000-0000-0000-0000-000000000000') return
const player = bot.players.find(player => player.uuid === packet.sender)
const message = msg.split('§r:§r ')[1].substring(2)
bot.emit('message', player, message)
} else if (msg.match(/§.*§b: \/.*/g)) {
let username = msg.split('§b: ')[0]
if (username.startsWith('§b')) { username = username.slice(2) }
const player = bot.players.find(player => player.username === username)
if (player == null) return
const command = msg.split('§b: ')[1]
bot.emit('cspy', player, command)
}
})
bot.on('packet.profileless_chat', (packet) => {
const message = parseText(nbt.simplify(packet.message))
bot.emit('chat', message, packet)
})
bot.on('packet.player_chat', (packet) => {
const message = packet.unsignedChatContent ? parseText(nbt.simplify(packet.unsignedChatContent)) : packet.plainMessage
bot.emit('chat', message, { sender: packet.senderUuid })
})
bot.on('packet.system_chat', (packet) => {
const json = nbt.simplify(packet.content)
if (json?.translate === 'advMode.setCommand.success') return
const message = parseText(json)
bot.emit('chat', message, packet)
})
}
module.exports = inject