chomens-bot-js/plugins/chat.js

78 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-11-27 02:35:28 -05:00
const { containsIllegalCharacters } = require('../util/containsIllegalCharacters')
const { chatPacketListener, parsePlayerMessages } = require('../util/chat')
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
function inject (bot, dcclient, config) {
2022-11-27 02:35:28 -05:00
bot.chatQueue = []
2022-12-05 04:54:50 -05:00
bot._chatQueue = []
2022-11-12 04:34:17 -05:00
const _chatQueueInterval = setInterval(() => {
if (bot.chatQueue.length !== 0) {
if (containsIllegalCharacters(bot.chatQueue[0])) {
2022-11-27 02:35:28 -05:00
bot.chatQueue.shift()
return
};
// totallynotskidded™ from mineflayer/lib/plugins/chat.js
for (const subMessage of bot.chatQueue[0].split('\n')) {
if (!subMessage) return
let smallMsg
for (let i = 0; i < subMessage.length; i += config.chat.messageLength) {
smallMsg = subMessage.substring(i, i + config.chat.messageLength)
2022-12-05 04:54:50 -05:00
bot._chatQueue.push(smallMsg)
}
}
bot.chatQueue.shift()
}
}, 0)
const chatQueueInterval = setInterval(function () {
2022-12-05 04:54:50 -05:00
if (bot._chatQueue.length !== 0) {
if (bot._chatQueue[0].startsWith('/') && minecraftVersionToNumber(bot.version) >= 1.19) {
// totallynotskidded™ from mineflayer
const command = bot._chatQueue[0].slice(1)
const timestamp = BigInt(Date.now())
bot._client.write('chat_command', {
command,
timestamp,
salt: 0n,
argumentSignatures: [],
signedPreview: false,
messageCount: 0,
acknowledged: Buffer.alloc(3),
// 1.19.2 Chat Command packet also includes an array of last seen messages
previousMessages: []
})
} else {
bot._client.chat(bot._chatQueue[0])
}
2022-12-05 04:54:50 -05:00
bot._chatQueue.shift()
2022-11-12 04:34:17 -05:00
}
2022-11-27 02:35:28 -05:00
}, 450)
2022-11-12 04:34:17 -05:00
bot.chat = (message) => {
2022-11-27 02:35:28 -05:00
bot.chatQueue.push(String(message))
}
2022-11-12 04:34:17 -05:00
2022-11-30 04:45:20 -05:00
bot.on('end', () => {
clearInterval(chatQueueInterval)
clearInterval(_chatQueueInterval)
2022-11-27 02:35:28 -05:00
})
2022-11-12 04:34:17 -05:00
function listener (packet) {
chatPacketListener(
packet,
bot,
minecraftVersionToNumber(bot.version) >= 1.19
)
}
// TODO: support playerChat (formattedMessage doesn't exist on kaboom so prefixes like [OP] doesn't appear)
// bot._client.on('playerChat', listener)
bot._client.on('systemChat', listener)
bot._client.on('chat', listener)
2023-03-14 22:05:43 -04:00
bot.on('message', (message, parsedMessage) => parsePlayerMessages(message, parsedMessage, bot))
}
2022-11-27 02:35:28 -05:00
module.exports = { inject }