Habitica Wiki
Habitica Wiki
Advertisement
Habitica Wiki



Description[]

Run as often as you want damage to be dealt to you. Checks if a specified dailies' streak is 0 and clicks a specified negative habit when it is the case.

Installation[]

  • 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
    • Replace 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.
    • You can replace Poison and Poisoned task with the names of existing tasks. If you don't the tasks specified in the script will be created.
 var habId = "#HabiticaUserID#";
 var habToken = "#HabiticaAPIToken#";
 var habitToCheck = 'Poison'
 var taskName = 'Poisoned task'
 
 
 function scheduleCron() {
  const getParams = {
    "method" : "get",
    "headers" : {
      "x-api-user" : habId, 
      "x-api-key" : habToken
    }
  };
  let response = UrlFetchApp.fetch("https://habitica.com/api/v3/tasks/user?type=dailys", getParams)
  response = JSON.parse(response.getContentText())
  Logger.log('Searching for the task: ' + taskName)
  for (task of response.data) {
    if (task.text.trim().toLowerCase() === taskName.trim().toLowerCase()){
      Logger.log('Found task')
      if(task.streak === 0.0) {
        Logger.log('Streak is 0. Deducting health...')
        response = UrlFetchApp.fetch("https://habitica.com/api/v3/tasks/user?type=habits", getParams)
        response = JSON.parse(response.getContentText())
        habit = checkHabitExists(response)
        if(habit) {
          scoreHabit(habit.id)
        } else {
          id = createHabit()
          scoreHabit(id)
        }
      }
    }
  }
 }
 function scoreHabit(id) {
 const postParams = {
    "method" : "post",
    "headers" : {
      "x-api-user" : habId, 
      "x-api-key" : habToken
    }
  };
  Logger.log('Scoring the habit...')
  response = UrlFetchApp.fetch(`https://habitica.com/api/v3/tasks/${id}/score/down`, postParams)
  if(response.getResponseCode() === 200) {
    Logger.log('Scoring successful')
  } else {
    Logger.log('There was an issue scoring the habit: ' + response.getContentText())
  }
 }
 function createHabit() {
 const postParams = {
    "method" : "post",
    "headers" : {
      "x-api-user" : habId, 
      "x-api-key" : habToken
    },
    'payload' : {
      'text': habitToCheck,
      'type': 'habit',
      'up': false
    }
  };
 Logger.log('Habit not yet created, creating one...')
 response = UrlFetchApp.fetch("https://habitica.com/api/v3/tasks/user", postParams)
 if(response.getResponseCode() === 201) {
   Logger.log('Habit created successfully')
   response = JSON.parse(response.getContentText())
   return response.data["_id"]
 }
 }
 function checkHabitExists(response) {
 for (habit of response.data) {
   if (habit.text.trim().toLowerCase() === habitToCheck.trim().toLowerCase()) {
     return habit
   }
 }
 return null
 }
  • Under Edit, select Current project's triggers. Then add a trigger that runs scheduleCron Time-driven on an Hour Timer with the parameter how often you'd like it.
  • Save and you're done!
  • You can run several instances of this script if you'd like several tasks to be held accountable to the degree this allows. Just repeat steps above.

If you wish this to run immediately instead of waiting on a trigger, from the edit window of the project, menu choose RunRun functionscheduleCron.

Advertisement