Advertisement
Ayasee

Habitica Armoire Script

Jan 12th, 2021
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. function autoBuyArmoire() {
  2. var habId = "user ID";
  3. var habToken = "API token";
  4. var threshold = 2000; // when your gold is more than this number, it will buy armoires until this number is reached again
  5. var sleepTime = 30000; // pause in the loop for 30000 milliseconds (30 seconds); this is to avoid the servers being overloaded
  6.  
  7. var paramsTemplate = {
  8. "method" : "get",
  9. "headers" : {
  10. "x-api-user" : habId,
  11. "x-api-key" : habToken
  12. }
  13. }
  14. var response = UrlFetchApp.fetch("https://habitica.com/api/v3/user?userFields=stats.gp", paramsTemplate);
  15. var balance = JSON.parse(response);
  16.  
  17. var purchases = Math.trunc((balance.data.stats.gp - threshold) / 100);
  18.  
  19. if (purchases > 0) {
  20. paramsTemplate = {
  21. "method" : "post",
  22. "headers" : {
  23. "x-api-user" : habId,
  24. "x-api-key" : habToken
  25. }
  26. }
  27.  
  28. // a Google script may not take more time than 6 minutes (360000 milliseconds), so we need to limit the maximum amount of purchases, or else you'll get errors
  29. var maxPurchases = (360000 / sleepTime) - 1;
  30.  
  31. if (purchases > maxPurchases) {
  32. purchases = maxPurchases;
  33. }
  34.  
  35. // whether you buy one more armoire after reaching the threshold, or staying above it, depends on the part between "var i = 0" and "i++"
  36. // if you want to stay above your gold threshold, it needs to be "i < purchases"
  37. // if you want to go slightly below your gold threshold, it needs to be "i <= purchases"
  38. for (var i = 0; i < purchases; i++) {
  39. UrlFetchApp.fetch("https://habitica.com/api/v3/user/buy-armoire", paramsTemplate);
  40. Utilities.sleep(sleepTime);
  41. }
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement