Seperate the chat parsers from the chat file & add message subtypes to the command class
This commit is contained in:
parent
0516ee1797
commit
6245e8ef40
16 changed files with 227 additions and 119 deletions
|
@ -37,6 +37,7 @@ module.exports = {
|
|||
c.reply(reply('nickname', c.nickname))
|
||||
c.reply(reply('command', c.command))
|
||||
c.reply(reply('msgType', c.msgType))
|
||||
c.reply(reply('msgSubtype', c.msgSubtype))
|
||||
c.reply(reply('prefix', c.prefix))
|
||||
c.reply(reply('args', c.args.join(', ')))
|
||||
c.reply(reply('verify', c.verify.toString()))
|
||||
|
|
8
index.js
8
index.js
|
@ -79,8 +79,12 @@ const createBot = function createBot (host, oldId) {
|
|||
console.log(`[${bot.id}] [info] ${msg}`)
|
||||
}
|
||||
|
||||
bot.displayChat = (type, msg) => {
|
||||
console.log(`[${bot.id}] [${type}] ${msg}`)
|
||||
bot.displayChat = (type, subtype, msg) => {
|
||||
if(settings.displaySubtypesToConsole){
|
||||
console.log(`[${bot.id}] [${type}] [${subtype}] ${msg}`)
|
||||
} else {
|
||||
console.log(`[${bot.id}] [${type}] ${msg}`)
|
||||
}
|
||||
}
|
||||
|
||||
loadplug(bot.id)
|
||||
|
|
|
@ -86,6 +86,7 @@
|
|||
"command.test.nickname": "Nickname",
|
||||
"command.test.command": "Command",
|
||||
"command.test.msgType": "Message type",
|
||||
"command.test.msgSubtype": "Message subtype",
|
||||
"command.test.prefix": "Prefix",
|
||||
"command.test.args": "Arguments",
|
||||
"command.test.verify": "Permission level",
|
||||
|
|
133
plugins/chat.js
133
plugins/chat.js
|
@ -57,32 +57,18 @@ module.exports = {
|
|||
for (const i in messageType.style) {
|
||||
json[i] = messageType.style[i]
|
||||
}
|
||||
let username = ''
|
||||
let nickname = ''
|
||||
let uuid = '00000000-0000-0000-0000-000000000000'
|
||||
let message = ''
|
||||
if (messageType.translation_key === '%s') {
|
||||
const parsed = parsePlain(json)
|
||||
const split = parsed.split(': ')
|
||||
const chatName = split.splice(0, 1)[0]
|
||||
const chatNameSplit = chatName.split(' ')
|
||||
nickname = chatNameSplit[chatNameSplit.length - 1]
|
||||
username = b.findRealName(chatName)
|
||||
uuid = b.findUUID(username)
|
||||
message = split.join(': ')
|
||||
} else {
|
||||
message = parsePlain(parse1204(data.message))
|
||||
uuid = b.findUUID(parsePlain(parse1204(data.name)))
|
||||
nickname = b.findDisplayName(uuid)
|
||||
username = parsePlain(parse1204(data.name))
|
||||
}
|
||||
b.emit('chat', {
|
||||
message = parsePlain(parse1204(data.message))
|
||||
uuid = b.findUUID(parsePlain(parse1204(data.name)))
|
||||
nickname = b.findDisplayName(uuid)
|
||||
username = parsePlain(parse1204(data.name))
|
||||
b.emit('chat_unparsed', {
|
||||
json,
|
||||
type: 'profileless',
|
||||
uuid,
|
||||
message,
|
||||
nickname,
|
||||
username
|
||||
username,
|
||||
playerChatType: messageType
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -105,117 +91,46 @@ module.exports = {
|
|||
for (const i in messageType.style) {
|
||||
json[i] = messageType.style[i]
|
||||
}
|
||||
b.emit('chat', {
|
||||
b.emit('chat_unparsed', {
|
||||
json,
|
||||
type: 'player',
|
||||
uuid: data.senderUuid,
|
||||
message: data.plainMessage,
|
||||
nickname: parsePlain(parse1204(data.networkName)),
|
||||
username: b.findRealNameFromUUID(data.senderUuid)
|
||||
username: b.findRealNameFromUUID(data.senderUuid),
|
||||
playerChatType: messageType
|
||||
})
|
||||
})
|
||||
|
||||
b._client.on('system_chat', (data) => {
|
||||
const json = parse1204(data.content)
|
||||
if (json.translate === '%s %s › %s') { // ChipmunkMod format
|
||||
if (json.with && json.with[1] && json.with[2]) {
|
||||
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',
|
||||
uuid: '00000000-0000-0000-0000-000000000000',
|
||||
message: '',
|
||||
nickname: '',
|
||||
username: ''
|
||||
})
|
||||
}
|
||||
} else if (json.extra && json.extra[4] && json.extra[3] && json.extra[5] && json.extra[4].text === ' » ') { // ChipmunkMod format - m_c_player
|
||||
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
|
||||
})
|
||||
} 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
|
||||
})
|
||||
}
|
||||
b.emit('chat_unparsed', {
|
||||
json,
|
||||
type: 'system',
|
||||
uuid: "00000000-0000-0000-0000-000000000000",
|
||||
message: "",
|
||||
nickname: "",
|
||||
username: "",
|
||||
playerChatType: {}
|
||||
})
|
||||
})
|
||||
|
||||
b._client.on('chat', (data) => { // Legacy chat for versions <1.19
|
||||
const json = parse1204(data.message)
|
||||
const parsed = parsePlain(json)
|
||||
let chatName
|
||||
let nickname
|
||||
let username
|
||||
let message
|
||||
let uuid
|
||||
if (json.translate === '%s %s › %s') { // ChipmunkMod format
|
||||
if (json.with && json.with[1] && json.with[2]) {
|
||||
username = parsePlain(json.with[1])
|
||||
uuid = b.findUUID(username)
|
||||
nickname = b.findDisplayName(uuid)
|
||||
message = parsePlain(json.with[2].extra)
|
||||
}
|
||||
} else if (json.extra && json.extra[4] && json.extra[3] && json.extra[5] && json.extra[4].text === ' » ') { // ChipmunkMod format - m_c_player
|
||||
username = parsePlain(json.extra[3])
|
||||
uuid = b.findUUID(username)
|
||||
nickname = b.findDisplayName(uuid)
|
||||
message = parsePlain(json.extra[5])
|
||||
} else 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]
|
||||
const chatNameSplit = chatName.split(' ')
|
||||
nickname = chatNameSplit[chatNameSplit.length - 1]
|
||||
username = b.findRealName(chatName)
|
||||
uuid = b.findUUID(username)
|
||||
message = split.join(': ')
|
||||
}
|
||||
if (data.uuid) uuid = data.uuid
|
||||
b.emit('chat', {
|
||||
b.emit('chat_unparsed', {
|
||||
json,
|
||||
type: 'legacy',
|
||||
uuid,
|
||||
message,
|
||||
nickname,
|
||||
username
|
||||
username,
|
||||
playerChatType: {}
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -232,8 +147,8 @@ module.exports = {
|
|||
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)
|
||||
b.displayChat(data.type, `${msgConsole}\x1b[0m`)
|
||||
b.emit('plainchat', msgPlain, data.type, data.subtype)
|
||||
b.displayChat(data.type, data.subtype, `${msgConsole}\x1b[0m`)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
47
plugins/chat_cmm.js
Executable file
47
plugins/chat_cmm.js
Executable file
|
@ -0,0 +1,47 @@
|
|||
const parsePlain = require('../util/chatparse_plain.js')
|
||||
module.exports = {
|
||||
load: (b) => {
|
||||
b.on("chat_unparsed", data => {
|
||||
if(data.type == "system" || data.type == "legacy"){
|
||||
if(data.parsed) return
|
||||
if (data.json.translate === '%s %s › %s' || data.json.translate === '[%s] %s › %s') {
|
||||
let subtype = 'chipmunkmod_';
|
||||
if(data.json.translate === '%s %s › %s'){
|
||||
subtype += 'name3_'
|
||||
} else if(data.json.translate === '[%s] %s › %s'){
|
||||
subtype += 'chomens_'
|
||||
}
|
||||
subtype += `${data.type}`
|
||||
if (data.json.with && data.json.with[1] && data.json.with[2]) {
|
||||
const username = parsePlain(data.json.with[1])
|
||||
const uuid = b.findUUID(username)
|
||||
const nickname = b.findDisplayName(uuid)
|
||||
const message = parsePlain(data.json.with[2].extra)
|
||||
data.parsed = true
|
||||
b.emit('chat', {
|
||||
json: data.json,
|
||||
type: data.type,
|
||||
subtype: subtype,
|
||||
uuid,
|
||||
message,
|
||||
nickname,
|
||||
username
|
||||
})
|
||||
} else {
|
||||
subtype += "_invalid"
|
||||
b.emit('chat', {
|
||||
json: data.json,
|
||||
type: data.type,
|
||||
subtype: subtype,
|
||||
uuid: '00000000-0000-0000-0000-000000000000',
|
||||
message: '',
|
||||
nickname: '',
|
||||
username: ''
|
||||
})
|
||||
data.parsed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
25
plugins/chat_cmm_mcp.js
Executable file
25
plugins/chat_cmm_mcp.js
Executable file
|
@ -0,0 +1,25 @@
|
|||
const parsePlain = require('../util/chatparse_plain.js')
|
||||
module.exports = {
|
||||
load: (b) => {
|
||||
b.on("chat_unparsed", data => {
|
||||
if(data.type == "system" || data.type == "legacy"){
|
||||
if(data.parsed) return
|
||||
if (data.json.extra && data.json.extra[4] && data.json.extra[3] && data.json.extra[5] && data.json.extra[4].text === ' » ') { // ChipmunkMod format - m_c_player
|
||||
const username = parsePlain(data.json.extra[3])
|
||||
const uuid = b.findUUID(username)
|
||||
const nickname = b.findDisplayName(uuid)
|
||||
const message = parsePlain(data.json.extra[5])
|
||||
b.emit('chat', {
|
||||
json,
|
||||
type: data.type,
|
||||
subtype: `chipmunkmod_mcp_${data.type}`.
|
||||
uuid,
|
||||
message,
|
||||
nickname,
|
||||
username
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
29
plugins/chat_extras_profileless.js
Executable file
29
plugins/chat_extras_profileless.js
Executable file
|
@ -0,0 +1,29 @@
|
|||
const parsePlain = require('../util/chatparse_plain.js')
|
||||
module.exports = {
|
||||
load: (b) => {
|
||||
b.on("chat_unparsed", data => {
|
||||
if(data.type == "profileless"){
|
||||
if(data.parsed) return
|
||||
if (data.playerChatType.translation_key === '%s') {
|
||||
const parsed = parsePlain(data.json)
|
||||
const split = parsed.split(': ')
|
||||
const chatName = split.splice(0, 1)[0]
|
||||
const chatNameSplit = chatName.split(' ')
|
||||
nickname = chatNameSplit[chatNameSplit.length - 1]
|
||||
username = b.findRealName(chatName)
|
||||
uuid = b.findUUID(username)
|
||||
message = split.join(': ')
|
||||
b.emit('chat', {
|
||||
json: data.json,
|
||||
type: data.type,
|
||||
subtype: "extras_profileless",
|
||||
uuid,
|
||||
message,
|
||||
nickname,
|
||||
username
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
18
plugins/chat_player.js
Executable file
18
plugins/chat_player.js
Executable file
|
@ -0,0 +1,18 @@
|
|||
module.exports = {
|
||||
load: (b) => {
|
||||
b.on("chat_unparsed", data => {
|
||||
if(data.type == "player" || data.type == "profileless"){
|
||||
if(data.parsed) return
|
||||
b.emit('chat', {
|
||||
json: data.json,
|
||||
type: data.type,
|
||||
subtype: `generic_${data.type}`,
|
||||
uuid: data.uuid,
|
||||
message: data.message,
|
||||
nickname: data.nickname,
|
||||
username: data.username
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
29
plugins/chat_system.js
Executable file
29
plugins/chat_system.js
Executable file
|
@ -0,0 +1,29 @@
|
|||
const parsePlain = require('../util/chatparse_plain.js')
|
||||
module.exports = {
|
||||
load: (b) => {
|
||||
b.on("chat_unparsed", data => {
|
||||
if(data.type == "system" || data.type == "legacy"){
|
||||
if(data.parsed) return
|
||||
if (data.type == "legacy" && data.json.translate === 'chat.type.text') return
|
||||
let subtype = `generic_${data.type}`
|
||||
if(data.type == "legacy" && data.uuid) subtype += "_withuuid"
|
||||
const parsed = parsePlain(data.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: data.json,
|
||||
type: data.type,
|
||||
subtype,
|
||||
uuid: uuid,
|
||||
message: split.join(': '),
|
||||
nickname: nickname,
|
||||
username: username
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
33
plugins/chat_vanilla_legacy.js
Executable file
33
plugins/chat_vanilla_legacy.js
Executable file
|
@ -0,0 +1,33 @@
|
|||
const parsePlain = require('../util/chatparse_plain.js')
|
||||
module.exports = {
|
||||
load: (b) => {
|
||||
b.on("chat_unparsed", data => {
|
||||
if(data.type == "legacy"){
|
||||
if(data.parsed) return
|
||||
let subtype = `vanilla_${data.type}`
|
||||
if(data.type == "legacy" && data.uuid) subtype += "_withuuid"
|
||||
if (data.json.translate === 'chat.type.text') { // Servers without Extras chat
|
||||
let message;
|
||||
let username;
|
||||
let uuid;
|
||||
let nickname;
|
||||
if (data.json.with && data.json.with.length >= 2) {
|
||||
message = parsePlain(data.json.with[1])
|
||||
username = parsePlain(data.json.with[0])
|
||||
}
|
||||
uuid = b.findUUID(username)
|
||||
nickname = b.findDisplayName(uuid)
|
||||
b.emit('chat', {
|
||||
json: data.json,
|
||||
type: data.type,
|
||||
subtype: subtype,
|
||||
uuid,
|
||||
message,
|
||||
nickname,
|
||||
username
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
|
@ -26,18 +26,18 @@ module.exports = {
|
|||
for (const prefix of b.prefix) {
|
||||
if (fullCommand.startsWith(prefix)) {
|
||||
const command = fullCommand.slice(prefix.length)
|
||||
b.runCommand(data.username, data.nickname, data.uuid, command, data.type, prefix)
|
||||
b.runCommand(data.username, data.nickname, data.uuid, command, data.type, data.subtype, prefix)
|
||||
}
|
||||
}
|
||||
})
|
||||
b.runCommand = (name, nickname, uuid, text, msgType, prefix) => {
|
||||
b.runCommand = (name, nickname, uuid, text, msgType, msgSubtype, prefix) => {
|
||||
if (uuid === '00000000-0000-0000-0000-000000000000') return
|
||||
if (Date.now() - b.lastCmd <= 1000) return
|
||||
const userSettings = loadSettings(uuid)
|
||||
b.lastCmd = Date.now()
|
||||
const lang = settings.defaultLang
|
||||
|
||||
const commandClass = new Command(uuid, name, nickname, text, msgType, prefix, b, userSettings)
|
||||
const commandClass = new Command(uuid, name, nickname, text, msgType, msgSubtype, prefix, b, userSettings)
|
||||
b.emit('command', commandClass)
|
||||
if (commandClass.cancel === true) return
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ module.exports = {
|
|||
setTimeout(() => { b.interval.ccqi = setInterval(b.advanceccq, 2) }, 1000)
|
||||
b.ccStarted = true
|
||||
})
|
||||
b.on('chat', (data) => {
|
||||
b.on('chat_unparsed', (data) => {
|
||||
if (data.json.translate === 'commands.fill.failed' || (data.json.extra && data.json.extra[0] && data.json.extra[0].translate === 'commands.fill.failed') ||
|
||||
data.json.translate === 'commands.fill.success' || (data.json.extra && data.json.extra[0] && data.json.extra[0].translate === 'commands.fill.success')) {
|
||||
if (!b.ccStarted) {
|
||||
|
|
|
@ -45,8 +45,12 @@ module.exports = {
|
|||
b.info = (msg) => {
|
||||
consoleWrite(`[${b.id}] [info] ${msg}`)
|
||||
}
|
||||
b.displayChat = (type, msg) => {
|
||||
consoleWrite(`[${b.id}] [${type}] ${msg}`)
|
||||
b.displayChat = (type, subtype, msg) => {
|
||||
if(settings.displaySubtypesToConsole){
|
||||
consoleWrite(`[${b.id}] [${type}] [${subtype}] ${msg}`)
|
||||
} else {
|
||||
consoleWrite(`[${b.id}] [${type}] ${msg}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,7 +77,7 @@ module.exports = {
|
|||
b._client.write('client_command', { actionId: 0 }) // Simulates respawning
|
||||
b.sc_tasks.respawn.failed = 0
|
||||
})
|
||||
b.on('chat', (data) => {
|
||||
b.on('chat_unparsed', (data) => {
|
||||
if (data.json.translate === 'chat.disabled.options' || (data.json.extra && data.json.extra[0] && data.json.extra[0].translate === 'chat.disabled.options') ||
|
||||
data.json.translate === 'Chat disabled in client options' || (data.json.extra && data.json.extra[0] && data.json.extra[0].translate === 'Chat disabled in client options')) {
|
||||
b.sc_tasks.respawn.failed = 1
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const settings = require('../settings.json')
|
||||
class Command {
|
||||
constructor (uuid, user, nick, cmd, msgType, prefix, bot, prefs) {
|
||||
constructor (uuid, user, nick, cmd, msgType, msgSubtype, prefix, bot, prefs) {
|
||||
this.send = (text, uuid) => { bot.tellraw(uuid || '@a', text) }
|
||||
this.reply = text => bot.tellraw(uuid, text)
|
||||
this.uuid = uuid
|
||||
|
@ -8,6 +8,7 @@ class Command {
|
|||
this.nickname = nick
|
||||
this.command = cmd
|
||||
this.msgType = msgType
|
||||
this.msgSubtype = msgSubtype
|
||||
this.prefix = prefix
|
||||
this.bot = bot
|
||||
this.type = 'minecraft'
|
||||
|
|
|
@ -11,6 +11,7 @@ class ConsoleCommand {
|
|||
this.nickname = 'Owner'
|
||||
this.command = cmd
|
||||
this.msgType = '_bot_console'
|
||||
this.msgSubtype = '_bot_console'
|
||||
this.prefix = ''
|
||||
this.bot = index2 >= 0
|
||||
? index.bots[index2]
|
||||
|
|
Loading…
Reference in a new issue