chomens-bot-js/bot.js
2022-12-11 12:20:21 +07:00

186 lines
4.9 KiB
JavaScript

const mc = require('minecraft-protocol')
const crypto = require('crypto')
const colorConvert = require('color-convert')
const sleep = require('sleep-promise')
const generateEaglerUsername = require('./util/generateEaglerUsername')
const { EventEmitter } = require('events')
const { loadPlugins } = require('./util/loadPlugins')
const uuid = require('uuid-by-string')
const moment = require('moment-timezone')
const cowsay = require('cowsay2')
const cows = require('cowsay2/cows')
const util = require('node:util')
const { VM } = require('vm2')
const randomstring = require('randomstring')
const mineflayer = require('mineflayer')
/**
* 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: '30000',
keepAlive: false,
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
bot.getplayerusername = {}
await sleep(200)
setNewBot(bot.options.host, bot)
await loadPlugins(bot, dcclient, config, rl)
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host])
bot.playersAddedPlayers = {}
bot.getplayerusername = {}
bot._client.on('login', async function (data) {
bot.entityId = data.entityId
bot.uuid = bot._client.uuid
bot.username = bot._client.username
const chatMessage = require('prismarine-chat')(bot.version)
const mcData = require('minecraft-data')(bot.version)
bot.console.info(
`Successfully logged in to: ${bot.options.host}:${bot.options.port}`
)
channel.send(
`Successfully logged in to: \`${bot.options.host}:${bot.options.port}\``
)
bot.vmoptions = {
timeout: 2000,
sandbox: {
run (cmd) {
bot.core.run(cmd)
},
mc,
mineflayer,
chat: bot.chat,
moment,
randomstring,
uuid,
chatMessage,
crypto,
colorConvert,
bruhifyText (message) {
if (
typeof message !== 'string'
) throw new SyntaxError('message must be a string')
bot.bruhifyText = message.substring(0, 1000)
},
generateEaglerUsername,
cowsay,
cows,
mcData
}
}
bot.vm = new VM(bot.vmoptions)
// await sleep(1400);
// bot.tellraw('@a', [
// {
// text: 'ChomeNS Bot',
// color: 'yellow',
// },
// {
// text: ' - ',
// color: 'dark_gray',
// },
// {
// text: 'made by ',
// color: 'gray',
// },
// {
// text: 'chayapak',
// color: 'gold',
// },
// ]);
})
bot.on('player_added', (player) => {
bot.playersAddedPlayers[player.name] = player.UUID
bot.getplayerusername[player.UUID] = player.name
})
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', (packet) => {
bot.write('keep_alive', { keepAliveId: packet.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 }