forked from ramotar/habitica-gas-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.gs
212 lines (169 loc) · 6.67 KB
/
setup.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/**
* Example script v1.2.3 by John Doe
*
* See Wiki page for info & setup instructions:
* https://habitica.fandom.com/wiki/
*/
/* ========================================== */
/* [Users] Required script data to fill in */
/* ========================================== */
const USER_ID = "PasteYourUserIdHere";
const API_TOKEN = "PasteYourApiTokenHere";
// IMPORTANT: Do not share your API token with anyone!
/* ========================================== */
/* [Users] Required customizations to fill in */
/* ========================================== */
// [Authors] Place all mandatory user-modified variables here
// - e.g. skill to use, number of times to use, task to use skill on, etc.
/* ========================================== */
/* [Users] Optional customizations to fill in */
/* ========================================== */
// [Authors] Place all optional user-modified variables here
// - e.g. enable/disable notifications, enable/disable script features, etc.
/* ========================================== */
/* [Users] Do not edit code below this line */
/* ========================================== */
// [Authors] Place your user ID and script name here
// - This is used for the "X-Client" HTTP header
// - See https://habitica.fandom.com/wiki/Guidance_for_Comrades#X-Client_Header
const AUTHOR_ID = "b477462a-5bb5-4040-9505-f0b049b4f0bb";
const SCRIPT_NAME = "HabiticaGASTemplate";
// [Authors] Add global variables here
// - Note that these do not persist in between script calls
// - If you want to save values between calls, use PropertiesService
// - See https://developers.google.com/apps-script/reference/properties/properties-service
const scriptProperties = PropertiesService.getScriptProperties();
/* =================================== */
/* [Authors] Below you find functions, */
/* that are only used once during */
/* installation, update or removal */
/* =================================== */
function install() {
// [Authors] These are one-time initial setup instructions that we'll ask
// the user to manually execute only once, during initial script setup
// - Add triggers and webhooks for your script to service the events you care about
// - Feel free to do all other one-time setup actions here as well
// e.g. creating tasks, reward buttons, etc.
// check, if setup was already executed
if (!getInstallTime()) {
// if all options entered by the user are valid
if (validateOptions()) {
// create triggers
createTriggers();
// create webhooks
createWebhooks();
// save the time the installation was completed
updateInstallTime();
logInfo("Installation of the script succesfully finished!");
}
}
else {
logError("Installation of the script was already executed before")
}
}
function uninstall() {
// [Authors] These are one-time instructions that we'll tell the user to
// execute during script removal
// - Add deleteWebhooks() here, if you created a webhook during initial setup
// - Remove all other permanent changes the script has introduced during initial
// setup and normal use
// delete triggers
deleteTriggers();
// delete webhooks
deleteWebhooks();
// remove the install time
deleteInstallTime();
logInfo("Removal of the script succesfully finished!");
}
function update() {
// [Authors] This function updates the script after the user changed settings.
// - It simply uninstalls and installs again.
uninstall();
install();
}
function createTriggers() {
// [Authors] This function is used to create your necessary triggers
// - Below you find an example trigger, that recurs every hour
// - Feel free to modify this trigger or add additional triggers
logInfo("Creating triggers");
ScriptApp.newTrigger("processTrigger")
.timeBased()
.everyHours(1)
.create();
}
function createWebhooks() {
// [Authors] This function is used to create webhooks to your script
// - Below you find an example webhook, that gets called, when a task is scored
// - Feel free to modify this webhook or add additional webhooks
logInfo("Creating webhooks");
let webhookData = {
"type": "taskActivity",
"options": {
"scored": true
}
}
api_createWebhook(webhookData);
}
function deleteTriggers() {
// [Authors] This function deletes all existing triggers for your script
let triggers = ScriptApp.getProjectTriggers();
if (triggers.length > 0) {
logInfo("Deleting triggers");
for (let trigger of triggers) {
ScriptApp.deleteTrigger(trigger);
}
}
}
function deleteWebhooks() {
// [Authors] This function deletes all existing webhooks to your script
let response = api_fetch("https://habitica.com/api/v3/user/webhook", GET_PARAMS);
let obj = parseJSON(response);
let webhooks = obj.data;
if (webhooks.length > 0) {
console.log("Deleting webhooks");
let webAppURL = getWebAppURL();
for (let webhook of webhooks) {
if (webhook.url == webAppURL) {
api_fetch("https://habitica.com/api/v3/user/webhook/" + webhook.id, DELETE_PARAMS);
}
}
}
}
function validateOptions() {
// [Authors] This function is used to validate the options entered by the user
// - Validation of the predefined script data is already programmed
// - Usually check for the right type and value
let valid = true;
if (typeof INT_USER_ID !== "string" || !TOKEN_REGEXP.test(INT_USER_ID)) {
logError("USER_ID must equal your Habitica User ID.\n\ne.g. const USER_ID = \"12345678-90ab-416b-cdef-1234567890ab\";\n\nYour Habitica User ID can be found at https://habitica.com/user/settings/api");
valid = false;
}
if (typeof INT_API_TOKEN !== "string" || !TOKEN_REGEXP.test(INT_API_TOKEN)) {
logError("API_TOKEN must equal your Habitica API Token.\n\ne.g. const API_TOKEN = \"2345678-90ab-416b-cdef-1234567890ab\";\n\nYour Habitica API Token can be found at https://habitica.com/user/settings/api");
valid = false;
}
// test credentials
if (valid) {
valid = testCredentials();
}
if (!valid) {
logInfo("Please fix the above errors, create a new version of the deployment, and run the doOneTimeSetup() function again.\nIf you aren't sure how to do this, see \"Changing the Settings\" in the documentation for this script.");
}
return valid;
}
function testCredentials() {
// [Authors] This function tests the user credentials
try {
api_getUser();
}
catch (error) {
if (error.message.startsWith("Request failed") && error.cause.getResponseCode() == 401) {
logError("Your USER_ID and/or API_TOKEN is incorrect. Both of these can be found at https://habitica.com/user/settings/api");
return false;
}
else {
throw error;
}
}
return true;
}