-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (243 loc) · 8.06 KB
/
index.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
const fs = require("fs");
const path = require("path");
const axios = require("axios");
var config = require("./config.json");
var cookie;
const VER = "0.1.4";
const UPDATE_CHANNEL = "https://github.com/alexinabox/hoyolab-claimer-js/releases/latest";
// SETUP LOGGING
const logFilePath = path.join('./botlog.txt');
const log = fs.createWriteStream(logFilePath, { flags: 'a+' });
// SETUP CONFIG
const config_params = [
"COOKIE",
"SERVER_UTC",
"DELAY_MINUTE",
"RANDOMIZE",
"RANDOM_RANGE",
"ACT_ID",
"DOMAIN_NAME",
];
async function checkConfig() {
try {
config = JSON.parse(fs.readFileSync(path.join("./config.json"), "utf8"));
config_params.forEach((param) => {
if (!(param in config)) {
throw new Error(`ERROR: Broken config file, ${param} not found`);
}
});
} catch (e) {
console.log(e);
sendErrorToDiscord(e);
console.log("Config not found/corrupted! Making default config...");
config = {
COOKIE: [],
SERVER_UTC: 8,
DELAY_MINUTE: 0,
RANDOMIZE: false,
RANDOM_RANGE: 3600,
ACT_ID: "e202102251931481",
DOMAIN_NAME: ".mihoyo.com"
};
config_file = fs.createWriteStream(path.join("./config.json"), { flags: "w" });
await config_file.write(JSON.stringify(config, null, 4));
config_file.close();
}
await new Promise(resolve => setTimeout(resolve, 1000));
return;
}
// END SETUP CONFIG
// GET COOKIE
function getCookie() {
cookie = config.COOKIE;
if (cookie == "") {
console.log("ERROR: Cookie not found! Please fill in your cookie in config.json");
process.exit(1);
}
}
// END GET COOKIE
// API CALLS
function getDailyStatus(cookie) {
const headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Origin': 'https://webstatic-sea.mihoyo.com',
'Connection': 'keep-alive',
'Referer': `https://webstatic-sea.mihoyo.com/ys/event/signin-sea/index.html?act_id=${config.ACT_ID}&lang=en-us`,
'Cache-Control': 'max-age=0',
'Cookie': cookie
}
const params = {
lang: 'en-us',
act_id: config.ACT_ID
}
return axios.get('https://hk4e-api-os.mihoyo.com/event/sol/info', { headers, params })
.then(response => response.data)
.catch(error => {
console.log('CONNECTION ERROR: cannot get daily check-in status');
console.log(error);
log.write('CONNECTION ERROR: cannot get daily check-in status\n');
log.write(error + '\n');
return null;
});
}
async function isClaimed(cookie) {
try {
return getDailyStatus(cookie)
.then(resp => resp ? resp.data.is_sign : null);
}
catch (e) {
console.log(e);
return null;
}
}
function claimReward(cookie) {
const headers = {
'Accept': 'application/json, text/plain, */*',
'Accept-Language': 'en-US,en;q=0.5',
'Content-Type': 'application/json;charset=utf-8',
'Origin': 'https://webstatic-sea.mihoyo.com',
'Connection': 'keep-alive',
'Referer': `https://webstatic-sea.mihoyo.com/ys/event/signin-sea/index.html?act_id=${config.ACT_ID}&lang=en-us`,
'Cookie': cookie
}
const params = {
lang: 'en-us'
}
const data = {
act_id: config.ACT_ID
}
return axios.post('https://hk4e-api-os.mihoyo.com/event/sol/sign', data, { headers, params })
.then(response => response.data)
.catch(error => {
console.log('CONNECTION ERROR: cannot claim daily check-in reward');
console.log(error);
log.write('CONNECTION ERROR: cannot claim daily check-in reward\n');
log.write(error + '\n');
return null;
});
}
// END API CALLS
// MAIN
async function main() {
console.log(`Hoyolab Daily Claimer v${VER}`);
console.log(`Checking for updates...`);
await axios.get(UPDATE_CHANNEL)
.then(response => {
const latest = response.request.res.responseUrl;
const latest_ver = latest.split('/').pop();
if (latest_ver != VER) {
console.log(`New version available: ${latest_ver}`);
console.log(`Please update at: ${latest}`);
} else {
console.log(`You are up to date!`);
}
})
.catch(error => {
console.log(`ERROR: Failed to check for updates`);
console.log(error);
sendErrorToDiscord(error);
});
getCookie();
for (let i = 0; i < cookie.length; i++) {
isClaimed(cookie[i])
.then(resp => {
if (resp) {
console.log('Aleady claimed reward for account ' + (i + 1) + ' today!');
log.write('Already claimed reward for account ' + (i + 1) + 'today!\n');
sendWebhookFail(i);
return;
} else {
claimReward(cookie[i])
.then(resp => {
if (resp.retcode == 0) {
console.log('Successfully claimed reward!');
log.write('Successfully claimed reward!\n');
sendWebhookSuccess(i);
return;
} else {
console.log('ERROR: Failed to claim reward!');
log.write('ERROR: Failed to claim reward!\n');
sendWebhookFail(i);
return;
}
});
}
});
}
}
//Discord Webhook event announcer
async function sendWebhookSuccess(i) {
fetch(config.WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"content": null,
"embeds": [
{
"title": "Hoyolab Daily Claimer",
"description": "Daily check-in reward claimed for account " + (i + 1) + "!",
"color": 1158948,
"footer": {
"text": "Hoyolab Daily Claimer v" + VER + " | AlexInABox"
},
"timestamp": new Date()
}
],
"attachments": []
}),
});
}
async function sendWebhookFail(i) {
fetch(config.WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"content": null,
"embeds": [
{
"title": "Hoyolab Daily Claimer",
"description": "Failed to claim daily check-in reward for account " + (i + 1) + "!",
"color": 13046547,
"footer": {
"text": "Hoyolab Daily Claimer v" + VER + " | AlexInABox"
},
"timestamp": new Date()
}
],
"attachments": []
}),
});
}
// END MAIN
//Repeating main function every 4hours
setInterval(main, 14401000);
main().catch(e => sendErrorToDiscord(e));
//Error handling
async function sendErrorToDiscord(e) {
fetch(config.WEBHOOK_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
"content": null,
"embeds": [
{
"title": "Hoyolab Daily Claimer",
"description": "ERROR: " + e,
"color": 13046547,
"footer": {
"text": "Hoyolab Daily Claimer v" + VER + " | AlexInABox"
},
"timestamp": new Date()
}
],
"attachments": []
}),
});
}