cletus/bot/bot-tasks/go-to-bed.js
2025-05-09 15:53:19 -05:00

42 lines
1.3 KiB
JavaScript

const { GoalNear } = require('mineflayer-pathfinder').goals;
const memory = require('../lib/memory');
module.exports = async function goToBed(bot, db, sayWithPersona) {
try {
const hour = bot.time.timeOfDay;
if (hour < 13000 || hour > 23999) {
sayWithPersona("someone asked you to sleep, but it's not night.");
return;
}
memory.getMemory(db, 'bed', (err, pos) => {
if (err || !pos) {
sayWithPersona("you tried to sleep but don't remember where your bed is.");
return;
}
sayWithPersona("it's nighttime. you're heading to your bed.");
bot.pathfinder.setGoal(new GoalNear(pos.x, pos.y, pos.z, 1));
const sleepCheck = setInterval(() => {
const bed = bot.findBlock({
matching: block => block.name.includes('bed'),
maxDistance: 4
});
if (bed) {
bot.sleep(bed).then(() => {
sayWithPersona("ugh, finally sleeping.");
clearInterval(sleepCheck);
}).catch(() => {
sayWithPersona("you found your bed but couldn't sleep. tragic.");
clearInterval(sleepCheck);
});
}
}, 2000);
});
} catch (err) {
console.error("go-to-bed.js failed:", err);
sayWithPersona("something broke while trying to sleep.");
}
};