mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-27 09:35:35 -05:00
80 lines
2.5 KiB
JavaScript
80 lines
2.5 KiB
JavaScript
|
||
// eslint-disable-next-line no-undef
|
||
// const parse = require('../util/text_parser');
|
||
const { containsIllegalCharacters } = require('../util/containsIllegalCharacters')
|
||
const { chatPacketListener, parsePlayerMessages } = require('../util/chat')
|
||
const minecraftVersionToNumber = require('../util/minecraftVersionToNumber')
|
||
|
||
function inject (bot, dcclient, config) {
|
||
bot.chatQueue = []
|
||
bot._chatQueue = []
|
||
|
||
const _chatQueueInterval = setInterval(() => {
|
||
if (bot.chatQueue.length !== 0) {
|
||
if (containsIllegalCharacters(bot.chatQueue[0])) {
|
||
bot.chatQueue.shift()
|
||
return
|
||
};
|
||
// totallynotskidded™️ from mineflayer/lib/plugins/chat.js
|
||
bot.chatQueue[0].split('\n').forEach((subMessage) => {
|
||
if (!subMessage) return
|
||
let smallMsg
|
||
for (let i = 0; i < subMessage.length; i += config.chat.messageLength) {
|
||
smallMsg = subMessage.substring(i, i + config.chat.messageLength)
|
||
bot._chatQueue.push(smallMsg)
|
||
}
|
||
})
|
||
bot.chatQueue.shift()
|
||
}
|
||
}, 0)
|
||
|
||
const chatQueueInterval = setInterval(function () {
|
||
if (bot._chatQueue.length !== 0) {
|
||
if (minecraftVersionToNumber(bot.version) >= 1.19) {
|
||
// totallynotskidded™️ from mineflayer/lib/plugins/chat.js
|
||
if (bot._chatQueue[0].startsWith('/')) {
|
||
bot.write('chat_command', {
|
||
command: bot._chatQueue[0].substring(1), // removes / from the command
|
||
timestamp: BigInt(Date.now()),
|
||
salt: 0n,
|
||
argumentSignatures: [],
|
||
signedPreview: false
|
||
})
|
||
} else {
|
||
bot.write('chat_message', {
|
||
message: bot._chatQueue[0],
|
||
timestamp: BigInt(Date.now()),
|
||
salt: 0,
|
||
signature: Buffer.alloc(0) // the bot will never go online mode i guess so i will just use this
|
||
})
|
||
}
|
||
} else {
|
||
bot.write('chat', { message: bot._chatQueue[0] })
|
||
}
|
||
bot._chatQueue.shift()
|
||
}
|
||
}, 450)
|
||
|
||
bot.chat = (message) => {
|
||
bot.chatQueue.push(String(message))
|
||
}
|
||
|
||
bot.on('end', () => {
|
||
clearInterval(chatQueueInterval)
|
||
clearInterval(_chatQueueInterval)
|
||
})
|
||
|
||
function listener (packet) {
|
||
chatPacketListener(
|
||
packet,
|
||
bot,
|
||
minecraftVersionToNumber(bot.version) >= 1.19
|
||
)
|
||
}
|
||
bot._client.on('system_chat', listener)
|
||
bot._client.on('chat', listener)
|
||
|
||
bot.on('message', (message, packet) => parsePlayerMessages(message, packet, bot))
|
||
}
|
||
|
||
module.exports = { inject }
|