owobot/plugins/chat.js

156 lines
5 KiB
JavaScript
Raw Normal View History

2024-08-12 05:13:32 -04:00
const settings = require('../settings.json')
const parsePlain = require('../util/chatparse_plain.js')
const parseConsole = require('../util/chatparse_console.js')
const parse1204 = require('../util/parseNBT.js')
2024-08-25 22:13:46 -04:00
const { getMessage } = require('../util/lang.js')
2024-08-22 15:24:23 -04:00
const convertChatStyleItem = (item) => {
2024-08-24 10:15:39 -04:00
const output = {}
for (const i in item) {
2024-08-22 15:24:23 -04:00
output[i] = item[i].value
}
2024-08-24 10:15:39 -04:00
return output
2024-08-22 15:24:23 -04:00
}
const convertChatTypeItem = (item) => {
2024-08-24 10:15:39 -04:00
if (item.style) {
2024-08-22 15:24:23 -04:00
return {
translation_key: item.translation_key.value,
parameters: item.parameters.value.value,
style: convertChatStyleItem(item.style.value)
}
} else {
return {
translation_key: item.translation_key.value,
parameters: item.parameters.value.value,
style: {}
}
}
}
module.exports = {
2024-08-12 05:13:32 -04:00
load: (b) => {
2024-08-25 22:29:39 -04:00
b.messageCount = 0
b.chatDisabledUntil = 0
b.interval.antiSpam = setInterval(() => {
b.messageCount = 0
2024-08-25 22:13:46 -04:00
}, 4000)
2024-08-24 10:15:39 -04:00
b.messageTypes = []
b._client.on('registry_data', (data) => {
if (data.codec.value['minecraft:chat_type']) {
2024-08-22 15:24:23 -04:00
b.messageTypes = data.codec.value['minecraft:chat_type']
2024-08-24 10:15:39 -04:00
const nbtItems = data.codec.value['minecraft:chat_type'].value.value.value.value
2024-08-22 15:24:23 -04:00
nbtItems.forEach((item, i) => {
b.messageTypes[i] = convertChatTypeItem(item.element.value.chat.value)
})
}
})
b._client.on('profileless_chat', (data) => {
2024-09-19 22:12:30 -04:00
let messageType = b.messageTypes[data.type]
2024-09-19 22:21:01 -04:00
if (messageType === undefined) messageType = { translation_key: '%s', parameters: ['content'] }
2024-08-24 10:15:39 -04:00
const json = { translate: messageType.translation_key, with: [] }
messageType.parameters.forEach((item, i) => {
if (item === 'content') {
2024-08-22 15:24:23 -04:00
json.with[i] = parse1204(data.message)
2024-08-24 10:15:39 -04:00
} else if (item === 'sender') {
2024-08-22 15:24:23 -04:00
json.with[i] = parse1204(data.name)
2024-08-24 10:15:39 -04:00
} else if (item === 'target') {
2024-08-22 15:24:23 -04:00
json.with[i] = parse1204(data.target)
}
})
2024-08-24 10:15:39 -04:00
for (const i in messageType.style) {
2024-08-22 15:24:23 -04:00
json[i] = messageType.style[i]
}
2024-09-19 22:21:01 -04:00
const message = parsePlain(parse1204(data.message))
const uuid = b.findUUID(parsePlain(parse1204(data.name)))
const nickname = b.findDisplayName(uuid)
const username = parsePlain(parse1204(data.name))
b.emit('chat_unparsed', {
2024-08-24 10:15:39 -04:00
json,
2024-08-22 15:24:23 -04:00
type: 'profileless',
uuid,
message,
nickname,
username,
playerChatType: messageType
2024-08-22 15:24:23 -04:00
})
})
2024-07-27 02:39:18 -04:00
b._client.on('player_chat', (data) => {
2024-09-19 22:12:30 -04:00
let messageType = b.messageTypes[data.type]
2024-09-19 22:21:01 -04:00
if (messageType === undefined) messageType = { translation_key: '%s', parameters: ['content'] }
2024-08-24 10:15:39 -04:00
const json = { translate: messageType.translation_key, with: [] }
messageType.parameters.forEach((item, i) => {
if (item === 'content') {
if (messageType.translation_key === '%s') {
2024-08-22 15:24:23 -04:00
json.with[i] = parse1204(data.unsignedChatContent)
} else {
json.with[i] = data.plainMessage
}
2024-08-24 10:15:39 -04:00
} else if (item === 'sender') {
2024-08-22 15:24:23 -04:00
json.with[i] = parse1204(data.networkName)
2024-08-24 10:15:39 -04:00
} else if (item === 'target') {
2024-08-22 15:24:23 -04:00
json.with[i] = parse1204(data.networkTargetName)
}
})
2024-08-24 10:15:39 -04:00
for (const i in messageType.style) {
2024-08-22 15:24:23 -04:00
json[i] = messageType.style[i]
}
b.emit('chat_unparsed', {
2024-08-24 10:15:39 -04:00
json,
2024-08-22 15:24:23 -04:00
type: 'player',
uuid: data.senderUuid,
message: data.plainMessage,
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid),
playerChatType: messageType
2024-08-22 15:24:23 -04:00
})
})
2024-08-12 05:13:32 -04:00
b._client.on('system_chat', (data) => {
const json = parse1204(data.content)
b.emit('chat_unparsed', {
json,
type: 'system',
2024-09-19 22:21:01 -04:00
uuid: '00000000-0000-0000-0000-000000000000',
message: '',
nickname: '',
username: '',
playerChatType: {}
})
})
2024-08-12 05:13:32 -04:00
b._client.on('chat', (data) => { // Legacy chat for versions <1.19
const json = parse1204(data.message)
let nickname
2024-08-12 05:13:32 -04:00
let username
let message
let uuid
if (data.uuid) uuid = data.uuid
b.emit('chat_unparsed', {
json,
type: 'legacy',
uuid,
message,
nickname,
username,
playerChatType: {}
})
})
2024-08-12 05:13:32 -04:00
b.on('chat', (data) => {
2024-08-25 22:29:39 -04:00
b.messageCount++
if (Date.now() < b.chatDisabledUntil) return
if (b.messageCount >= 100) {
b.info(getMessage(settings.defaultLang, 'chat.antiSpamTriggered'))
2024-08-25 22:13:46 -04:00
b.chatDisabledUntil = Date.now() + 30000
return
}
2024-08-12 05:13:32 -04:00
const msgConsole = parseConsole(data.json)
const msgPlain = parsePlain(data.json)
if (settings.logJSONmessages) console.log(data.json)
if (msgPlain.endsWith('\n\n\n\n\nThe chat has been cleared')) return
if (msgPlain.startsWith('Command set: ')) return
b.emit('plainchat', msgPlain, data.type, data.subtype)
b.displayChat(data.type, data.subtype, `${msgConsole}\x1b[0m`)
})
2024-08-12 05:13:32 -04:00
}
2024-07-27 02:39:18 -04:00
}