forked from naveenpoddar/uptimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetchProjects.js
57 lines (51 loc) · 1.3 KB
/
fetchProjects.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
const { Client, MessageEmbed } = require("discord.js");
const { error_logs } = require("./config.json");
const fetch = require("node-fetch");
const UrlsConfig = require("./database/models/UrlsConfig");
/**
* Fetch Projects
* @param {string[]} projects
* @param {Client} client
*/
module.exports = async (projects, client) => {
projects.forEach(async (url) => {
let doc = await UrlsConfig.findOne({
projectURL: url,
});
let pinged = doc.get("pinged");
let author = doc.get("authorID");
let errors = false;
try {
await fetch(url);
} catch (e) {
errors = true;
let embed = new MessageEmbed()
.setTitle(`Unable to fetch`)
.setColor("#2990ff")
.addField("Url", url)
.addField("Author", author)
.addField("Error", `${e.name}\n\n${e.message}`);
await UrlsConfig.findOneAndUpdate(
{ projectURL: url },
{
error: true,
errorText: e.message,
},
{ new: true }
);
client.channels.cache.get(error_logs)?.send(embed);
} finally {
if (!errors) {
pinged++;
await UrlsConfig.findOneAndUpdate(
{ projectURL: url },
{
error: false,
pinged,
},
{ new: true }
);
}
}
});
};