2024-09-13 15:09:11 -04:00

108 lines
2.8 KiB
JavaScript

'use strict';
const {sleep} = require('../utils');
const conf = require('../conf');
const {CJbot} = require('../model/minecraft');
const inventoryViewer = require('mineflayer-web-inventory');
const commands = require('./commands');
const {onJoin} = require('./player_list');
const ai = require('./ai');
for(let name in conf.mc.bots){
if(CJbot.bots[name]) continue;
let bot = new CJbot({name, host: conf.mc.host, ...conf.mc.bots[name]});
CJbot.bots[name] = bot;
for(let command of conf.mc.bots[name].commands || ['default']){
for(let [name, toAdd] of Object.entries(commands[command])){
bot.addCommand(name, toAdd)
}
}
bot.on('onReady', async function(argument) {
// inventoryViewer(bot.bot);
try{
// onJoin(bot);
// await sleep(1000);
// bot.bot.setControlState('jump', true);
// setTimeout(()=> bot.bot.setControlState('jump', false), 5000);
if(bot.hasAi){
console.log(`${bot.bot.entity.username} has AI`);
let messages = [];
bot.bot.on('message', (message, type)=>{
if(type === 'game_info') return;
if(message.toString().startsWith('<') && message.toString().split('>')[0].includes(bot.bot.entity.username)) return;
console.log(`Message ${type}: ${message.toString()}`)
messages.push('>', message.toString());
});
await sleep(500);
let aiChat = await ai.run(
bot.bot.entity.username,
conf.ai.interval,
Object.values(bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n')
);
setInterval(async ()=>{
let result;
if(messages.length ===0) return;
try{
result = await aiChat.sendMessage(messages);
}catch(error){
console.log('error AI API', error, result);
messages = [];
return ;
}
try{
messages = [];
if(!result.response.text()) return;
for(let message of JSON.parse(result.response.text())){
console.log('toSay', message.delay, message.text);
if(message.text === '___') return;
setTimeout(async (message)=>{
await bot.sayAiSafe(message.text);
}, message.delay*1000, message);
}
}catch(error){
console.log('Error in AI message loop', error, result);
}
}, conf.ai.interval*1000);
}
}catch(error){
console.log('error in onReady', error);
}
});
bot.on('error', console.log);
// bot.on('message', function(message, type){
// console.log(`${type}: ${message.toString()}`)
// });
}
(async ()=>{try{
for(let name in CJbot.bots){
let bot = CJbot.bots[name];
if(bot.autoConnect){
console.log('Trying to connect', name)
console.log('Status for', name, await bot.connect());
await sleep(30000);
}
}
}catch(e){
console.log('!!!!!!!! error:', e)
}})()
// module.exports = {bot: ez, henry, owen, linda, jimin, nova, ez};