fix(eater): 🥅 catch async error on full food

Attempt to fix async error returned by `bot.consume`.
Fixed by wrapping in a `try{}` block and using `.catch`.
Still don't know why or how this works 🤷.
This commit is contained in:
jay 2021-03-22 17:32:56 +05:00
parent 1e82045221
commit 1597acca72

View File

@ -2,11 +2,7 @@ let cfg = {}
let bot = {} let bot = {}
let isEating = false let isEating = false
function callbackHandle(err) { function eat(callback = e => e && console.error(e)) {
if (err) console.error(err)
}
function eat(callback) {
isEating = true isEating = true
const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name) const foodNames = require('minecraft-data')(bot.version).foodsArray.map((item) => item.name)
@ -37,23 +33,23 @@ function eat(callback) {
bot.equip(best_food, 'hand', function (error) { bot.equip(best_food, 'hand', function (error) {
if (error) { if (error) {
console.error(error) console.warn(error, best_food)
isEating = false isEating = false
bot.emit('eat_stop') bot.emit('eat_stop')
} else { } else {
bot.consume(function (err) { try {
if (err) { bot.consume().catch(error => {
console.error(err) if (error.message === "Food is full") {
console.warn(error, best_food)
} else {
return callback({ error, best_food })
}
}).finally(() => {
isEating = false isEating = false
bot.emit('eat_stop') bot.emit('eat_stop')
return callback(err) })
} else { } catch { }
isEating = false if (bot.food !== 20) eat(callback)
bot.emit('eat_stop')
callback(null)
if (!bot.food === 20) eat(callbackHandle)
}
})
} }
}) })
} }
@ -76,7 +72,7 @@ function checkFood() {
// TODO implement better idle state // TODO implement better idle state
) || true // idle most likely ) || true // idle most likely
) { ) {
eat(callbackHandle) eat()
} }
} }
} }