-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (120 loc) · 3.56 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
const fetch = require("node-fetch");
require("dotenv").config();
class InformationFileSyntaxException extends Error {
constructor(message) {
super(message);
this.name = "InformationFileSyntaxException";
}
}
function isValidNumber(value) {
return Number.isInteger(Number(value));
}
function parseValueAsArray(value) {
value = value.substring(1, value.length - 1); // remove brackets
value = value.split(",").map((v) => v.trim()); // split by comma and trim whitespace
if (value.some((v) => v.startsWith('"') && v.endsWith('"'))) {
value = value.map((v) => v.substring(1, v.length - 1)); // remove quotes
} else if (value.some((v) => isValidNumber(v))) {
value = value.map((v) => Number(v)); // convert to numbers
} else {
throw new InformationFileSyntaxException(
`Array value for key with identifier '${key}' malformed.`
);
}
return value;
}
function parseValueAsString(value) {
return value.substring(1, value.length - 1);
}
function getDataAsObject(data) {
let lines = data.split("\n");
let obj = {};
lines.forEach((line) => {
if (line.startsWith("#")) return; // ignore comments
if (line.trim() === "") return; // ignore empty lines
if (!line.includes("=")) return; // ignore lines without an equal sign
let [key, ...value] = line.split("=");
value = value.join("=");
if (value.trim() === "") return; // ignore lines without a value
if (value.startsWith("[") && value.endsWith("]")) {
value = parseValueAsArray(value);
} else if (isValidNumber(value)) {
value = Number(value);
} else if (value.startsWith('"') && value.endsWith('"')) {
value = parseValueAsString(value);
} else {
throw new InformationFileSyntaxException(
`Value for key with identifier '${key}' malformed.`
);
}
obj[key] = value;
});
return obj;
}
// Backend
const { kelp } = require("./kelp");
const { client } = require("./bot");
const { EmbedBuilder } = require("discord.js");
require("./bot");
kelp.settings({
PORT: 9102,
OPTIONS: ["body-parser", "cors", "ejs", "public", "routes"],
})
kelp.listen();
var cached = {
total: 0,
repos: [],
};
function cache() {
fetch("https://api.github.com/users/znci/repos", {
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
}
})
.then((res) => res.json())
.then(async (json) => {
json.forEach(async (element) => {
let request = await fetch(
`https://raw.githubusercontent.com/znci/${element.name}/${element.default_branch}/.znci`
);
if (request.status === 200) {
let data = await request.text();
let obj = await getDataAsObject(data);
if (obj) {
if(parseInt(obj["PRIVATE"]) === 0) {
cached['repos'].push(obj);
cached['total'] = cached['repos'].length;
}
}
}
});
});
};
function getCached() {
return cached;
}
cache();
setInterval(() => {
cached = {
total: 0,
repos: [],
};
cache();
}, 60000)
client.on("ready", async () => {
setInterval(async () => {
const channel = await client.channels.fetch("1086624387614113883");
let embed = new EmbedBuilder()
.setTitle("Update")
.setDescription(`Running a cache update - Total cached: ${cached['total']}`)
.setColor("#afef76")
.addFields(
{ name: "Online Services", value: `${cached['repos'].map(v => v['NAME']).join(", ")}`}
)
.setTimestamp();
channel.send({ embeds: [embed] });
}, 120000);
})
exports.cache = cache;
exports.cached = cached;
exports.getCached = getCached;