mirror of
https://github.com/ChomeNS/chomens-bot-mc.git
synced 2024-11-14 10:44:55 -05:00
116 lines
3.2 KiB
JavaScript
116 lines
3.2 KiB
JavaScript
const mc = require('minecraft-protocol')
|
|
const sleep = require('sleep-promise')
|
|
const { EventEmitter } = require('events')
|
|
const { loadPlugins } = require('./util/loadPlugins')
|
|
const util = require('node:util')
|
|
const randomstring = require('randomstring')
|
|
|
|
/**
|
|
* makes the bot
|
|
* @param {object} server the server object used in the config
|
|
* @param {object} config the config file
|
|
* @param {Function} getBots get bots function in index.js
|
|
* @param {Function} setNewBot ig real
|
|
* @param {Class} dcclient discord client
|
|
* @param {object} rl readline.
|
|
* @return {object} the bot object
|
|
*/
|
|
async function createBot (server, config, getBots, setNewBot, dcclient, rl) {
|
|
const bot = new EventEmitter()
|
|
bot.options = {
|
|
username: !server.kaboom
|
|
? 'ChomeNS_Bot'
|
|
: randomstring.generate(8),
|
|
host: server.host,
|
|
port: server.port,
|
|
version: config.version,
|
|
kaboom: server.kaboom,
|
|
logging: server.logging,
|
|
checkTimeoutInterval: config.timeoutInterval,
|
|
hideErrors: true
|
|
}
|
|
bot._client = mc.createClient(bot.options)
|
|
bot.setMaxListeners(Infinity) // is this bad code?
|
|
bot._client.setMaxListeners(Infinity) // this too
|
|
bot.version = bot._client.version
|
|
bot.write = (name, data) => bot._client.write(name, data)
|
|
bot.end = (reason = 'end', event) => {
|
|
bot.emit('end', reason, event)
|
|
bot.removeAllListeners()
|
|
bot._client.end()
|
|
bot._client.removeAllListeners()
|
|
}
|
|
bot.visibility = false
|
|
bot.getBots = getBots
|
|
|
|
await sleep(200)
|
|
|
|
setNewBot(bot.options.host, bot)
|
|
|
|
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host])
|
|
|
|
channel.send(
|
|
`Connecting to: \`${bot.options.host}:${bot.options.port}\``
|
|
)
|
|
bot._client.on('login', async function (data) {
|
|
bot.entityId = data.entityId
|
|
bot.uuid = bot._client.uuid
|
|
bot.username = bot._client.username
|
|
|
|
channel.send(
|
|
`Successfully logged in to: \`${bot.options.host}:${bot.options.port}\``
|
|
)
|
|
})
|
|
|
|
await loadPlugins(bot, dcclient, config, rl)
|
|
|
|
bot._client.on('end', function (reason) {
|
|
bot.end(reason, 'end')
|
|
})
|
|
|
|
bot.on('end', (reason, event) => {
|
|
bot.console.info(
|
|
`Disconnected from ${bot.options.host} (${event} event): ${util.inspect(reason)}`
|
|
)
|
|
channel.send(`Disconnected: \`${util.inspect(reason)}\``)
|
|
|
|
let timeout = config.reconnectTimeout
|
|
|
|
try {
|
|
if (reason.text) {
|
|
if (reason.text ===
|
|
'Wait 5 seconds before connecting, thanks! :)' ||
|
|
reason.text ===
|
|
'You are logging in too fast, try again later.'
|
|
) timeout = 1000 * 7
|
|
}
|
|
} catch (e) {
|
|
bot.console.error(e)
|
|
}
|
|
|
|
setTimeout(() => {
|
|
bot.end()
|
|
createBot(server, config, getBots, setNewBot, dcclient, rl)
|
|
}, timeout)
|
|
})
|
|
|
|
bot._client.on('keep_alive', ({ keepAliveId }) => {
|
|
bot.write('keep_alive', { keepAliveId })
|
|
})
|
|
|
|
bot._client.on('kick_disconnect', function (data) {
|
|
const parsed = JSON.parse(data.reason)
|
|
bot.end(parsed, 'kick_disconnect')
|
|
})
|
|
|
|
bot._client.on('disconnect', function (data) {
|
|
const parsed = JSON.parse(data.reason)
|
|
bot.end(parsed, 'disconnect')
|
|
})
|
|
|
|
bot._client.on('error', function () {})
|
|
|
|
return bot
|
|
};
|
|
|
|
module.exports = { createBot }
|