Caution: This script is incomplete and is a work in progress!

This script allows the user to browse their habitica tasks (todos, habits, and dailies). The create new task functionality still doesn't work properly.

Improvements:

  • Fix the post functionality
  • Manage checklists properly
  • Display only undone tasks
// Name: Habitica
// Description: Browse and manage your Habitica tasks, dailies, and habits
// Author: Positron
// Shortcuts:
import "@johnlindquist/kit"
// Get the user API token
const apiToken = await env("HABITICA_API_TOKEN");
// Get the user ID which is also used as the client id
const userID = await env("HABITICA_USER_ID");
const actionType = await arg("What would you like to do?", ["Browse", "Create new tasks"])
if(actionType == "Browse"){
const requestType = await arg("What would you like to browse?", ["Todos", "Dailies", "Habits"])
// if the user wants to browse their todos, then show them to him/her then let them choose one to mark as done
if(requestType == "Todos"){
const todos = await getUserData("todos")
// console.log(todos)
const todoID = await arg("Which todo would you like to mark as done?", todos.data.map(({_id, text, checklist, updatedAt}) => ({
name: text,
value: _id,
description: `Updated at ${updatedAt}`,
preview: `
<h3>${text}</h3>
<p>${checklist?.map(({text}) => text) ?? ""}</p>
`
})))
// mark the todo as done
await markTaskDone(todoID)
}
// Same thing but for dailies
else if(requestType == "Dailies"){
const dailies = await getUserData("dailys")
const dailyID = await arg("Which daily would you like to mark as done?", dailies.data.map(({_id, text, checklist, updatedAt}) => ({
name: text,
value: _id,
description: `Updated at ${updatedAt}`,
preview: `
<h3>${text}</h3>
<p>${checklist?.map(({text}) => text) ?? ""}</p>
`
})))
await markTaskDone(dailyID)
}
// Same thing but for habits
else if(requestType == "Habits"){
const habits = await getUserData("habits")
const habitID = await arg("Which habit would you like to mark as done?", habits.data.map(({_id, text, updatedAt}) => ({
name: text,
value: _id,
description: `Updated at ${updatedAt}`,
// preview: `
// <h3>${text}</h3>
// <p>${checklist?.map(({text}) => text) ?? ""}</p>
// `
})))
await markTaskDone(habitID)
}
}
// Logic for creating new tasks
else if(actionType == "Create new tasks"){
const type = await arg("What type of task would you like to create?", ["Todo", "Daily", "Habit"])
const text = await arg("What would you like the task to say?")
if (type == "Habit"){
const upOrDown = await arg("Would you like to create a positive or negative habit?", ["Positive", "Negative"])
const upOrDownBool = upOrDown == "Positive" ? true : false
await createTask(type, text, upOrDownBool)
}
else {
await createTask(type, text)
}
}
// Gets the user's tasks from Habitica
async function getUserData(type) {
const response = await get(`https://habitica.com/api/v3/tasks/user?type=${type}`, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
return response.data
}
// function to mark task as done
async function markTaskDone(id) {
// need to put an empty second argument in the post request
const response = await post(`https://habitica.com/api/v3/tasks/${id}/score/up`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`}})
// console.log(response.data)
}
// function to create a new task
async function createTask(type, text, habitDirection = undefined) {
if (habitDirection == true){
const response = await post(`https://habitica.com/api/v3/tasks/user?text=${text}&type=${type}&up=true`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
}
else if(habitDirection == false){
const response = await post(`https://habitica.com/api/v3/tasks/user?text=${encodeURI(text)}&type=${type}&down=true`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
}
else{
const response = await post(`https://habitica.com/api/v3/tasks/user?text=${encodeURI(text)}&type=${type}`, {}, {headers: {
'x-client': `${userID}`,
'x-api-user': `${userID}`,
'x-api-key': `${apiToken}`
}})
}
}