PrismarineJS / mineflayer

Create Minecraft bots with a powerful, stable, and high level JavaScript API.
https://prismarinejs.github.io/mineflayer/
MIT License
4.96k stars 905 forks source link

code crashed after bot.stopDigging() #3240

Closed DUK-maybe closed 10 months ago

DUK-maybe commented 10 months ago

Versions

Detailed description of a problem

i was trying to make the bot activate the noteblock without changing its tone (the bot.activateBlock change the noteblock sound) so i was thinking of digging the block and instantly stop digging, however the bot.stopDigging() make the code crash with the error

diggingTask.cancel(new Error('Digging aborted'))
                         ^

Error: Digging aborted
    at bot.stopDigging (C:\Users\_____\Desktop\mineflayer\node_modules\mineflayer\lib\plugins\digging.js:159:26)
    at EventEmitter.<anonymous> (C:\Users\_______\Desktop\mineflayer\index.js:18:9)

What did you try yet?

i searched other same issues with error code "digging aborted" but nothing helped Did you try any method from the API? Did you try any example? Any error from those?

Your current code


const delay = ms => new Promise(res => setTimeout(res, ms));
bot.on('chat',async(username, message) => {
  if (message==="!note"){

    bot.dig(bot.blockAt(new Vec3(-10,69,-78)))
    await delay(200)
    bot.stopDigging()

  }
})

Expected behavior

It should not crash the code and the bot should stop digging PS:I am new to nodejs

Pix3lPirat3 commented 10 months ago

You should be await'ing bot.dig() instead, the functions that "returns a Promise" can be await'ed so you can wait till the task completes. Please keep https://prismarinejs.github.io/mineflayer/#/api bookmarked.

Proper:

bot.on('chat' ,async(username, message) => {
  if(message === "!note") {
    await bot.dig(bot.blockAt(new Vec3(-10, 69, -78)));
  }
})
DUK-maybe commented 10 months ago

You should be await'ing bot.dig() instead, the functions that "returns a Promise" can be await'ed so you can wait till the task completes. Please keep https://prismarinejs.github.io/mineflayer/#/api bookmarked.

Proper:

bot.on('chat' ,async(username, message) => {
  if(message === "!note") {
    await bot.dig(bot.blockAt(new Vec3(-10, 69, -78)));
  }
})

the code doesn't crash anymore but it is still digging,even i call bot.stopDigging()

Pix3lPirat3 commented 10 months ago

Well how are you trying to stop it? .dig() will throw an error if you call stopDigging, so you'd need to dig().catch() it

bot.on('chat' ,async(username, message) => {
  if(message === "!note") {
    await bot.dig(bot.blockAt(new Vec3(-10, 69, -78)));
  }
  if(message === "!stop") {
    bot.stopDigging();
  }
})
DUK-maybe commented 10 months ago

Well how are you trying to stop it? .dig() will throw an error if you call stopDigging, so you'd need to dig().catch() it

bot.on('chat' ,async(username, message) => {
  if(message === "!note") {
    await bot.dig(bot.blockAt(new Vec3(-10, 69, -78)));
  }
  if(message === "!stop") {
    bot.stopDigging();
  }
})

the .catch() worked , thank you for helping me