LOG-SURROUNDINGS cooldown
All checks were successful
Deploy Cletus Bot / deploy (push) Successful in 25s

This commit is contained in:
roberts 2025-05-10 19:41:12 -05:00
parent 9ae853529d
commit 37ee4a0ac1

View file

@ -2,7 +2,6 @@ const { getBot } = require('../core/context');
const { saveMemory, getMemory } = 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',
@ -14,6 +13,16 @@ const INTERESTING_BLOCKS = [
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;
@ -26,6 +35,7 @@ module.exports = async function logSurroundings() {
const now = Date.now();
const observedTypes = new Set();
const chatSummary = {};
for (const pos of found) {
const block = bot.blockAt(pos);
@ -38,47 +48,35 @@ module.exports = async function logSurroundings() {
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) return;
saveMemory(db, label, memoryData);
if (!err && !row) saveMemory(db, label, memoryData);
});
if (!observedTypes.has(block.name)) {
observedTypes.add(block.name);
const typeLabel = `recent:${block.name}`;
await new Promise(resolve => {
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); // minutes
if (age > COOLDOWN_MINUTES) {
saveMemory(db, typeLabel, { lastSeen: now });
bot.chat(`I saw another ${block.name.replace(/_/g, ' ')} nearby.`);
}
}
resolve();
});
});
}
}
try {
const memory = await getMemoryAsync(db, typeLabel);
const age = memory ? (now - memory.lastSeen) / 60000 : Infinity;
const newTypes = Array.from(observedTypes);
if (newTypes.length > 0) {
const counts = {};
for (const pos of found) {
const name = bot.blockAt(pos)?.name;
if (newTypes.includes(name)) {
counts[name] = (counts[name] || 0) + 1;
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 summaryMsg = Object.entries(counts)
const entries = Object.entries(chatSummary);
if (entries.length > 0) {
const summary = entries
.map(([name, count]) => `${count} ${name.replace(/_/g, ' ')}`)
.join(', ');
if (summaryMsg) bot.chat(`I noticed: ${summaryMsg}`);
bot.chat(`I noticed: ${summary}`);
}
};