cletus/bot/lib/log-surroundings.js
roberts 37ee4a0ac1
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 25s
LOG-SURROUNDINGS cooldown
2025-05-10 19:41:12 -05:00

82 lines
2.3 KiB
JavaScript

const { getBot } = require('../core/context');
const { saveMemory, getMemory } = require('../memory');
const db = require('../db');
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;
// Wrap getMemory in a Promise for async/await
function getMemoryAsync(db, label) {
return new Promise((resolve, reject) => {
getMemory(db, label, (err, data) => {
if (err) return reject(err);
resolve(data);
});
});
}
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();
const chatSummary = {};
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
};
// Save exact block location if it's not already recorded
db.get(`SELECT id FROM memory WHERE label = ?`, [label], (err, row) => {
if (!err && !row) saveMemory(db, label, memoryData);
});
if (!observedTypes.has(block.name)) {
observedTypes.add(block.name);
const typeLabel = `recent:${block.name}`;
try {
const memory = await getMemoryAsync(db, typeLabel);
const age = memory ? (now - memory.lastSeen) / 60000 : Infinity;
if (age >= COOLDOWN_MINUTES) {
await saveMemory(db, typeLabel, { lastSeen: now });
chatSummary[block.name] = (chatSummary[block.name] || 0) + 1;
}
} catch (err) {
await saveMemory(db, typeLabel, { lastSeen: now });
chatSummary[block.name] = (chatSummary[block.name] || 0) + 1;
}
}
}
const entries = Object.entries(chatSummary);
if (entries.length > 0) {
const summary = entries
.map(([name, count]) => `${count} ${name.replace(/_/g, ' ')}`)
.join(', ');
bot.chat(`I noticed: ${summary}`);
}
};