chomens-bot-js/bot.js

134 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-11-27 02:35:28 -05:00
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')
2022-11-14 05:45:49 -05:00
/**
* 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
2022-11-27 00:46:19 -05:00
* @param {Function} setNewBot ig real
2022-11-14 05:45:49 -05:00
* @param {Class} dcclient discord client
2022-11-20 02:29:11 -05:00
* @param {object} rl readline.
* @return {object} the bot object
2022-11-14 05:45:49 -05:00
*/
2022-11-27 02:35:28 -05:00
async function createBot (server, config, getBots, setNewBot, dcclient, rl) {
const bot = new EventEmitter()
2022-11-14 05:45:49 -05:00
bot.options = {
2022-11-27 02:35:28 -05:00
username: !server.kaboom
? 'ChomeNS_Bot'
: randomstring.generate(8),
2022-11-25 08:05:01 -05:00
host: server.host,
port: server.port,
2022-11-14 05:45:49 -05:00
version: config.version,
kaboom: server.kaboom,
2022-12-03 07:53:47 -05:00
logging: server.logging,
checkTimeoutInterval: config.timeoutInterval,
2022-11-27 02:35:28 -05:00
hideErrors: true
}
// among us fix for bot.options.host and bot.options.port
bot.server = {
host: server.host,
port: server.port
}
bot.visibility = false
bot.getBots = getBots
2022-11-24 08:23:04 -05:00
bot.end = (reason = 'end', event) => {
2022-11-27 02:35:28 -05:00
bot.emit('end', reason, event)
bot.removeAllListeners()
bot._client.end()
bot._client.removeAllListeners()
}
try {
bot._client = mc.createClient(bot.options)
} catch (e) {
// should i make like if error is only econnrefused and others?
// btw this is to prevent uncaught (which does not reconnect the bot)
bot.end(e.toString(), 'uncaught but actually no') // will display "Disconnected (uncaught but actually no event): error here"
}
bot.setMaxListeners(Infinity)
bot._client.setMaxListeners(Infinity)
bot.version = bot._client.version
bot.write = (name, data) => bot._client.write(name, data)
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
await sleep(200)
2022-11-14 05:45:49 -05:00
setNewBot(bot.server.host, bot)
2022-11-27 00:46:19 -05:00
const channel = dcclient.channels.cache.get(config.discord.servers[bot.server.host])
2022-11-14 05:45:49 -05:00
channel.send(
`Connecting to: \`${bot.server.host}:${bot.server.port}\``
)
2022-11-27 02:35:28 -05:00
bot._client.on('login', async function (data) {
bot.entityId = data.entityId
bot.uuid = bot._client.uuid
bot.username = bot._client.username
2022-11-14 05:45:49 -05:00
channel.send(
`Successfully logged in to: \`${bot.server.host}:${bot.server.port}\``
2022-11-27 02:35:28 -05:00
)
})
2022-11-14 05:45:49 -05:00
await loadPlugins(bot, dcclient, config, rl)
bot._client.on('end', (reason) => {
2022-11-27 02:35:28 -05:00
bot.end(reason, 'end')
})
2022-11-14 05:45:49 -05:00
2022-11-30 04:45:20 -05:00
bot.on('end', (reason, event) => {
2022-11-14 05:45:49 -05:00
bot.console.info(
`Disconnected from ${bot.server.host} (${event} event): ${util.inspect(reason)}`
2022-11-27 02:35:28 -05:00
)
channel.send(`Disconnected: \`${util.inspect(reason)}\``)
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
let timeout = config.reconnectTimeout
2022-11-14 05:45:49 -05:00
try {
if (reason.text) {
if (reason.text ===
'Wait 5 seconds before connecting, thanks! :)' ||
reason.text ===
'You are logging in too fast, try again later.'
2022-11-27 02:35:28 -05:00
) timeout = 1000 * 7
2022-11-14 05:45:49 -05:00
}
} catch (e) {
2022-11-27 02:35:28 -05:00
bot.console.error(e)
2022-11-14 05:45:49 -05:00
}
setTimeout(() => {
2022-11-27 02:35:28 -05:00
bot.end()
createBot(server, config, getBots, setNewBot, dcclient, rl)
}, timeout)
})
2022-11-14 05:45:49 -05:00
2022-12-22 06:06:34 -05:00
bot._client.on('keep_alive', ({ keepAliveId }) => {
bot.write('keep_alive', { keepAliveId })
2022-11-27 02:35:28 -05:00
})
2022-11-21 07:50:02 -05:00
bot._client.on('kick_disconnect', (data) => {
2022-11-27 02:35:28 -05:00
const parsed = JSON.parse(data.reason)
bot.end(parsed, 'kick_disconnect')
})
2022-11-14 05:45:49 -05:00
bot._client.on('disconnect', (data) => {
2022-11-27 02:35:28 -05:00
const parsed = JSON.parse(data.reason)
bot.end(parsed, 'disconnect')
})
2022-11-14 05:45:49 -05:00
bot._client.on('error', () => {})
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
return bot
2022-11-14 05:45:49 -05:00
};
2022-11-27 02:35:28 -05:00
module.exports = { createBot }