owobot/plugins/chat.js

240 lines
8 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-08-22 15:24:23 -04:00
const messageType = b.messageTypes[data.type]
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-08-24 10:15:39 -04:00
let username = ''
let nickname = ''
let uuid = '00000000-0000-0000-0000-000000000000'
let message = ''
if (messageType.translation_key === '%s') {
2024-08-12 05:13:32 -04:00
const parsed = parsePlain(json)
const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0]
const chatNameSplit = chatName.split(' ')
2024-08-22 15:24:23 -04:00
nickname = chatNameSplit[chatNameSplit.length - 1]
username = b.findRealName(chatName)
uuid = b.findUUID(username)
2024-08-24 10:15:39 -04:00
message = split.join(': ')
} else {
2024-08-22 15:24:23 -04:00
message = parsePlain(parse1204(data.message))
uuid = b.findUUID(parsePlain(parse1204(data.name)))
nickname = b.findDisplayName(uuid)
username = parsePlain(parse1204(data.name))
}
2024-08-22 15:24:23 -04:00
b.emit('chat', {
2024-08-24 10:15:39 -04:00
json,
2024-08-22 15:24:23 -04:00
type: 'profileless',
uuid,
message,
nickname,
username
})
})
2024-07-27 02:39:18 -04:00
b._client.on('player_chat', (data) => {
2024-08-22 15:24:23 -04:00
const messageType = b.messageTypes[data.type]
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]
}
2024-08-22 15:24:23 -04:00
b.emit('chat', {
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)
})
})
2024-08-12 05:13:32 -04:00
b._client.on('system_chat', (data) => {
const json = parse1204(data.content)
2024-09-12 00:26:36 -04:00
if (json.translate === '%s %s %s') { // ChipmunkMod format
if (json.with && json.with[1] && json.with[2]) {
2024-09-02 15:52:27 -04:00
const username = parsePlain(json.with[1])
const uuid = b.findUUID(username)
const nickname = b.findDisplayName(uuid)
const message = parsePlain(json.with[2].extra)
b.emit('chat', {
json,
type: 'system',
uuid,
message,
nickname,
username
})
} else {
b.emit('chat', {
json,
type: 'system',
2024-09-12 00:26:36 -04:00
uuid: '00000000-0000-0000-0000-000000000000',
message: '',
nickname: '',
username: ''
2024-09-02 15:52:27 -04:00
})
}
2024-09-12 00:26:36 -04:00
} else if (json.extra && json.extra[4] && json.extra[3] && json.extra[5] && json.extra[4].text === ' » ') { // ChipmunkMod format - m_c_player
2024-09-02 22:32:35 -04:00
const username = parsePlain(json.extra[3])
const uuid = b.findUUID(username)
const nickname = b.findDisplayName(uuid)
const message = parsePlain(json.extra[5])
b.emit('chat', {
json,
type: 'system',
uuid,
message,
nickname,
username
})
2024-09-02 15:52:27 -04:00
} else { // Generic system chat format
const parsed = parsePlain(json)
const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0]
const chatNameSplit = chatName.split(' ')
const nickname = chatNameSplit[chatNameSplit.length - 1]
const username = b.findRealName(chatName)
const uuid = b.findUUID(username)
b.emit('chat', {
json,
type: 'system',
uuid,
message: split.join(': '),
nickname,
username
})
}
})
2024-08-12 05:13:32 -04:00
b._client.on('chat', (data) => { // Legacy chat for versions <1.19
const json = parse1204(data.message)
2024-08-12 05:13:32 -04:00
const parsed = parsePlain(json)
let chatName
let nickname
2024-08-12 05:13:32 -04:00
let username
let message
let uuid
2024-09-12 00:26:36 -04:00
if (json.translate === '%s %s %s') { // ChipmunkMod format
if (json.with && json.with[1] && json.with[2]) {
2024-09-02 15:52:27 -04:00
username = parsePlain(json.with[1])
uuid = b.findUUID(username)
nickname = b.findDisplayName(uuid)
message = parsePlain(json.with[2].extra)
}
2024-09-12 00:26:36 -04:00
} else if (json.extra && json.extra[4] && json.extra[3] && json.extra[5] && json.extra[4].text === ' » ') { // ChipmunkMod format - m_c_player
2024-09-02 22:32:35 -04:00
username = parsePlain(json.extra[3])
uuid = b.findUUID(username)
nickname = b.findDisplayName(uuid)
message = parsePlain(json.extra[5])
2024-09-12 00:26:36 -04:00
} else if (b.host.options.isVanilla && json.translate === 'chat.type.text') { // Servers without Extras chat
2024-08-12 05:13:32 -04:00
if (json.with && json.with.length >= 2) {
message = parsePlain(json.with[1])
username = parsePlain(json.with[0])
}
uuid = b.findUUID(username)
} else { // Servers with Extras chat, such as Kaboom
const split = parsed.split(': ')
chatName = split.splice(0, 1)[0]
const chatNameSplit = chatName.split(' ')
nickname = chatNameSplit[chatNameSplit.length - 1]
2024-08-12 05:13:32 -04:00
username = b.findRealName(chatName)
uuid = b.findUUID(username)
message = split.join(': ')
2024-08-12 05:13:32 -04:00
}
if (data.uuid) uuid = data.uuid
b.emit('chat', {
json,
type: 'legacy',
uuid,
message,
nickname,
username
})
})
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)
2024-08-12 05:13:32 -04:00
b.displayChat(data.type, `${msgConsole}\x1b[0m`)
})
2024-08-12 05:13:32 -04:00
}
2024-07-27 02:39:18 -04:00
}