-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
86 lines (79 loc) · 2.52 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
import fetch from "node-fetch";
import { readFile, writeFileSync } from "node:fs";
import { Octokit } from "@octokit/rest";
import * as dotenv from "dotenv";
const octokit = new Octokit();
dotenv.config();
const asfport = process.env.ASF_PORT || "1242";
const asfhost = process.env.ASF_HOST || "localhost";
const password = process.env.ASF_PASSWORD || "";
const commandprefix = process.env.ASF_COMMAND_PREFIX || "!";
const asfhttps =
process.env.ASF_HTTPS &&
(process.env.ASF_HTTPS === true || process.env.ASF_HTTPS === "true");
const asfbots = process.env.ASF_BOTS || "asf";
let lastLength;
readFile("lastlength", function read(err, data) {
if (!err && data) {
lastLength = data;
} else if (err.code === "ENOENT") {
writeFileSync("lastlength", "0");
lastLength = 0;
} else {
console.log("Error with lastlength: ", err.code);
}
});
checkGame();
setInterval(checkGame, 6 * 60 * 60 * 1000); //Runs every six hours
function checkGame() {
octokit.gists
.get({ gist_id: "e8c5cf365d816f2640242bf01d8d3675" })
.then((gist) => {
const codes = gist.data.files["Steam Codes"].content.split("\n");
//THIS IS BAD, and definitely not scalable.
if (lastLength < codes.length) {
const lastLengthBeforeRun = lastLength;
if (lastLength + 40 < codes.length) {
console.log("Only runs on the last 40 games");
lastLength = codes.length - 40;
}
let asfcommand = `${commandprefix}addlicense ${asfbots} `;
for (lastLength; lastLength < codes.length; lastLength++) {
asfcommand += `${codes[lastLength]},`;
}
asfcommand = asfcommand.slice(0, -1);
const command = { Command: asfcommand };
const url = `http${
asfhttps ? "s" : ""
}://${asfhost}:${asfport}/Api/Command`;
const headers = { "Content-Type": "application/json" };
if (password && password.length > 0) {
headers.Authentication = password;
}
fetch(url, {
method: "post",
body: JSON.stringify(command),
headers: headers,
})
.then((res) => res.json())
.then((body) => {
if (body.Success) {
console.log(`Success: ${asfcommand}`);
console.debug(body);
writeFileSync("lastlength", lastLength.toString());
} else {
console.log("Error: ");
console.log(body);
}
})
.catch((err) => {
console.log(`error running '${command.Command}':`);
console.log(err);
console.log("Trying again in six hours");
lastLength = lastLengthBeforeRun;
});
} else {
console.log(`Found: ${codes.length} and has: ${lastLength}`);
}
});
}