From d8a907dc63bb21edbc2c4ef1b6e1fbe24411c5f0 Mon Sep 17 00:00:00 2001 From: roberts Date: Sat, 10 May 2025 19:28:56 -0500 Subject: [PATCH] Updated Idle and HandleChat. --- bot/states/HandleChat.js | 26 +++++++++++++++++++++++--- bot/states/Idle.js | 15 +++++++++------ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/bot/states/HandleChat.js b/bot/states/HandleChat.js index 90de8ff..a52e192 100644 --- a/bot/states/HandleChat.js +++ b/bot/states/HandleChat.js @@ -26,8 +26,11 @@ module.exports = async function HandleChat() { if (matchedTask) { try { const task = require(path.join(taskDir, matchedTask)); + const { setActiveTask, clearActiveTask } = require('../core/context'); + setActiveTask(matchedTask); bot.chat(`Okay, I’ll try to ${matchedTask.replace(/-/g, ' ')}.`); - await task(); + await task(bot, db, bot.chat); + clearActiveTask(); return; } catch (err) { console.error(`[TASK ERROR] ${matchedTask}:`, err.message); @@ -35,6 +38,23 @@ module.exports = async function HandleChat() { } } - const response = await chatWithAI(message, config.ai); - bot.chat(response); + if (!matchedTask) { + const prompt = `Player said: "${message}". If this is a command, what task name (like 'find-flower', 'chop-tree') would you execute? Otherwise, reply "none".`; + const aiResponse = await chatWithAI(prompt, config.ai); + const aiTask = aiResponse.trim().toLowerCase().replace(/\s+/g, '-'); + + if (availableTasks.includes(aiTask)) { + const task = require(path.join(taskDir, aiTask)); + bot.chat(`Okay, I’ll try to ${aiTask.replace(/-/g, ' ')}.`); + setActiveTask(aiTask); + await task(bot, db, bot.chat); + clearActiveTask(); + return; + } + } + + if (msg.includes(bot.username.toLowerCase())) { + const response = await chatWithAI(message, config.ai); + bot.chat(response); + } }; \ No newline at end of file diff --git a/bot/states/Idle.js b/bot/states/Idle.js index 9d6087c..69232b4 100644 --- a/bot/states/Idle.js +++ b/bot/states/Idle.js @@ -1,6 +1,8 @@ // states/Idle.js const { getBot, getActiveTask, isCombatLocked, getStateMachine } = require('../core/context'); - +const context = require('../core/context'); +const { chatWithAI } = require('../lib/ai-helper'); +const config = require('../config.json'); const { GoalNear } = require('mineflayer-pathfinder').goals; const { getHomeZone } = require('../memory/locations'); const db = require('../db'); @@ -43,11 +45,12 @@ module.exports = async function Idle() { const bounds = zone?.bounds || fallbackBounds; const safeRadius = Math.min(bounds.x, bounds.z); - const actionRoll = Math.floor(Math.random() * 3); - + const prompt = `You're in a safe zone. What would be a useful task to do right now? Your options are: cut grass, tend crops, patrol for mobs.`; + const response = await chatWithAI(prompt, config.ai); + // Randomize an action to do things around the home area. - if (actionRoll === 0) { + if (response.includes('grass')) { const grass = bot.findBlock({ matching: block => block.name === 'tall_grass', maxDistance: safeRadius @@ -63,7 +66,7 @@ module.exports = async function Idle() { } // manage a farm by harvesting crops. - } else if (actionRoll === 1) { + } else if (response.includes('crop') || response.includes('farm')) { const crops = bot.findBlocks({ matching: block => ['wheat', 'carrots', 'potatoes'].includes(block.name), maxDistance: safeRadius, @@ -84,7 +87,7 @@ module.exports = async function Idle() { } // Find and attack mobs. - } else { + } else if (response.includes('mob') || response.includes('patrol')) { const mob = Object.values(bot.entities).find(e => e.type === 'mob' &&