Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function autoBuyHealthPotion() {
- var habId = "Habitica User ID";
- var habToken = "Habitica API Token";
- var threshold = 35; // when your health is less than this number, it will buy potions
- var multiPotion = true; // if this is true, the script will additionally calculate how many potions need to be bought
- var oneMorePot = true; // better safe than sorry mode - buy one more potion than needed
- var sleepTime = 30000; // pause in the loop for 30000 milliseconds (30 seconds); this is to avoid the servers being overloaded
- var paramsTemplate = {
- "method" : "get",
- "headers" : {
- "x-api-user" : habId,
- "x-api-key" : habToken
- }
- }
- var response = UrlFetchApp.fetch("https://habitica.com/api/v3/user?userFields=stats.hp", paramsTemplate);
- var balance = JSON.parse(response);
- var hpValue = balance.data.stats.hp;
- if (hpValue <= threshold) {
- paramsTemplate = {
- "method" : "post",
- "headers" : {
- "x-api-user" : habId,
- "x-api-key" : habToken
- }
- }
- if (multiPotion) {
- var purchases = (50 - hpValue) / 15; // health potion restores 15 health
- if (oneMorePot && !(hpValue + purchases * 15 == 50)) { purchases++; }
- for (var i = 0; i < purchases; i++) {
- UrlFetchApp.fetch("https://habitica.com/api/v3/user/buy-health-potion", paramsTemplate);
- Utilities.sleep(sleepTime);
- }
- } else {
- UrlFetchApp.fetch("https://habitica.com/api/v3/user/buy-health-potion", paramsTemplate);
- }
- }
- }
Add Comment
Please, Sign In to add comment