owobot/plugins/!chat.js

149 lines
5.1 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')
const messageTypes = [
'',
'chat.type.emote',
'commands.message.display.incoming',
2024-08-12 05:13:32 -04:00
'commands.message.display.outgoing',
'',
'chat.type.announcement',
2024-08-12 05:13:32 -04:00
'chat.type.team.text',
'chat.type.team.sent'
2024-07-27 02:39:18 -04:00
]
module.exports = {
2024-08-12 05:13:32 -04:00
load: (b) => {
b._client.on('profileless_chat', (data) => {
if (data.type === 4) {
const json = parse1204(data.message)
2024-08-12 05:13:32 -04:00
const parsed = parsePlain(json)
const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0]
const username = b.findRealName(chatName)
const uuid = b.findUUID(username)
b.emit('chat', { json, type: 'profileless', uuid, message: split.join(': '), username })
2024-08-12 05:13:32 -04:00
} else if (data.type === 6 || data.type === 7) {
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.target),
parse1204(data.name),
data.message
]
},
type: 'profileless',
uuid: data.senderUuid,
message: parsePlain(data.message),
username: parsePlain(parse1204(data.name))
})
} else {
b.emit('chat', {
json: {
translate: messageTypes[data.type],
2024-08-12 05:13:32 -04:00
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.name),
parse1204(data.message)
]
},
type: 'profileless',
uuid: '00000000-0000-0000-0000-000000000000',
2024-08-12 05:13:32 -04:00
message: parsePlain(parse1204(data.message)),
username: parsePlain(parse1204(data.name))
2024-07-27 02:39:18 -04:00
})
}
})
2024-07-27 02:39:18 -04:00
b._client.on('player_chat', (data) => {
if (data.type === 4) {
2024-08-12 05:13:32 -04:00
b.emit('chat', { json: parse1204(data.unsignedChatContent), type: 'player', uuid: data.senderUuid, message: data.plainMessage, username: parsePlain(parse1204(data.networkName)) })
} else if (data.type === 6 || data.type === 7) {
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: data.type === 2 ? 'gray' : 'reset',
with: [
2024-08-12 05:13:32 -04:00
parse1204(data.networkTargetName),
parse1204(data.networkName),
data.plainMessage
]
},
type: 'player',
uuid: data.senderUuid,
2024-08-12 05:13:32 -04:00
message: parsePlain(data.plainMessage),
username: parsePlain(parse1204(data.networkName))
})
} else {
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.networkName),
data.plainMessage
]
},
type: 'player',
uuid: data.senderUuid,
message: parsePlain(data.plainMessage),
username: parsePlain(parse1204(data.networkName))
2024-07-27 02:39:18 -04:00
})
}
})
2024-08-12 05:13:32 -04:00
b._client.on('system_chat', (data) => {
const json = parse1204(data.content)
2024-08-12 05:13:32 -04:00
const parsed = parsePlain(json)
const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0]
const username = b.findRealName(chatName)
const uuid = b.findUUID(username)
b.emit('chat', { json, type: 'system', uuid, message: split.join(': '), username })
})
2024-08-12 05:13:32 -04:00
b._client.on('chat', (data) => { // Legacy chat
const json = parse1204(data.message)
2024-08-12 05:13:32 -04:00
const parsed = parsePlain(json)
let chatName
let username
let message
let uuid
if (b.host.options.isVanilla && json.translate === 'chat.type.text') { // Servers without Extras chat
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(': ')
message = split.join(': ')
uuid = b.findUUID(username)
chatName = split.splice(0, 1)[0]
username = b.findRealName(chatName)
}
b.emit('chat', { json, type: 'legacy', uuid: data.uuid ? data.uuid : uuid, message, username })
})
2024-08-12 05:13:32 -04:00
b.on('chat', (data) => {
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)
b.displayChat(data.type, `${msgConsole}\x1b[0m`)
2024-08-12 05:13:32 -04:00
const fullCommand = data.message
for (const i in b.prefix) {
if (fullCommand.startsWith(b.prefix[i])) {
const command = fullCommand.slice(b.prefix[i].length)
b.runCommand(data.username, data.uuid, command, b.prefix[i])
}
}
})
2024-08-12 05:13:32 -04:00
}
2024-07-27 02:39:18 -04:00
}