119 lines
3.1 KiB
JavaScript
119 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
const axios = require('axios');
|
|
|
|
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 = [''];
|
|
|
|
let bulbaItems = await axios.get('https://webstore.bulbastore.uk/api/listings');
|
|
bulbaItems = bulbaItems.data.listings.map(i=>i.listing_name);
|
|
|
|
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 = new Ai(conf.ai.prompt(
|
|
bot.bot.entity.username,
|
|
conf.ai.interval,
|
|
Object.values(bot.getPlayers()).map(player=>`<[${player.lvl}] ${player.username}>`).join('\n'),
|
|
bulbaItems
|
|
))
|
|
|
|
|
|
setInterval(async ()=>{
|
|
let result;
|
|
// if(messages.length ===0) return;
|
|
|
|
try{
|
|
result = await aiChat.chat(JSON.stringify({
|
|
messages, currentTime:Date.now()+1}
|
|
));
|
|
}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);
|
|
if(result || result.response || result.response.text()){
|
|
console.log(result.response.text())
|
|
}
|
|
}
|
|
}, 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};
|