PrismarineJS / mineflayer

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

Bots suddendly disconnect at the same time when creating instances inside a loop #2716

Open AdolfRoxler opened 2 years ago

AdolfRoxler commented 2 years ago

Versions

Detailed description of a problem

A clear and concise description of what the problem is, with as much context as possible. What are you building? What problem are you trying to solve?

I'm trying to get an arbitrary amount of bots inside of a server. The maximum that will go in is 7 before all of them mysteriously disconnect.

What did you try yet?

See if my way of handling requests is bad. I don't see anything bad here.

If I do it separately outside a loop everything seems to work out fine.

Did you try any method from the API? mineflayer.createBot(), bot.on(), Did you try any example? Any error from those?

Your current code


/*
function generateinstance(user,ip,port,profile,ver) {
    let u="Notch"; if(user!=="" && user!==undefined){u=user}
    let h="localhost"; if(ip!=="" && ip!==undefined){h=ip}
    let p=25565; if(port!=="" && port!==undefined){p=port}

    let instance = mineflayer.createBot({username: u,port: p,host: h})
    if (instance===undefined){console.log("wtf");return}
    let numba = bots.indexOf(instance)
    bots.push(instance)

    if (profile===true && activeprofile!==undefined){

    if (activeprofile.loginchat!=="") {instance.on("login",()=>{instance.chat(activeprofile.loginchat.toString())})}

    instance.on("error",(err)=>{
    console.log("Instance #"+(numba+1)+" had the following error: "+err)
    })

    instance.on("end",(a)=>{console.log(a)})

    if (activeprofile.disableai===false){
    instance.on("physicsTick",()=>{
    const player = (entity) => entity.type === "player"; let closestplr = instance.nearestEntity(player)
    if (!closestplr) return; instance.lookAt(closestplr.position.offset(0,closestplr.height,0))
    })

    instance.on("kicked",()=>{
    if(numba>=0){ bots.splice(numba,numba); 
    setTimeout(function(){generateinstance(u,h,p,profile)},10000)
    }});

  }
    }else{console.log("Instance #"+(numba+1)+" was to have a profile but there's none to give!")}
}

function generatebots() {
if (Object.keys(targetlist).length>0){} else {console.log("You don't have any servers to log into!"); addserver(selectserver);  return}
if (currenttarget!==undefined && currenttarget!==""){} else {console.log("You have to select a server!"); selectserver(generatebots); return}

let bulk = [{
 name: "prefix",
 type: "Input",
 message: "Type your desired bot prefix",
},
{
name: "count",
type: "Numeral",
message: "How many bots do you want to generate in bulk?",
initial: 5
},
{
name: "profile",
type: "Confirm",
message: "Do you want to use a profile?",
initial: true
},
{
 name: "delay",
 type: "Numeral",
 message: "Set a delay in between each bot (ms)",
 initial: 1
}
]

prompt(bulk)
.then(answer=>{
  let i=0

  for (i;i<answer.count;i++){
  let z=i
  setTimeout(function(){generateinstance(answer.prefix+((z).toString()),currenttarget[0],currenttarget[1],answer.profile)},answer.delay*i)}

  commandandconquer();
})
.catch()

}
*/

Expected behavior

A clear and concise description of what you expected to happen. If I input to generate 20 bots, it should generate 20 bots with the same prefix (with diff numbers) without any leaving.

Additional context

Add any other context about the problem here.

AdolfRoxler commented 2 years ago

Did some testing, every instance involved in the same loop will get kicked if it goes over 5 (??? why)

AdolfRoxler commented 2 years ago

Also, how do I prevent clients from timing out?

amoraschi commented 2 years ago

Are you waiting between creating each bot? Your code isn't really well formatted

AdolfRoxler commented 2 years ago

Are you waiting between creating each bot? Your code isn't really well formatted

I eventually just set a higher delay to 4500ms for bigger bot counts and there's no more bullshit disconnections.

This is a snippet, of course, this'll prompt you hoe many bots do you want to generate, the delay in between joins and if you want to use a common profile for eac one.

I'm not very fluid in js (in fact, I hate it, it's just mediocre at everything without any redeeming qualities with unnecessarily weird and badly documented syntax like it's c++ while being a wannabe C), I come from coding in lua.