chipmunkbot-archive/plugins/chat.js
2023-06-26 23:04:54 -04:00

51 lines
No EOL
1.7 KiB
JavaScript

const parseText = require('./../util/text_parser.js')
function inject (bot) {
bot.chatQueue = bot.chatQueue ?? []
setInterval(() => {
if (!bot.loggedIn)
return
const message = bot.chatQueue.shift()
if (message != null) {
bot._client.write('chat', { message })
}
}, 200)
bot._client.on('chat', (packet) => {
const message = parseText(packet.message)
bot.emit('chat', message, packet)
})
bot.on('chat', (message, packet) => {
if (!/Command set: .*/.test(message.clean)) console.log(`[${bot.host}] ${message.ansi}\x1b[0m`)
let msg = message.raw;
if (msg.match(/<.*§r> §r.*/g)) {
if (packet.sender === '00000000-0000-0000-0000-000000000000') return
const player = bot.players[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[packet.sender]
const message = msg.split('> ')[1]
bot.emit("message", player, message)
} else if (msg.match(/.* .*§r: §.*/g)) {
if (packet.sender === '00000000-0000-0000-0000-000000000000') return
const player = bot.players[packet.sender]
const message = msg.split('§r: ')[1].substr(2)
bot.emit("message", bot.players[packet.sender], 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[username]
if (player == null) return
const command = msg.split('§b: ')[1]
bot.emit("cspy", player, command)
}
})
}
module.exports = inject