Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function autoBuyArmoire() {
- var habId = "user ID";
- var habToken = "API token";
- var threshold = 2000; // when your gold is more than this number, it will buy armoires until this number is reached again
- 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.gp", paramsTemplate);
- var balance = JSON.parse(response);
- var purchases = Math.trunc((balance.data.stats.gp - threshold) / 100);
- if (purchases > 0) {
- paramsTemplate = {
- "method" : "post",
- "headers" : {
- "x-api-user" : habId,
- "x-api-key" : habToken
- }
- }
- // 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
- var maxPurchases = (360000 / sleepTime) - 1;
- if (purchases > maxPurchases) {
- purchases = maxPurchases;
- }
- // whether you buy one more armoire after reaching the threshold, or staying above it, depends on the part between "var i = 0" and "i++"
- // if you want to stay above your gold threshold, it needs to be "i < purchases"
- // if you want to go slightly below your gold threshold, it needs to be "i <= purchases"
- for (var i = 0; i < purchases; i++) {
- UrlFetchApp.fetch("https://habitica.com/api/v3/user/buy-armoire", paramsTemplate);
- Utilities.sleep(sleepTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement