multi bots online

This commit is contained in:
2023-05-08 10:58:05 -04:00
parent cbf7f78604
commit 0616ce2ea5
11 changed files with 1005 additions and 565 deletions

View File

@ -15,28 +15,38 @@ class CJbot{
combatTag = false;
doLogOut = false;
static bots = {};
static __addLock = false;
static __toConnect = [];
constructor(args){
this.name = args.name;
this.username = args.username;
this.password = args.password;
this.host = args.host;
this.auth = args.auth || 'microsoft';
this.version = args.version || '1.18.2';
this.version = args.version || '1.19.2';
this.autoReConnect = 'autoConnect' in args ? args.autoReConnect : true;
this.autoConnect = 'autoConnect' in args ? args.autoConnect : true;
if(this.autoConnect){
console.log('connecting', this.username, this.autoConnect)
this.connect();
}
if(this.autoReConnect){
this.__autoReConnect()
}
if(this.autoReConnect) this.__autoReConnect()
}
connect(){
return new Promise((resolve, reject) =>{
// let cancle = setTimeout(()=>{
// reject('TimeOut')
// }, 10000)
// resolve = (...args)=>{
// clearTimeout(cancle);
// return resolve(...args);
// }
// reject = (...args)=>{
// // clearTimeout(cancle);
// return reject(...args);
// }
this.bot = mineflayer.createBot({
host: this.host,
username: this.username,
@ -51,29 +61,36 @@ class CJbot{
reject(m)
})
this.bot.on('end', reason =>{
console.log('Connection ended:', reason)
this.isReady = false
}
);
this.bot.on('end', (m)=>{
console.log(this.name, 'Connection ended:', m);
this.isReady = false;
reject(m);
});
this.bot.on('login', ()=>{
this.__onReady()
resolve()
});
setTimeout(async ()=>{try{
if(this.autoReConnect && !this.isReady) await this.connect();
}catch(error){
console.error('minecraft.js/connect/setTimeout/', this.name, ' ', error)
}}, 30000);
});
}
static
once(event){
return new Promise((resolve, reject)=> this.bot.once(event, resolve));
}
async __onReady(){
async __onReady(){try{
this.__startListeners();
console.log('listeners', this.listeners.onReady)
// console.log('listeners', this.listeners.onReady)
for(let callback of this.listeners.onReady || []){
console.log('calling listener', callback)
@ -86,7 +103,10 @@ class CJbot{
// Start chat listeners
this.__listen();
}
}catch(error){
console.error('minecraft.js/__onReady/', this.name, ' ', error)
}}
__startListeners(){
@ -111,37 +131,34 @@ class CJbot{
}
__autoReConnect(){
console.log('auto connect function')
this.on('end', (reason)=>{
console.error('MC on end', reason)
try{
console.log('auto connect function')
this.on('end', async (reason)=>{
console.error('_autorestart MC on end', reason)
await sleep('30000')
this.connect()
});
this.connect()
});
this.on('kick', console.error)
this.bot.on('kick', console.error)
this.on('error', (error)=>{
console.error('MC on error', error);
this.on('error', (error)=>{
console.error('MC on error', error);
this.connect();
});
}catch(error){
console.error('error in __autoReConnect', error);
this.connect();
});
}
}
__error(){
__error(message){
this.bot.on('error', (error)=>{
console.error(`ERROR!!! MC bot ${this.username} on ${this.host} had an error:\n`, error)
});
}
getPlayers(){
for (let [username, value] of Object.entries(this.bot.players)){
value.lvl = Number(value.displayName.extra[0].text)
}
return this.bot.players;
}
/* Chat and messaging*/
@ -200,10 +217,10 @@ class CJbot{
}
quit(force){
if(!this.combatTag){
if(!this.combatTag || force){
this.bot.quit();
}else{
console.log('stoped logout due to combatTag')
console.log('Logout prevented due to combatTag');
this.doLogOut = true;
}
}
@ -241,7 +258,7 @@ class CJbot{
});
}
async __doCommand(from, command){
async __doCommand(from, command){try{
if(this.commandLock){
this.whisper(from, `cool down, try again in ${this.commandCollDownTime/1000} seconds...`);
return ;
@ -261,12 +278,23 @@ class CJbot{
}else{
this.whisper(from, `I dont know anything about ${cmd}`);
}
}
}catch(error){
console.error('minecraft.js/__doCommand/', this.name, ' ', error)
}}
addCommand(name, obj){
if(this.commands[name]) return false;
this.commands[name] = obj;
}
getPlayers(){
for (let [username, value] of Object.entries(this.bot.players)){
value.lvl = Number(value.displayName.extra[0].text)
}
return this.bot.players;
}
}
module.exports = {CJbot};