Ayasee

Habitica Auto Health Potions

May 22nd, 2022 (edited)
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function autoBuyHealthPotion() {
  2.   var habId = "Habitica User ID";
  3.   var habToken = "Habitica API Token";
  4.   var threshold = 35; // when your health is less than this number, it will buy potions
  5.   var multiPotion = true; // if this is true, the script will additionally calculate how many potions need to be bought
  6.   var oneMorePot = true; // better safe than sorry mode - buy one more potion than needed
  7.   var sleepTime = 30000; // pause in the loop for 30000 milliseconds (30 seconds); this is to avoid the servers being overloaded
  8.  
  9.   var paramsTemplate = {
  10.     "method" : "get",
  11.     "headers" : {
  12.       "x-api-user" : habId,
  13.       "x-api-key" : habToken    
  14.     }
  15.   }
  16.   var response = UrlFetchApp.fetch("https://habitica.com/api/v3/user?userFields=stats.hp", paramsTemplate);
  17.   var balance = JSON.parse(response);
  18.   var hpValue = balance.data.stats.hp;
  19.  
  20.   if (hpValue <= threshold) {
  21.     paramsTemplate = {
  22.       "method" : "post",
  23.       "headers" : {
  24.         "x-api-user" : habId,
  25.         "x-api-key" : habToken
  26.       }
  27.     }
  28.    
  29.     if (multiPotion) {
  30.       var purchases = (50 - hpValue) / 15; // health potion restores 15 health
  31.  
  32.       if (oneMorePot && !(hpValue + purchases * 15 == 50)) { purchases++; }
  33.  
  34.       for (var i = 0; i < purchases; i++) {
  35.         UrlFetchApp.fetch("https://habitica.com/api/v3/user/buy-health-potion", paramsTemplate);
  36.         Utilities.sleep(sleepTime);
  37.       }
  38.     } else {
  39.       UrlFetchApp.fetch("https://habitica.com/api/v3/user/buy-health-potion", paramsTemplate);
  40.     }
  41.   }
  42. }
Add Comment
Please, Sign In to add comment