cletus/bot/lib/log-surroundings.js
roberts efbe339dbb
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 25s
updated logging surroundings
2025-05-10 19:08:16 -05:00

81 lines
No EOL
2.4 KiB
JavaScript

const { getBot } = require('../core/context');
const { saveMemory } = require('../memory');
const db = require('../db');
// Define what kinds of blocks to remember. This should probably be a variable later so that I can add to this list.
const INTERESTING_BLOCKS = [
'coal_ore', 'iron_ore', 'diamond_ore', 'gold_ore', 'emerald_ore',
'redstone_ore', 'lapis_ore', 'copper_ore',
'oak_log', 'birch_log', 'spruce_log',
'crafting_table', 'furnace', 'chest', 'anvil',
'enchanting_table', 'lectern', 'bell',
'bed', 'beacon', 'portal'
];
const COOLDOWN_MINUTES = 5;
module.exports = async function logSurroundings() {
const bot = getBot();
if (!bot) return;
const found = bot.findBlocks({
matching: block => INTERESTING_BLOCKS.includes(block.name),
maxDistance: 12,
count: 10
});
const now = Date.now();
const observedTypes = new Set();
for (const pos of found) {
const block = bot.blockAt(pos);
if (!block || !block.name) continue;
const label = `found:${block.name}:${pos.x},${pos.y},${pos.z}`;
const memoryData = {
name: block.name,
position: pos,
timestamp: now
};
// Store this exact block if not seen before
db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => {
if (err || row) return;
saveMemory(db, label, memoryData);
});
if (!observedTypes.has(block.name)) {
observedTypes.add(block.name);
const typeLabel = `recent:${block.name}`;
getMemory(db, typeLabel, (err, memory) => {
if (err || !memory) {
saveMemory(db, typeLabel, { lastSeen: now });
bot.chat(`I noticed ${block.name.replace(/_/g, ' ')} nearby.`);
} else {
const age = (now - memory.lastSeen) / (1000 * 60); // in minutes
if (age > COOLDOWN_MINUTES) {
saveMemory(db, typeLabel, { lastSeen: now });
bot.chat(`I saw another ${block.name.replace(/_/g, ' ')} nearby.`);
}
}
});
}
}
if (found.length > 0) {
const summary = found
.map(pos => bot.blockAt(pos)?.name)
.filter(Boolean)
.reduce((acc, name) => {
acc[name] = (acc[name] || 0) + 1;
return acc;
}, {});
const summaryMsg = Object.entries(summary)
.map(([name, count]) => `${count} ${name.replace(/_/g, ' ')}`)
.join(', ');
bot.chat(`I noticed: ${summaryMsg}`);
}
};