-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
375 lines (328 loc) · 12.2 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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
var colors = require('colors');
var readLine = require('readline');
const sqlite3 = require('sqlite3').verbose();
var SteamCommunity = require('steamcommunity');
var community = new SteamCommunity();
const SteamID = require('steamid');
var config = require('./config.json');
const FormData = require('form-data');
const fetch = require('node-fetch');
var moment = require('moment');
moment().format();
const { version } = require('./package.json');
function updateChecker() {
fetch('https://raw.githubusercontent.com/KniferFTW/rep4rep-bot/main/package.json', {
method: 'GET'
})
.then(res => res.json())
.then(json => {
if(json.version > version) {
console.log(`\n[UPDATE] New update available. Current version: v${version}, newest version: v${json.version}.`.bold.yellow)
console.log('[UPDATE] Get the latest version here: https://github.com/KniferFTW/rep4rep-bot'.bold.yellow)
}
}).catch(error => {
console.log('\n[UPDATE] Unable to check for new updates!'.bold.red)
});
}
var rl = readLine.createInterface({
"input": process.stdin,
"output": process.stdout
});
let db = new sqlite3.Database('./steamprofiles.db', (err) => {
if (err) {
console.log(err);
process.exit();
}
createTables();
homeMenu();
});
function createTables() {
let tables = [
`CREATE TABLE IF NOT EXISTS steamprofiles (
id integer PRIMARY KEY AUTOINCREMENT,
username varchar,
steamId varchar UNIQUE,
cookies text,
token varchar,
last_comment datetime
)`,
];
tables.forEach(query => {
db.run(query, function(err) {
if (err) {
console.log(err);
process.exit();
}
});
});
}
function printHeader(headTitle = 'Home') {
console.log('\x1Bc');
let title = 'Rep4Rep Bot - ' + headTitle + '\n';
console.log(title.bold.bgBlue);
}
function homeMenu(err = false) {
printHeader();
setTimeout(updateChecker, 1500);
console.log('1) Auto Run');
console.log('2) Manage Steam Accounts');
console.log('CTRL + C to exit at any time.'.gray);
if (err) { console.log(err.bold.red); }
let validOptions = [1, 2];
rl.question('>> ', function(chosenOption) {
if (validOptions.includes(parseInt(chosenOption))) {
switch (parseInt(chosenOption)) {
case 1:
autoRun();
break;
case 2:
profilesMenu();
break;
default:
break;
}
} else {
homeMenu('Invalid Option, Retry.');
}
});
}
async function profilesMenu(err = false) {
printHeader('Manage Steam Accounts');
let steamProfiles = await db_all('SELECT id, username, steamId, last_comment FROM steamprofiles');
if (Object.keys(steamProfiles).length !== 0) {
console.table(steamProfiles);
} else {
console.log('[ No Accounts added yet ]'.bold);
}
console.log();
console.log('1) Add a Steam Account');
console.log('2) Re-Login to a Steam Account');
console.log('3) Remove a Steam Account');
console.log('4) Back \n'.gray);
if (err) { console.log(err.bold.red); }
let validOptions = [1, 2, 3, 4];
rl.question('>> ', function(chosenOption) {
if (validOptions.includes(parseInt(chosenOption))) {
switch (parseInt(chosenOption)) {
case 1:
addSteamAccount();
break;
case 2:
reloginSteamAccount();
break;
case 3:
removeSteamAccount();
break;
case 4:
homeMenu();
break;
default:
break;
}
} else {
profilesMenu('Invalid Option, Retry.');
}
});
}
async function db_all(query) {
return new Promise(function(resolve,reject){
db.all(query, function(err,rows){
if(err){return reject(err);}
resolve(rows);
});
});
}
async function isLoggedIn() {
return new Promise(function(resolve,reject){
community.loggedIn(function(err, loggedIn, familyView) {
if(err){return reject(err);}
resolve(loggedIn);
});
});
}
async function autoRun() {
const response = await fetch('https://rep4rep.com/pub-api/user/steamprofiles?apiToken=' + config.apiToken);
const data = await response.json();
if (data.error) {
homeMenu(data.error);
return;
}
// hella nasty
let repSteamProfiles = [];
let repSteamProfilesObj = {};
data.forEach((steamProfile) => {
repSteamProfiles.push(steamProfile.steamId);
repSteamProfilesObj[steamProfile.steamId] = steamProfile.id;
});
let steamProfiles = await db_all('SELECT id, username, steamId, last_comment, cookies, token FROM steamprofiles');
if (Object.keys(steamProfiles).length == 0) {
homeMenu('No local steam accounts added to comment from.');
return;
}
for (const steamProfile of steamProfiles) {
// if profile doesnt exist on rep4rep add it
if (!repSteamProfiles.includes(steamProfile.steamId)) {
console.log('account not added on rep4rep!!');
let form = new FormData();
form.append('apiToken', config.apiToken);
form.append('steamProfile', steamProfile.steamId);
const response = await fetch('https://rep4rep.com/pub-api/user/steamprofiles/add', {
method: 'post',
body: form
});
const data = await response.json();
if (data.error) {
homeMenu(data.error);
return;
}
console.log(steamProfile.username + ' added to rep4rep.');
autoRun(); // hella nasty, fix later
return;
}
let hours = moment().diff(moment(steamProfile.last_comment), 'hours');
if (hours >= 24 || !steamProfile.last_comment) {
console.log('attempting to leave comments from: ' + steamProfile.username);
console.log('[ 15 sec delay between each comment ]'.bold.cyan);
community.setCookies(JSON.parse(steamProfile.cookies));
community.oAuthToken = steamProfile.token;
let loggedIn = await isLoggedIn();
if (!loggedIn) {
console.log(steamProfile.username + ' is logged out, re-login from the manage profiles view.'.bold.red);
continue;
}
// fetch available tasks (30)
const response = await fetch('https://rep4rep.com/pub-api/tasks?apiToken=' + config.apiToken + '&steamProfile=' + repSteamProfilesObj[steamProfile.steamId]);
const data = await response.json();
if (data.error) {
homeMenu(data.error);
return;
}
let failedAttempts = 0;
for (const task of data ) {
if (failedAttempts == 2) {
console.log('failed twice, skipping steamProfile.'.bold.yellow); // aka completed/steam limited for today
break;
}
console.log(steamProfile.username + ' -> ' + task.targetSteamProfileName + ' | ' + task.requiredCommentText);
await community.postUserComment(task.targetSteamProfileId, task.requiredCommentText, async function(err) {
if (err) {
console.log(err.message);
failedAttempts++;
} else {
console.log('posted comment successfully.'.bold.green);
db.run(`UPDATE steamprofiles SET last_comment=DATETIME('now', 'localtime') WHERE id=?`, [steamProfile.id], function(err) {
if (err) {
console.log(err.message);
process.exit();
}
});
// mark task as complete
let form = new FormData();
form.append('apiToken', config.apiToken);
form.append('taskId', task.taskId);
form.append('commentId', task.requiredCommentId);
form.append('authorSteamProfileId', repSteamProfilesObj[steamProfile.steamId]);
const response = await fetch('https://rep4rep.com/pub-api/tasks/complete', {
method: 'post',
body: form
});
const data = await response.json();
if (data.error) {
console.log(data.error);
process.exit();
}
console.log(data.info ?? data.success);
}
});
await sleep(15000);
}
} else {
console.log(steamProfile.username.bold.cyan + ' not ready yet. Try again in: ' + (24-hours).toString().bold.red + ' hours.');
continue;
}
}
console.log('Done with Auto Run, Exiting.'.bold.green);
process.exit();
}
async function sleep(millis) {
return new Promise(resolve => setTimeout(resolve, millis));
}
async function addSteamAccount(err = false) {
printHeader('Add a Steam Account');
if (err) { console.log(err.bold.red); }
rl.question("Steam Login Username: ", function(accountName) {
rl.question("Password: ", function(password) {
doLogin(accountName, password);
});
});
}
async function reloginSteamAccount() {
rl.question("Steam Login Username: ", function(accountName) {
console.log(accountName);
db.get('SELECT id, cookies, token FROM steamprofiles WHERE username = ?', [accountName], function(err, row) {
if (err) {
console.log(err.message);
process.exit();
}
community.setCookies(JSON.parse(row.cookies));
community.oAuthToken = row.token;
rl.question("Password: ", function(password) {
doLogin(accountName, password);
});
});
});
}
async function removeSteamAccount() {
rl.question("Username or id to remove: ", function(accountName) {
db.run(`DELETE FROM steamprofiles WHERE id = ? OR username = ?`, [accountName, accountName], function(err) {
if (err) {
console.log(err.message);
process.exit();
}
profilesMenu('Steam Account Removed! (if it was found)');
});
});
}
function doLogin(accountName, password, authCode, twoFactorCode, captcha) {
community.login({
"accountName": accountName,
"password": password,
"authCode": authCode,
"twoFactorCode": twoFactorCode,
"captcha": captcha
}, function(err, sessionID, cookies, steamguard, oauthToken) {
if(err) {
if(err.message == 'SteamGuardMobile') {
rl.question("Steam Authenticator Code: ", function(code) {
doLogin(accountName, password, null, code);
});
return;
}
if(err.message == 'SteamGuard') {
console.log("An email has been sent to your address at " + err.emaildomain);
rl.question("Steam Guard Code: ", function(code) {
doLogin(accountName, password, code);
});
return;
}
if(err.message == 'CAPTCHA') {
console.log(err.captchaurl);
rl.question("CAPTCHA: ", function(captchaInput) {
doLogin(accountName, password, authCode, twoFactorCode, captchaInput);
});
return;
}
profilesMenu(err.message);
return;
}
console.log("Logged on!");
steamId64 = community.steamID.getSteamID64();
db.run(`INSERT OR REPLACE INTO steamprofiles (username, steamId, cookies, token) VALUES (?, ?, ?, ?)`, [accountName, steamId64, JSON.stringify(cookies), oauthToken], function(err) {
if (err) {
console.log(err.message);
process.exit();
}
profilesMenu('Steam Account added! (or updated)');
});
});
}