owobot/plugins/!chat.js

193 lines
6.2 KiB
JavaScript
Raw Normal View History

2024-08-12 04:33:43 -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',
'commands.message.display.outgoing',
'',
'chat.type.announcement',
'chat.type.team.text',
'chat.type.team.sent'
]
module.exports = {
load: (b) => {
b._client.on('profileless_chat', (data) => {
if (data.type === 4) {
const json = parse1204(data.message)
const parsed = parsePlain(json)
const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0]
2024-08-18 03:18:09 -04:00
const chatNameSplit = chatName.split(' ')
const nickname = chatNameSplit[chatNameSplit.length - 1]
2024-08-12 04:33:43 -04:00
const username = b.findRealName(chatName)
const uuid = b.findUUID(username)
2024-08-17 07:31:18 -04:00
b.emit('chat', {
json,
type: 'profileless',
uuid,
message: split.join(': '),
nickname,
username
})
2024-08-12 04:33:43 -04:00
} else if (data.type === 6 || data.type === 7) {
2024-08-17 07:31:18 -04:00
const uuid = b.findUUID(parsePlain(parse1204(data.name)))
const nickname = b.findDisplayName(uuid)
2024-08-12 04:33:43 -04:00
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',
2024-08-17 07:31:18 -04:00
uuid,
2024-08-12 04:33:43 -04:00
message: parsePlain(data.message),
2024-08-17 07:31:18 -04:00
nickname,
2024-08-12 04:33:43 -04:00
username: parsePlain(parse1204(data.name))
})
} else {
2024-08-17 07:31:18 -04:00
const uuid = b.findUUID(parsePlain(parse1204(data.name)))
const nickname = b.findDisplayName(uuid)
2024-08-12 04:33:43 -04:00
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: (data.type === 2 || data.type === 3) ? 'gray' : 'reset',
with: [
parse1204(data.name),
parse1204(data.message)
]
},
type: 'profileless',
2024-08-17 07:31:18 -04:00
uuid,
2024-08-12 04:33:43 -04:00
message: parsePlain(parse1204(data.message)),
2024-08-17 07:31:18 -04:00
nickname,
2024-08-12 04:33:43 -04:00
username: parsePlain(parse1204(data.name))
})
}
})
b._client.on('player_chat', (data) => {
if (data.type === 4) {
2024-08-17 07:31:18 -04:00
b.emit('chat', {
json: parse1204(data.unsignedChatContent),
2024-08-18 03:18:09 -04:00
type: 'player',
uuid: data.senderUuid,
2024-08-17 07:31:18 -04:00
message: data.plainMessage,
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid)
})
2024-08-12 04:33:43 -04:00
} else if (data.type === 6 || data.type === 7) {
b.emit('chat', {
json: {
translate: messageTypes[data.type],
color: data.type === 2 ? 'gray' : 'reset',
with: [
parse1204(data.networkTargetName),
parse1204(data.networkName),
data.plainMessage
]
},
type: 'player',
uuid: data.senderUuid,
message: parsePlain(data.plainMessage),
2024-08-17 07:31:18 -04:00
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid)
2024-08-12 04:33:43 -04:00
})
} 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),
2024-08-17 07:31:18 -04:00
nickname: parsePlain(parse1204(data.networkName)),
username: b.findRealNameFromUUID(data.senderUuid)
2024-08-12 04:33:43 -04:00
})
}
})
b._client.on('system_chat', (data) => {
const json = parse1204(data.content)
const parsed = parsePlain(json)
const split = parsed.split(': ')
const chatName = split.splice(0, 1)[0]
2024-08-18 03:18:09 -04:00
const chatNameSplit = chatName.split(' ')
const nickname = chatNameSplit[chatNameSplit.length - 1]
2024-08-12 04:33:43 -04:00
const username = b.findRealName(chatName)
const uuid = b.findUUID(username)
2024-08-17 07:31:18 -04:00
b.emit('chat', {
json,
type: 'system',
2024-08-17 07:50:30 -04:00
uuid,
message: split.join(': '),
2024-08-17 07:31:18 -04:00
nickname,
username
})
2024-08-12 04:33:43 -04:00
})
2024-08-18 03:17:47 -04:00
b._client.on('chat', (data) => { // Legacy chat for versions <1.19
2024-08-12 04:33:43 -04:00
const json = parse1204(data.message)
const parsed = parsePlain(json)
let chatName
2024-08-17 07:50:30 -04:00
let nickname
2024-08-12 04:33:43 -04:00
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(': ')
chatName = split.splice(0, 1)[0]
2024-08-18 03:18:09 -04:00
const chatNameSplit = chatName.split(' ')
nickname = chatNameSplit[chatNameSplit.length - 1]
2024-08-12 04:33:43 -04:00
username = b.findRealName(chatName)
2024-08-17 07:00:33 -04:00
uuid = b.findUUID(username)
message = split.join(': ')
2024-08-12 04:33:43 -04:00
}
2024-08-18 03:18:09 -04:00
if (data.uuid) uuid = data.uuid
2024-08-17 07:38:04 -04:00
b.emit('chat', {
json,
type: 'legacy',
2024-08-18 03:18:09 -04:00
uuid,
2024-08-17 07:38:04 -04:00
message,
2024-08-17 07:50:30 -04:00
nickname,
2024-08-17 07:38:04 -04:00
username
})
2024-08-12 04:33:43 -04:00
})
b.on('chat', (data) => {
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
2024-08-15 05:36:13 -04:00
b.emit('plainchat', msgPlain, data.type)
2024-08-12 04:33:43 -04:00
b.displayChat(data.type, `${msgConsole}\x1b[0m`)
const fullCommand = data.message
for (const i in b.prefix) {
if (fullCommand.startsWith(b.prefix[i])) {
const command = fullCommand.slice(b.prefix[i].length)
2024-08-17 07:38:04 -04:00
b.runCommand(data.username, data.nickname, data.uuid, command, data.type, b.prefix[i])
2024-08-12 04:33:43 -04:00
}
}
})
}
}