2024-08-22 13:50:44 -04:00
const fs = require ( 'fs' )
if ( ! fs . readdirSync ( '.' ) . includes ( 'settings.json' ) ) {
2024-08-24 10:15:39 -04:00
console . log ( 'Settings file is missing, using defaults.' )
fs . copyFileSync ( 'settings_example.json' , 'settings.json' )
2024-08-22 13:50:44 -04:00
}
if ( ! fs . readdirSync ( '.' ) . includes ( 'secret.json' ) ) {
2024-08-24 10:15:39 -04:00
console . log ( 'Secrets file is missing, using defaults.' )
fs . copyFileSync ( 'secret_example.json' , 'secret.json' )
2024-09-21 14:58:34 -04:00
console . log ( 'Please change the hashing keys in the secrets file. It is also recommended to remove permissions of other users to read from this file, for example, by giving it 600 permissions if running on a Unix or Unix-like OS.' )
2024-08-22 13:50:44 -04:00
}
2024-07-28 02:37:31 -04:00
const m = require ( 'minecraft-protocol' )
const generateUser = require ( './util/usergen.js' )
const EventEmitter = require ( 'node:events' )
2024-08-22 13:50:44 -04:00
const settings = require ( './settings.json' )
2024-09-02 16:41:07 -04:00
const secret = require ( './secret.json' )
2024-09-22 23:45:18 -04:00
const version = require ( './version.json' )
const protover = require ( './util/getProtocolVersion.js' )
2024-10-12 01:44:33 -04:00
const mcd = require ( 'minecraft-data' )
2024-09-11 23:44:17 -04:00
module . exports . bots = [ ]
2024-07-27 02:39:18 -04:00
2024-08-22 07:34:39 -04:00
const botplug = [ ]
const bpl = fs . readdirSync ( 'plugins' )
for ( const plugin of bpl ) {
if ( ! plugin . endsWith ( '.js' ) ) {
continue
2024-07-28 02:37:31 -04:00
}
2024-08-22 07:34:39 -04:00
try {
botplug . push ( require ( ` ./plugins/ ${ plugin } ` ) )
} catch ( e ) { console . log ( e ) }
}
const loadplug = ( botno ) => {
2024-07-28 02:37:31 -04:00
botplug . forEach ( ( plug ) => {
try {
2024-08-12 05:13:32 -04:00
if ( plug . load ) {
2024-09-11 23:57:32 -04:00
plug . load ( module . exports . bots [ botno ] )
2024-07-28 02:37:31 -04:00
}
} catch ( e ) { console . log ( e ) }
} )
2024-07-27 02:39:18 -04:00
}
2024-10-12 01:44:33 -04:00
let bypassWarningShown = false
2024-09-22 23:45:18 -04:00
2024-07-28 02:37:31 -04:00
const createBot = function createBot ( host , oldId ) {
if ( host . options . disabled ) {
return
}
2024-09-02 16:41:07 -04:00
const options = {
2024-07-28 02:37:31 -04:00
host : host . host ,
port : host . port ? host . port : 25565 ,
2024-08-12 05:13:32 -04:00
version : host . version ? host . version : settings . version _mc
2024-09-02 16:41:07 -04:00
}
2024-10-12 01:44:33 -04:00
if ( protover ( options . version ) < version . minimumMcVersion ) {
if ( ! settings . bypassVersionRequirement ) {
2024-09-22 23:45:18 -04:00
console . error ( ` [error] ${ version . botName } does not support Minecraft versions below ${ version . minimumMcVersion } ( ${ mcd . postNettyVersionsByProtocolVersion . pc [ version . minimumMcVersion ] [ 0 ] . minecraftVersion } ) ` )
return
} else {
2024-10-12 01:44:33 -04:00
if ( ! bypassWarningShown ) console . warn ( '[warning] You have disabled the version requirement, allowing the bot to join to servers with old Minecraft versions. These versions are unsupported and may break at any time. Any issues on such versions will not be fixed.' )
bypassWarningShown = true
2024-09-22 23:45:18 -04:00
}
}
2024-09-12 00:26:36 -04:00
if ( host . options . online ) {
2024-09-02 16:41:07 -04:00
options . username = secret . onlineEmail
options . password = secret . onlinePass
options . auth = 'microsoft'
} else {
options . username = generateUser ( host . options . legalName )
}
const bot = new EventEmitter ( )
bot . _client = m . createClient ( options )
2024-07-28 02:37:31 -04:00
if ( typeof oldId !== 'undefined' ) {
2024-09-11 23:57:32 -04:00
for ( const i in module . exports . bots [ oldId ] . interval ) {
clearInterval ( module . exports . bots [ oldId ] . interval [ i ] )
2024-07-27 02:39:18 -04:00
}
2024-09-11 23:57:32 -04:00
delete module . exports . bots [ oldId ]
2024-07-28 02:37:31 -04:00
bot . id = oldId
2024-09-11 23:44:17 -04:00
module . exports . bots [ oldId ] = bot
2024-07-28 02:37:31 -04:00
} else {
2024-09-11 23:57:32 -04:00
bot . id = module . exports . bots . length
2024-09-11 23:58:16 -04:00
module . exports . bots . push ( bot )
2024-07-28 02:37:31 -04:00
}
2024-07-27 02:39:18 -04:00
2024-07-28 02:37:31 -04:00
bot . host = host
2024-09-19 22:21:01 -04:00
if ( bot . host . host . includes ( ':' ) ) {
2024-09-13 23:24:48 -04:00
bot . host . options . displayAsIPv6 = true
}
2024-07-28 02:37:31 -04:00
bot . interval = { }
2024-07-27 02:39:18 -04:00
2024-07-28 02:37:31 -04:00
bot . info = ( msg ) => {
console . log ( ` [ ${ bot . id } ] [info] ${ msg } ` )
}
2024-08-12 05:13:32 -04:00
2024-09-19 00:31:15 -04:00
bot . displayChat = ( type , subtype , msg ) => {
2024-09-19 22:21:01 -04:00
if ( settings . displaySubtypesToConsole ) {
2024-09-19 00:31:15 -04:00
console . log ( ` [ ${ bot . id } ] [ ${ type } ] [ ${ subtype } ] ${ msg } ` )
} else {
console . log ( ` [ ${ bot . id } ] [ ${ type } ] ${ msg } ` )
}
2024-08-12 05:13:32 -04:00
}
2024-07-28 02:37:31 -04:00
loadplug ( bot . id )
bot . _client . on ( 'error' , ( err ) => {
console . log ( err )
} )
2024-07-27 02:39:18 -04:00
}
2024-08-22 07:34:39 -04:00
for ( const server of settings . servers ) {
createBot ( server )
2024-07-27 02:39:18 -04:00
}
2024-08-12 05:13:32 -04:00
2024-07-28 02:37:31 -04:00
module . exports . createBot = createBot