chipmunkbot3/plugins/chat-filter.js
2024-02-29 22:58:11 -05:00

64 lines
1.7 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) {
// const mcData = require('minecraft-data')(bot._client.version)
// const { id } = mcData.itemsByName.cod
bot.chatFilter = {
enabled: false,
_lines: Array(100).fill(''),
_filter
}
bot.on('chat', ({ raw }) => {
const filtered = _filter(raw)
bot.chatFilter._lines = [...bot.chatFilter._lines, ...filtered.split('\n')]
while (bot.chatFilter._lines.length > 99) {
bot.chatFilter._lines.shift()
}
if (raw !== filtered) {
bot._client.write('set_creative_slot', {
slot: 36,
item: {
present: true,
itemId: /* id */1,
itemCount: 1,
nbtData: nbt.comp({
i: nbt.string('\xa7r' + bot.chatFilter._lines.join('\xa7r\n'))
})
}
})
setTimeout(() => {
bot.core.run('minecraft:tellraw @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