builder-247 / node-autotip

Commandline-based autotip client for Hypixel using mineflayer
MIT License
20 stars 6 forks source link

Temporarily disable on player join #41

Open SeasonalFerret opened 1 year ago

SeasonalFerret commented 1 year ago

When the player logs into hypixel, we get kicked with this message:

info:  Kicked for {"extra":[{"color":"red","text":"You logged in from another location!"}],"text":""}

When that happens, wait 5 minutes and check hypixel API for player online status. https://api.hypixel.net/#tag/Player-Data/paths/~1status/get

If the user is online, just wait another 5 minutes. If it says the user is offline, we can reconnect and continue.

https://github.com/builder-247/node-autotip/blob/97b0d66fe464c22af0e3335f1b1e9630646cd570/index.js#L146

WhyClove commented 9 months ago

Mines, a bit inefficient, but it gets the job done

// add this to near top to allow request module to be added
const request = require('request')

// put the  two variables below autotipSession

// Make sure to replace "INSERT_YOUR_UUID_HERE" with the uuid you want to check if they're online
const apiEndpoint = `https://api.hypixel.net/status?uuid=INSERT_YOUR_UUID_HERE`;
// i created a variable called hypixelKey in credentials.json but you can just put it in here
const apiKey = credentials.hypixelKey;

// put the below under onMessage() function

async function checkSessionStatus() {
  return new Promise((resolve) => {
    request.get({
      url: apiEndpoint,
      headers: { 'Api-Key': apiKey },
      json: true
    }, (error, response, body) => {
      if (error) {
        console.error('Error:', error);
        resolve(false);
      }

      const session = body.session;

      if (session && session.online) {
        console.log('Player is online. Waiting for 5 minutes...');
        resolve(true);
      } else {
        console.log('Player is offline. Logging in to the server...');
        resolve(false);
      }
    });
  });
}

You'd also want to replace the init function.

async function main() {
// Checks if player is online
  online = await checkSessionStatus()
  if (online == false) {
  bot = mineflayer.createBot(options);
  bot._client.once('session', session => options.session = session);
  bot.once('login', onLogin);
  bot.on('message', onMessage);
  bot.on('kicked', (reason) => {
    logger.info(`Kicked for ${reason}`);
  });
  bot.once('end', () => {
    // Schedule the main function to run again after 3 minutes of bot exiting
    setTimeout(main, 3 * 60 * 1000);
  });
} else {
  // If the player is not online, recheck after 5 minutes
  setTimeout(main, 5 * 60 * 1000);
}
}

Above the process.on stuff just put main()