chomens-bot-js/bot.js

187 lines
4.9 KiB
JavaScript
Raw Normal View History

2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
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')
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,
2022-11-14 05:45:49 -05:00
checkTimeoutInterval: '30000',
keepAlive: false,
2022-11-27 02:35:28 -05:00
hideErrors: true
}
bot._client = mc.createClient(bot.options)
2022-12-09 06:03:29 -05:00
bot.setMaxListeners(Infinity) // is this bad code?
bot._client.setMaxListeners(Infinity) // this too
2022-11-27 02:35:28 -05:00
bot.version = bot._client.version
bot.write = (name, data) => bot._client.write(name, data)
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()
}
bot.visibility = false
bot.getBots = getBots
bot.getplayerusername = {}
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
2022-11-27 02:35:28 -05:00
setNewBot(bot.options.host, bot)
2022-11-27 00:46:19 -05:00
2022-11-27 02:35:28 -05:00
await loadPlugins(bot, dcclient, config, rl)
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
const channel = dcclient.channels.cache.get(config.discord.servers[bot.options.host])
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
bot.playersAddedPlayers = {}
bot.getplayerusername = {}
2022-11-14 05:45:49 -05:00
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
const chatMessage = require('prismarine-chat')(bot.version)
const mcData = require('minecraft-data')(bot.version)
2022-11-14 05:45:49 -05:00
bot.console.info(
2022-11-27 02:35:28 -05:00
`Successfully logged in to: ${bot.options.host}:${bot.options.port}`
)
2022-11-14 05:45:49 -05:00
channel.send(
2022-11-27 02:35:28 -05:00
`Successfully logged in to: \`${bot.options.host}:${bot.options.port}\``
)
2022-11-14 05:45:49 -05:00
bot.vmoptions = {
timeout: 2000,
sandbox: {
2022-11-27 02:35:28 -05:00
run (cmd) {
bot.core.run(cmd)
2022-11-14 05:45:49 -05:00
},
mc,
mineflayer,
chat: bot.chat,
moment,
randomstring,
uuid,
chatMessage,
crypto,
colorConvert,
2022-11-27 02:35:28 -05:00
bruhifyText (message) {
2022-11-14 05:45:49 -05:00
if (
typeof message !== 'string'
2022-11-27 02:35:28 -05:00
) throw new SyntaxError('message must be a string')
bot.bruhifyText = message.substring(0, 1000)
2022-11-14 05:45:49 -05:00
},
generateEaglerUsername,
cowsay,
cows,
2022-11-27 02:35:28 -05:00
mcData
}
}
bot.vm = new VM(bot.vmoptions)
2022-11-14 05:45:49 -05:00
// await sleep(1400);
// bot.tellraw('@a', [
// {
// text: 'ChomeNS Bot',
// color: 'yellow',
// },
// {
// text: ' - ',
// color: 'dark_gray',
// },
// {
// text: 'made by ',
// color: 'gray',
// },
// {
// text: 'chayapak',
// color: 'gold',
// },
// ]);
2022-11-27 02:35:28 -05:00
})
2022-11-14 05:45:49 -05:00
bot.on('player_added', (player) => {
2022-11-27 02:35:28 -05:00
bot.playersAddedPlayers[player.name] = player.UUID
bot.getplayerusername[player.UUID] = player.name
})
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
bot._client.on('end', function (reason) {
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(
2022-11-27 02:35:28 -05:00
`Disconnected from ${bot.options.host} (${event} event): ${util.inspect(reason)}`
)
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
bot._client.on('keep_alive', (packet) => {
2022-11-27 02:35:28 -05:00
bot.write('keep_alive', { keepAliveId: packet.keepAliveId })
})
2022-11-21 07:50:02 -05:00
2022-11-27 02:35:28 -05:00
bot._client.on('kick_disconnect', function (data) {
const parsed = JSON.parse(data.reason)
bot.end(parsed, 'kick_disconnect')
})
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
bot._client.on('disconnect', function (data) {
const parsed = JSON.parse(data.reason)
bot.end(parsed, 'disconnect')
})
2022-11-14 05:45:49 -05:00
2022-11-27 02:35:28 -05:00
bot._client.on('error', function () {})
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 }