Auto Force-Start Quests

== Description ==
Below are three alternative versions of a script that force-starts pending quests: 
* one for parties where members who want to participate are expected to join the quest soon after the invite has been sent (practically ones where everyone uses the auto accept script) 
* two for parties where a longer time window is to be given for members to manually join quests
Only party leader can force-start all quests in the party, so it is recommended that the script is run by the party leader. Another option is to have all people who send quest invites run their own script.
== Installation instructions ==
* 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 (choose one from the twoalternatives) 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.
* Under Edit, select Current project's triggers. Then add a trigger that runs forceStartQuest Time-driven, see suggested values for each script.
* Save and you're done!
== Version for Parties Mandating Auto Accept Script Use ==
by justuskraft
This script force-starts a pending quest after 1 to 2 hours. It is best used in combination with the auto accept quest script. Everyone who wants to participate in the quest can do so by enabling auto accept quest. If some people don't want to take part, the quest owner doesn't have to start the quest manually after said time. The Time-driven trigger for this script should be set to an Hour timer with the parameter of Every hour.
 function forceStartQuest() { 
 var habId = "#HabiticaUserID#";
 var habToken = "#HabiticaAPIToken#";
 var scriptProperties = PropertiesService.getScriptProperties();
 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 (scriptProperties.getProperty('PENDING_QUEST') == 'true') {
 scriptProperties.setProperty('PENDING_QUEST', 'false');
 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/force-start", params);
 } else if ((party.data.quest.key != undefined) && (party.data.quest.active != true)) {
 scriptProperties.setProperty('PENDING_QUEST', 'true');
 } else {
 scriptProperties.setProperty('PENDING_QUEST', 'false');
 }
 }
== Version Starting the Quest After a Specified Time ==
by Antonbury
This script can be configured to wait for a number of hours before starting the quest, making it possible to automatically start quests in parties where all members do not use the auto accept script. This script is based on the no-delay version of the script, which has been created by justuskraft.
To set the delay between the invite being noticed by the script and the quest being started, edit the value of time_to_join_hours. For example, to give party members 5 hours to join the quest, the fourth line of the script should be:
var time_to_join_hours = 5
The part on the original line after // is a comment and does not affect the script, it can be left there or removed.
Setting the parameters for the Time-driven trigger for this script is a bit more free than for the script above, as script running interval does not singularly dictate the quest starting delay. Long intervals do increase the time from invite being sent out to the script noticin it, though. Thus, a relatively short interval can be handy, especially if the time frame for joining the quests is reasonably short. For example, Minute timer with the value of Every 15 minutes has worked well.
 // Edit the three lines below to match your needs
 var habId = "#HabiticaUserID#"; // Your Habitica ID: found under settings -> API -> user ID
 var habToken = "#HabiticaAPIToken#"; // Your Habitica API token: found under settings -> API -> Show API Token
 var time_to_join_hours = 5 // How many hours should the quest invite at least be out before force starting
 
 
 // Do not edit anything below (unless you know what you are doing)
 
 var time_to_join_ms = time_to_join_hours*60*60*1000;
 var api_headers = {
 "x-client": "f687a6c7-860a-4c7c-8a07-9d0dcbb7c831-QuestForceStarter",
 "x-api-user": habId,
 "x-api-key": habToken
 }
 
 function forceStartQuest() {
 var scriptProperties = PropertiesService.getScriptProperties();
 var params = {
 "method": "get",
 "headers": api_headers,
 }
 
 var response = UrlFetchApp.fetch("https://habitica.com/api/v3/groups/party", {"method": "get", "headers": api_headers});
 var party = JSON.parse(response);
 var current_time = new Date();
 
 if ((party.data.quest.key == undefined) || (party.data.quest.active == true)) { // either there's no invite out, or the quest has already begun
 scriptProperties.setProperty('PENDING_QUEST', 'false');
 return;
 }
 
 if (scriptProperties.getProperty('PENDING_QUEST') == 'true') { // there's an active invite we have seen before
 var invite_time = current_time - Date.parse(scriptProperties.getProperty('INVITE_TIMESTAMP')); // how long the invite is known to have been out (in ms)
 if (invite_time > time_to_join_ms) {
 UrlFetchApp.fetch("https://habitica.com/api/v3/groups/party/quests/force-start", {"method": "post", "headers": api_headers});
 scriptProperties.setProperty('PENDING_QUEST', 'false');
 }
 } else if ((party.data.quest.key != undefined) && (party.data.quest.active != true)) { // there's an active invite we haven't seen before
 scriptProperties.setProperty('PENDING_QUEST', 'true');
 scriptProperties.setProperty('INVITE_TIMESTAMP', current_time);
 }
 }
== Version Starting the Quest Based on Number of Participants ==
by Antonbury
This version of the script starts the quest when a certain number of party members have accepted the invite. That way, if the party for example has known inactive members, the quest can be started automatically as soon as all active members have accepted the invite: just set the required_participants to the number of active members in your party.
If you are not the party leader, you can still force-start quests for which you sent the invite out yourself. In that case, set own_quests_only to true.
This script should be run with a time-driven trigger similar to the one for the time-based starting script. I recommend Minute timer with Every 15 minutes or some other shortish interval.
 // Edit the four lines below to match your needs
 var habId = "#HabiticaUserID#"; // Your Habitica ID: found under settings -> API -> user ID
 var habToken = "#HabiticaAPIToken#"; // Your Habitica API token: found under settings -> API -> Show API Token
 var required_participants = 10 // How many people should have accepted the quest invite before force starting
 var own_quests_only = false; // Change the 'false' here to 'true' if you only want to auto-start your own quests
 
 
 // Do not edit anything below (unless you know what you are doing)
 var api_headers = {
 "x-client": "f687a6c7-860a-4c7c-8a07-9d0dcbb7c831-QuestForceStarter_participants",
 "x-api-user": habId,
 "x-api-key": habToken
 }
 
 function forceStartQuest() {
 
 var params = {
 "method": "get",
 "headers": api_headers,
 }
 var response = UrlFetchApp.fetch("https://habitica.com/api/v3/groups/party", params);
 var party = JSON.parse(response);
 
 
 if (own_quests_only && party.data.quest.leader != habId) {
 return;
 }
 
 if ((party.data.quest.key != undefined) && (party.data.quest.active != true)) {
 if (Object.values(party.data.quest.members).filter(Boolean).length >= required_participants) {
 var params = {
 "method": "post",
 "headers": api_headers,
 }
 UrlFetchApp.fetch("https://habitica.com/api/v3/groups/party/quests/force-start", params);
 }
 }
 }
Category:Extensions, Add-Ons, and Customizations