cletus/bot/bot-tasks/find-flower.js
roberts 2a9f9159bd
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 26s
Massive Overhaul. Added state machine.
2025-05-10 12:24:59 -05:00

54 lines
1.9 KiB
JavaScript

const { getBot, getStateMachine, setAllowDestruction } = require('../core/context');
const db = require('../db');
const path = require('path');
const fs = require('fs');
const { chatWithAI } = require('../lib/ai-helper');
const config = require('../config.json');
const taskName = 'find-flower';
module.exports = async function FindFlower() {
const bot = getBot();
console.log(`[TASK] Starting ${taskName}`);
setAllowDestruction(false);
try {
const flowerNames = [
'dandelion', 'poppy', 'blue_orchid', 'allium',
'azure_bluet', 'red_tulip', 'orange_tulip', 'white_tulip',
'pink_tulip', 'oxeye_daisy', 'cornflower', 'lily_of_the_valley'
];
const flower = bot.findBlock({
matching: block => flowerNames.includes(block.name),
maxDistance: 32
});
if (!flower) {
bot.chat("I couldn't find a flower nearby.");
db.run(`INSERT INTO tasks (action, outcome, score) VALUES (?, 'not-found', 0)`, [taskName]);
return;
}
const { GoalNear } = require('mineflayer-pathfinder').goals;
bot.pathfinder.setGoal(new GoalNear(flower.position.x, flower.position.y, flower.position.z, 1));
bot.chat("Heading to a flower...");
db.run(`INSERT INTO tasks (action, outcome, score) VALUES (?, 'success', 1)`, [taskName]);
} catch (err) {
console.error(`[TASK] ${taskName} failed:`, err.message);
bot.chat(`Task ${taskName} failed.`);
db.run(`INSERT INTO tasks (action, outcome, score) VALUES (?, 'failed', -1)`, [taskName]);
const scriptPath = path.join(__dirname, `${taskName}.js`);
const original = fs.readFileSync(scriptPath, 'utf8');
const prompt = `This script called '${taskName}' failed. Please improve it:\n\n${original}`;
const rewritten = await chatWithAI(prompt, config.ai);
fs.writeFileSync(scriptPath, rewritten);
} finally {
setTimeout(() => {
getStateMachine().transition('Idle');
}, 10000);
}
};