AI
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
process.env.DEBUG = 'mineflayer:*'; // Enables all debugging logs
|
||||
|
||||
const mineflayer = require('mineflayer');
|
||||
const minecraftData = require('minecraft-data');
|
||||
const { pathfinder, Movements, goals: { GoalNear } } = require('mineflayer-pathfinder');
|
||||
@ -46,6 +48,8 @@ class CJbot{
|
||||
this.auth = args.auth || 'microsoft';
|
||||
this.version = args.version || '1.20.1';
|
||||
|
||||
this.hasAi = args.hasAi;
|
||||
|
||||
// States if the bot should connect when its loaded
|
||||
this.autoReConnect = 'autoConnect' in args ? args.autoReConnect : true;
|
||||
this.autoConnect = 'autoConnect' in args ? args.autoConnect : true;
|
||||
@ -57,44 +61,48 @@ class CJbot{
|
||||
|
||||
connect(){
|
||||
return new Promise((resolve, reject) =>{
|
||||
try{
|
||||
// Create the mineflayer instance
|
||||
this.bot = mineflayer.createBot({
|
||||
host: this.host,
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
version: this.version,
|
||||
auth: this.auth,
|
||||
});
|
||||
|
||||
// Create the mineflayer instance
|
||||
this.bot = mineflayer.createBot({
|
||||
host: this.host,
|
||||
username: this.username,
|
||||
password: this.password,
|
||||
version: this.version,
|
||||
auth: this.auth,
|
||||
});
|
||||
// If an error happens before the login event, toss an error back to
|
||||
// the caller of the function
|
||||
let onError = this.bot.on('error', (m)=>{
|
||||
console.log(this.name, m.toString())
|
||||
reject(m)
|
||||
})
|
||||
|
||||
// If an error happens before the login event, toss an error back to
|
||||
// the caller of the function
|
||||
let onError = this.bot.on('error', (m)=>{
|
||||
console.log(this.bot.version, m.toString())
|
||||
reject(m)
|
||||
})
|
||||
// If the connection ends before the login event, toss an error back
|
||||
// to the caller of the function
|
||||
this.bot.on('end', (reason, ...args)=>{
|
||||
console.log(this.name, 'Connection ended:', reason, ...args);
|
||||
this.isReady = false;
|
||||
reject(reason);
|
||||
});
|
||||
|
||||
// If the connection ends before the login event, toss an error back
|
||||
// to the caller of the function
|
||||
this.bot.on('end', (m)=>{
|
||||
console.log(this.name, 'Connection ended:', m);
|
||||
this.isReady = false;
|
||||
reject(m);
|
||||
});
|
||||
// When the bot is ready, return to the caller success
|
||||
this.bot.on('login', ()=>{
|
||||
this.__onReady()
|
||||
resolve()
|
||||
});
|
||||
|
||||
// When the bot is ready, return to the caller success
|
||||
this.bot.on('login', ()=>{
|
||||
this.__onReady()
|
||||
resolve()
|
||||
});
|
||||
|
||||
// Set a timer to try to connect again in 30 seconds if the bot is
|
||||
// not connected
|
||||
setTimeout(async ()=>{try{
|
||||
if(this.autoReConnect && !this.isReady) await this.connect();
|
||||
// Set a timer to try to connect again in 30 seconds if the bot is
|
||||
// not connected
|
||||
setTimeout(async ()=>{try{
|
||||
if(this.autoReConnect && !this.isReady) await this.connect();
|
||||
}catch(error){
|
||||
console.error('minecraft.js | connect | setTimeout |', this.name, ' ', error)
|
||||
}}, 30000);
|
||||
}catch(error){
|
||||
console.error('minecraft.js | connect | setTimeout |', this.name, ' ', error)
|
||||
}}, 30000);
|
||||
reject(error);
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
@ -119,25 +127,25 @@ class CJbot{
|
||||
|
||||
// Call the internal listeners when the bot is ready
|
||||
for(let callback of this.listeners.onReady || []){
|
||||
console.log('calling listener', callback)
|
||||
await callback.call(this)
|
||||
console.log('calling listener', callback);
|
||||
await callback.call(this);
|
||||
}
|
||||
|
||||
this.isReady = true;
|
||||
this.__error()
|
||||
this.__error();
|
||||
console.log('Bot is ready', this.bot.entity.username, this.username);
|
||||
|
||||
// Start chat listeners
|
||||
this.__listen();
|
||||
|
||||
}catch(error){
|
||||
console.error('minecraft.js | __onReady | ', this.name, ' ', error)
|
||||
console.error('minecraft.js | __onReady | ', this.name, ' ', error);
|
||||
}}
|
||||
|
||||
__startListeners(){
|
||||
for(let event in this.listeners){
|
||||
for(let callback of this.listeners[event]){
|
||||
this.bot.on(event, callback)
|
||||
this.bot.on(event, callback);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -264,7 +272,7 @@ class CJbot{
|
||||
}
|
||||
|
||||
__chatCoolDown(){
|
||||
return Math.floor(Math.random() * (3000 - 1500) + 1500);
|
||||
return Math.floor(Math.random() * (3000 - 2000) + 2000);
|
||||
}
|
||||
|
||||
async say(...messages){
|
||||
@ -278,6 +286,16 @@ class CJbot{
|
||||
}
|
||||
}
|
||||
|
||||
async sayAiSafe(...messages){
|
||||
for(let message of messages){
|
||||
if(message.startsWith('/') && !(message.startsWith('/msg') || message.startsWith('/help'))){
|
||||
console.log('bot tried to execute bad command', message);
|
||||
message = '.'+message;
|
||||
}
|
||||
await this.say(message);
|
||||
}
|
||||
}
|
||||
|
||||
async whisper(to, ...messages){
|
||||
await this.say(...messages.map(message=>`/msg ${to} ${message}`));
|
||||
}
|
||||
@ -314,9 +332,9 @@ class CJbot{
|
||||
console.error(`Chat command error on ${cmd} from ${from}\n`, error);
|
||||
}
|
||||
this.__unLockCommand();
|
||||
}else{
|
||||
}/*else{
|
||||
this.whisper(from, `I dont know anything about ${cmd}`);
|
||||
}
|
||||
}*/
|
||||
}catch(error){
|
||||
console.error('minecraft.js | __doCommand |', this.name, ' ', error)
|
||||
}}
|
||||
|
Reference in New Issue
Block a user