// bot-tasks/TaskTemplate.js const { getBot, getAllowDestruction, setAllowDestruction, getStateMachine } = 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'); // Optional: name of the task (same as filename) const taskName = 'task-template'; module.exports = async function TaskTemplate() { const bot = getBot(); console.log(`[TASK] Starting ${taskName}`); setAllowDestruction(true); // 🔓 allow digging/building temporarily try { // Your logic here — Example: walk to a random spot const dx = Math.floor(Math.random() * 10 - 5); const dz = Math.floor(Math.random() * 10 - 5); const target = bot.entity.position.offset(dx, 0, dz); const { GoalNear } = require('mineflayer-pathfinder').goals; bot.pathfinder.setGoal(new GoalNear(target.x, target.y, target.z, 1)); bot.chat(`I'm working on ${taskName}`); // Simulate a completed task db.run(`INSERT INTO tasks (action, outcome, score) VALUES (?, 'success', 0)`, [taskName]); } catch (err) { console.error(`[TASK] ${taskName} failed:`, err.message); bot.chat(`Task ${taskName} failed.`); // Log failure to DB db.run(`INSERT INTO tasks (action, outcome, score) VALUES (?, 'failed', -1)`, [taskName]); // Optional: Trigger rewrite if consistently failing const scriptPath = path.join(__dirname, `${taskName}.js`); const original = fs.readFileSync(scriptPath, 'utf8'); const prompt = `This script failed:\n${original}\nImprove it to avoid failure. Only return code.`; const response = await chatWithAI(prompt, config.ai); fs.writeFileSync(scriptPath, response); console.log(`[AI] Rewrote task: ${taskName}`); } finally { setAllowDestruction(false); // 🔒 disable again for safety // Return to idle after completion setTimeout(() => { getStateMachine().transition('Idle'); }, 10000); } };