FANDOM



Description Edit

Google Apps Script is a scripting language based on JavaScript that lets you manipulate Habitica and other G Suite products like Calendar, Docs, Sheets, Slides, and Forms. There's nothing to install — code editor right in your browser, and your scripts run on Google's servers. All scripts can be schedule to give a level of autonomy.

Usage Edit

In order to user this feature, you need to have a Gmail account. It is recommended that you keep all scripts private, because you need to embed your Habitica User Id and API Token in the script.

Other than the Auto Cron script, no other scripts will cause you to cron. For example if you cast a party buff after your Custom Day Start, you will still have to either run the Auto Cron or use Habitica website/apps to cron.

When scheduling the scripts, do not schedule them to run more than hourly. Be nice to Habitica servers! When setting up a script for the first time, it may be best to run the script manually by choosing Run => Run function => [Script to be scheduled] to check that it runs without errors before scheduling it.

Auto Cron Edit

by cTheDragons

Run after your Custom Day Start, this script will Cron you automatically. Please note, using this method will not allow you to use the Record Yesterday's Activities. Any dailies not ticked off at the time of cronning will be marked as missed.

  • Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
  • Paste the code snippet below into the editor, replacing the spaces marked #HabiticaUserID# and #HabiticaAPIToken# with Habitica User Id and API Token (Leave the quotes). These can be found under the API tab in your Habitica settings.
 function scheduleCron() {
   var habId = "#HabiticaUserID#";
   var habToken = "#HabiticaAPIToken#";
 
   paramsTemplate = {
     "method" : "post",
     "headers" : {
       "x-api-user" : habId, 
       "x-api-key" : habToken
     }
   }
   
   var params = paramsTemplate;
   UrlFetchApp.fetch("https://habitica.com/api/v3/cron", params)
 }
  • Under Edit, select Current project's triggers. Then add a trigger that runs scheduleCron Time-driven on a Day timer with the parameter of same hour starting with your Custom Day Start. For example if you Custom Day Start is midnight set your tirgger to Midnight to 1am.
  • Save and you're done!

If you have cron before the script has run, the script will not do anything. You can use this feature as a catch all in case you miss your regular time. For example if your Custom Day Start is set to midnight, you can set this script to run at 3am to 4am. This will allow you cron manually between midnight and 3am, and thus use the Record Yesterday's Activities feature if you have a late night. Otherwise this script will cron you automatically between 3am and 4am so your party quest damage occurs before you wake up.

Auto Accept Quests Edit

by cTheDragons

Run hourly, this script will join a quest automatically if the invite is available in your party.

  • Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
  • Paste the code snippet below into the editor, replacing the spaces marked #HabiticaUserID# and #HabiticaAPIToken# with Habitica User Id and API Token (Leave the quotes). These can be found under the API tab in your Habitica settings.
 function scheduleJoinQuest() {
   var habId = "#HabiticaUserID#";
   var habToken = "#HabiticaAPIToken#";
 
   var paramsTemplate = {
     "method" : "get",
     "headers" : {
       "x-api-user" : habId, 
       "x-api-key" : habToken    
     }
   }  
   var response = UrlFetchApp.fetch("https://habitica.com/api/v3/groups/party", paramsTemplate);
   var party = JSON.parse(response);
 
   if ((party.data.quest.key != undefined) && (party.data.quest.active != true) && (party.data.quest.members[habId] == undefined)){
   paramsTemplate = {
       "method" : "post",
       "headers" : {
         "x-api-user" : habId, 
         "x-api-key" : habToken       
       }     
     }
     var params = paramsTemplate;
  
     UrlFetchApp.fetch("https://habitica.com/api/v3/groups/party/quests/accept", params)
   }
 }
  • Under Edit, select Current project's triggers. Then add a trigger that runs scheduleJoinQuest Time-driven on a Hour timer with the parameter of Every hour.
  • Save and you're done!


Schedule Casting Party Buffs Skills Edit

by cTheDragons

Run at specific times, this script will buff your party n number of times with your desired party buff. This script is deliberately designed not to check you have enough mana first so it will error if it does not meet it buffing criteria.

  • Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
  • Paste the code snippet below into the editor,
    • replacing the spaces marked #HabiticaUserID# and #HabiticaAPIToken# with Habitica User Id and API Token (Leave the quotes). These can be found under the API tab in your Habitica settings.
    • Change the ntimes to the number of buffs you wish to do each run. Currently set to 3.
    • Change the skillId to skill you wish to cast based what is describe in the comments in options. Below it currently set to Earthquake with skillId = "earth"
 function schedulePartyBuff() {
 
   var habId = "#HabiticaUserID#";
   var habToken = "#HabiticaAPIToken#";
   var ntimes = 3
   var skillId = "earth"
   /*
   Below is a list  of options of the party buff skills. Replace the value in skillId above for the skill you desire. Ensure you copy the quotes.
   See http://habitica.fandom.com/wiki/Skills for more information on skills.
   Options for skills:
     Warrior Valourous Presence (STR): "valorousPresence" 
     Warrior Intimidating Gaze (CON): "intimidate" 
     Rogue Tools of Trade (PER): "toolsOfTrade"
     Healer Protective Aura (CON): "protectAura"
     Healer Blessing (HP): "healAll"
     Mage Ethereal Surge (mana): "mpheal"
     Mage EarthQuake (INT): "earth"
   */
   var url = "https://habitica.com/api/v3/user/class/cast/" + skillId 
   var sleepTime = 200
   
   //set paramaters
   var paramsTemplate = {
     "method" : "post",
     "headers" : {
       "x-api-user" : habId, 
       "x-api-key" : habToken
     }
   }
   var params = paramsTemplate;
   
   for (var i = 0; i < ntimes; i++) { 
     UrlFetchApp.fetch(url, params)
     Utilities.sleep(sleepTime);// pause in the loop for 200 milliseconds; //This is to avoid buff being swallowed up from servers too busy.
   }
 }
  • Under Edit, select Current project's triggers. Then add a trigger that runs schedulePartyBuff Time-driven on a Day Timer with the parameter of of which time is best.
  • Click notifications, change the second parameter from daily to immediate. (This will alert you if you failed to cast the number of party buffs).
  • Save and you're done!

If you wish this to run immediately instead of waiting on a trigger, from the edit window of the project, menu choose Run => Run function => schedulePartyBuff.

Modification Edit

by Piti: As a little modification the following script limits the number of buffs to the current amount of mana. Also the script equips some desired gear before casting the skill and then returns to other gear for later purposes . The displayed modifiaction works for Warriors, equips gear with high STR values and leaves you with high INT values after the script is finished.

function PitiGearAndBuffs() {
    var habId = "#HabiticaUser-ID#";
   var habToken = "#HabiticaAPI-Token#";
    var paramsTemplate = {
      "method" : "get",
      "headers" : {
        "x-api-user" : habId, 
        "x-api-key" : habToken    
      }
    }  
   var response = UrlFetchApp.fetch("https://habitica.com/api/v3/user", paramsTemplate);
    var user = JSON.parse(response);
    var maxNumberOfBuffs = ((user.data.stats.mp)/20); //20-Wizzard(Valorous Presence) 30-Rogue(ToolsOfTheTrade)
    var head = user.data.items.gear.equipped.head; // save initial gear
    var armor = user.data.items.gear.equipped.armor;
    var weapon = user.data.items.gear.equipped.weapon;
    var shield = user.data.items.gear.equipped.shield;
   

   if (maxNumberOfBuffs >=1) {
   paramsTemplate = {
        "method" : "post",
        "headers" : {
          "x-api-user" : habId, 
          "x-api-key" : habToken       
        }     
      }
    var params = paramsTemplate;
   

   if (user.data.items.gear.equipped.head !== "head_special_2") {
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/head_special_2", params);
      head = "head_special_2"; 
      Utilities.sleep(200);
   }
   

    if (user.data.items.gear.equipped.armor !== "armor_special_finnedOceanicArmor"){
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/armor_special_finnedOceanicArmor", params);
      armor = "armor_special_finnedOceanicArmor";
      Utilities.sleep(200);
    }
  

  if (user.data.items.gear.equipped.weapon !== "weapon_warrior_6"){
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/weapon_warrior_6", params);
      weapon = "weapon_warrior_6";
      Utilities.sleep(200);
    }
   

  if (user.data.items.gear.equipped.shield !== "shield_special_lootBag"){
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/shield_special_lootBag", params);
      shield = "shield_special_lootBag";
      Utilities.sleep(200);
    }
  

  // cast Valorous Presence 4 times (or less, if you don't have enough mana)   
  for (var i = 1; i < 4; i++) { 
      if (maxNumberOfBuffs >= i) {
     UrlFetchApp.fetch("https://habitica.com/api/v3/user/class/cast/valorousPresence", params); 
       Utilities.sleep(5000);
       }   
  }
  

  

  // change battle gear a second time   
   if (head !== "head_special_2") {
   //   UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/head_special_2", params);
      Utilities.sleep(200);
   }
   

    if (armor !== "armor_special_2"){
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/armor_special_2", params);
      Utilities.sleep(200);
    }
  

  if (weapon !== "weapon_special_nomadsScimitar"){
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/weapon_special_nomadsScimitar", params);
      Utilities.sleep(200);
    }
   

  if (shield !== "shield_special_wintryMirror"){
      UrlFetchApp.fetch("https://habitica.com/api/v3/user/equip/equipped/shield_special_wintryMirror", params);
      Utilities.sleep(200);
    }
    

   

   } // end maxNumberOfBuffs
   

  }

Please note, that the desired gear has to be integrated manually. The tool Habitican Wardrobes can provide valuable information about which gear is best for your purpose and how the gear's name will be in means of the script notation. In Habitican Wardrobes please press 'Display Gear' and see results at the very bottom.

Bulk Attack Boss Edit

by cTheDragons

This script is great for Warriors and Mages that will allow you to bulk attack the quest boss.

  • Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
  • Paste the code snippet below into the editor,
    • replacing the spaces marked #HabiticaUserID# and #HabiticaAPIToken# with Habitica User Id and API Token (Leave the quotes). These can be found under the API tab in your Habitica settings.
    • Change the ntimes to how many times you wish to use a skill each run. Currently set to 8.
    • Change the skillId to skill you wish to cast based what is describe in the comments in options. Below it currently set to Burst of Flames with skillId = "fireball"
    • Change the targetId to match one of your tasks. To find the task id:
      • Open Data Display Tool
      • Go to section for Task Overview.
      • Search for the task name you wish to target. (If you are unsure which is best, check under Skills and Buffs for a recommendation based on the skill you are using.)
      • Click toggle developer data to show the task id. (It is a long string of numbers and letters like 6eb598a1-886a-4be6-a6c5-9e9fdc8f5b44 as per example below).
 function bulkAttackBoss() {
 
   var habId = "#HabiticaUserID#";
   var habToken = "#HabiticaAPIToken#";
   var ntimes = 8
   var skillId = "fireball"
   var targetId = "6eb598a1-886a-4be6-a6c5-9e9fdc8f5b44"   
   
   /*
   Below is a list  of options of the party buff skills. Replace the value in skillId above for the skill you desire. Ensure you copy the quotes.
   See http://habitica.fandom.com/wiki/Skills for more information on skills.
   Options for skills:
     Warrior Brutal Smash: "smash" 
     Mage Burst of Flames: "fireball"
   */
   var url = "https://habitica.com/api/v3/user/class/cast/" + skillId + "?targetId=" + targetId 
   var sleepTime = 200
   
   //set paramaters
   var paramsTemplate = {
     "method" : "post",
     "headers" : {
       "x-api-user" : habId, 
       "x-api-key" : habToken
     }
   }
   var params = paramsTemplate;
 
   for (var i = 0; i < ntimes; i++) { 
     UrlFetchApp.fetch(url, params)
     Utilities.sleep(sleepTime);// pause in the loop for 200 milliseconds; //This is to avoid buff being swallowed up from servers too busy.
   }
 }
  • Save the script
  • From the menu choose Run => Run function => bulkAttackBoss.

Bulk Buy Armorie Edit

by cTheDragons

This script will allow you to bulk buy from the armorie and allow you to view the results in the logs.

  • Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
  • Paste the code snippet below into the editor,
    • replacing the spaces marked #HabiticaUserID# and #HabiticaAPIToken# with Habitica User Id and API Token (Leave the quotes). These can be found under the API tab in your Habitica settings.
    • Change the ntimes to how many times you wish to buy from the armorie each run. Currently set to 5.
 function bulkBuyArmorie() {
 
   var habId = "#HabiticaUserID#";
   var habToken = "#HabiticaAPIToken#";
   var ntimes = 5
 
   var url = "https://habitica.com/api/v3/user/buy-armoire" 
   var sleepTime = 200
   var response
   
   //set paramaters
   var paramsTemplate = {
     "method" : "post",
     "headers" : {
       "x-api-user" : habId, 
       "x-api-key" : habToken
     }
   }
   var params = paramsTemplate;
 
   for (var i = 0; i < ntimes; i++) { 
     response = UrlFetchApp.fetch(url, params)
     var result = JSON.parse(response);
     Logger.log(result.message)
     if (result.data.armoire.type == 'food') {
       Logger.log("You gained " + result.data.armoire.dropText + ".")
     } else {
       Logger.log("You gained " + result.data.armoire.value + " " + result.data.armoire.type + ".")    
     }
     Utilities.sleep(sleepTime);// pause in the loop for 200 milliseconds; //This is to avoid the next buy being swallowed up from servers too busy.
   }
 }
  • Save the script
  • From the menu choose Run => Run function => bulkBuyArmorie.
  • From the menu,choose View => Logs to view what you received.

Schedule To-Dos with Google Calendar Edit

by Snickersnacker

The script will create specific to-dos in your calendar on a specified date. Effectively giving a to-do a "start date".

  • Create a new calendar in your personal Google Calendar name HabiticaReminders.
  • Go to script.google.com. If this is your first script, this will automatically create a new Google script for you and open an editor for it. Otherwise, edit an existing project by clicking the pencil icon next to it, or create another.
  • Paste the code snippet below into the editor, replacing the spaces marked #HabiticaUserID# and #HabiticaAPIToken# with Habitica User Id and API Token. (Leave the quotes). These can be found under the API tab in your Habitica settings.
 function scheduleToDos() {
   var habId = "#HabiticaUserID#";
   var habToken = "#HabiticaAPIToken#";
 
   var now = new Date();
   var events = CalendarApp.getCalendarsByName("HabiticaReminders")[0].getEventsForDay(now);
 
   var paramsTemplate = {
     "method" : "post",
     "headers" : {
       "x-api-user" : habId, 
       "x-api-key" : habToken
     }
   }
 
   for (i = 0; i < events.length; i++) {
     var params = paramsTemplate;
     params["payload"] = {
       "text" : events[i].getTitle(), 
       "type" : "todo",
       "priority" : "1.5"
     }
 
     UrlFetchApp.fetch("https://habitica.com/api/v3/tasks/user", params)
   } 
 }
  • Under Edit, select Current project's triggers. Then add a trigger that runs scheduleToDos Time-driven on a Day timer. Suggested time is after your cron, early in the morning like 3-4am.
  • Save and you're done!

At the daily time, the script will harvest all the events in a Google Calendar called HabiticaReminders scheduled for that day. It will then and add them to Habitica as medium priority to-dos. To schedule a future task, simply select the correct day in Google Calendar and add an event with the to-do text as a title. Make sure you add it to HabiticaReminders and not your default calendar. Suggestion is to hide the calendar the rest of the time, so it doesn't clutter the calendar interface.