autoeat, improved command parsing

This commit is contained in:
MaxRobinsonTheGreat 2024-01-14 14:33:25 -06:00
parent ff9c241876
commit 44fe31a66e
3 changed files with 31 additions and 10 deletions

View file

@ -2,7 +2,7 @@ import { initBot } from '../utils/mcdata.js';
import { sendRequest } from '../utils/gpt.js'; import { sendRequest } from '../utils/gpt.js';
import { History } from './history.js'; import { History } from './history.js';
import { Coder } from './coder.js'; import { Coder } from './coder.js';
import { containsCommand, executeCommand } from './commands.js'; import { containsCommand, commandExists, executeCommand } from './commands.js';
import { Events } from './events.js'; import { Events } from './events.js';
@ -20,8 +20,6 @@ export class Agent {
this.bot.on('login', async () => { this.bot.on('login', async () => {
await this.history.loadExamples(); await this.history.loadExamples();
if (!init_message)
this.bot.chat('Hello world! I am ' + this.name);
console.log(`${this.name} logged in.`); console.log(`${this.name} logged in.`);
const ignore_messages = [ const ignore_messages = [
@ -42,10 +40,17 @@ export class Agent {
this.handleMessage(username, message); this.handleMessage(username, message);
}); });
// set the bot to automatically eat food when hungry
this.bot.autoEat.options = {
priority: 'foodPoints',
startAt: 14,
bannedFood: []
};
if (init_message) { if (init_message) {
this.handleMessage('system', init_message); this.handleMessage('system', init_message);
} else { } else {
this.bot.chat('Hello world! I am ' + this.name);
this.bot.emit('finished_executing'); this.bot.emit('finished_executing');
} }
}); });
@ -77,7 +82,11 @@ export class Agent {
let command_name = containsCommand(res); let command_name = containsCommand(res);
if (command_name) { // contains query or command if (command_name) { // contains query or command
console.log('Query/Command response:', res); console.log('Command message:', res);
if (!commandExists(command_name)) {
this.history.add('system', `Command ${command_name} does not exist. Use !newAction to perform custom actions.`);
continue;
}
let pre_message = res.substring(0, res.indexOf(command_name)).trim(); let pre_message = res.substring(0, res.indexOf(command_name)).trim();

View file

@ -17,14 +17,17 @@ const argRegex = /(?:"[^"]*"|'[^']*'|[^,])+/g;
export function containsCommand(message) { export function containsCommand(message) {
const commandMatch = message.match(commandRegex); const commandMatch = message.match(commandRegex);
if (commandMatch) { if (commandMatch)
const commandName = "!"+commandMatch[1]; return commandName;
if (commandList.some((command) => command.name === commandName))
return commandName;
}
return null; return null;
} }
export function commandExists(commandName) {
if (!commandName.startsWith("!"))
commandName = "!" + commandName;
return commandMap[commandName] !== undefined;
}
// todo: handle arrays? // todo: handle arrays?
function parseCommandMessage(message) { function parseCommandMessage(message) {
const commandMatch = message.match(commandRegex); const commandMatch = message.match(commandRegex);

View file

@ -3,6 +3,8 @@ import { createBot } from 'mineflayer';
import { pathfinder } from 'mineflayer-pathfinder'; import { pathfinder } from 'mineflayer-pathfinder';
import { plugin as pvp } from 'mineflayer-pvp'; import { plugin as pvp } from 'mineflayer-pvp';
import { plugin as collectblock } from 'mineflayer-collectblock'; import { plugin as collectblock } from 'mineflayer-collectblock';
import { plugin as autoEat } from 'mineflayer-auto-eat';
const mc_version = '1.19.3' const mc_version = '1.19.3'
const mcdata = minecraftData(mc_version); const mcdata = minecraftData(mc_version);
@ -18,6 +20,8 @@ export function initBot(username) {
bot.loadPlugin(pathfinder); bot.loadPlugin(pathfinder);
bot.loadPlugin(pvp); bot.loadPlugin(pvp);
bot.loadPlugin(collectblock); bot.loadPlugin(collectblock);
bot.loadPlugin(autoEat);
return bot; return bot;
} }
@ -77,3 +81,8 @@ export function getAllBlockIds(ignore) {
} }
return blockIds; return blockIds;
} }
export function getBiomeName(bot) {
const biomeId = bot.world.getBiome(bot.entity.position);
return mcdata.biomes[biomeId].name;
}