chipmunkbot3/plugins/chat-filter.js
Chipmunk 5f3560910b Refactor the chat system
not sure if it's better or worse now. also, i have tried to optimize color codes, but this optimization seems unreliable (i might fix or remove it in the future)
2024-03-17 23:52:58 -04:00

62 lines
1.6 KiB
JavaScript

const nbt = require('prismarine-nbt')
// const SNBT = require('../util/snbt.js')
// const toNBTUUID = require('../util/uuid-to-nbt-uuid.js')
// filter the chat
function inject (bot) {
bot.chatFilter = {
enabled: false,
_lines: Array(100).fill(''),
_filter
}
bot.on('chat_color_code', message => {
const filtered = _filter(message)
bot.chatFilter._lines = [...bot.chatFilter._lines, ...filtered.split('\n')]
while (bot.chatFilter._lines.length > 100) {
bot.chatFilter._lines.shift()
}
if (message !== filtered) {
bot._client.write('set_creative_slot', {
slot: 36,
item: {
present: true,
itemId: 1,
itemCount: 1,
nbtData: nbt.comp({
i: nbt.string('\xa7r' + bot.chatFilter._lines.join('\xa7r\n'))
})
}
})
setTimeout(() => {
bot.core.run('minecraft:tellmessage @a ' + JSON.stringify({ nbt: 'SelectedItem.tag.i', entity: bot.uuid }))
}, 50)
}
})
}
function _filter (message) {
let filtered = message
filtered = filtered.replace(/geese/g, censor)
let separatorIndex = filtered.indexOf('\xa7r:\xa7r \xa7')
if (separatorIndex !== -1) {
separatorIndex += '\xa7r:\xa7r \xa7'.length + 1
const sender = filtered.substring(0, separatorIndex)
let msg = filtered.substring(separatorIndex)
let modified
if (msg[0] === '>') { msg = '\xa7a' + msg; modified = true }
if (modified) filtered = sender + msg
}
return filtered
}
function censor (match) {
let c = ''
while (c.length < match.length) { c += '\u0d9e' }
return c
}
module.exports = inject