This repository has been archived by the owner on Jan 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.js
55 lines (49 loc) · 1.58 KB
/
check.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
require("dotenv").config();
const axios = require("axios");
const format = require("date-fns/format");
const subDays = require("date-fns/subDays");
const getAttachment = require("./lib/Play").getAttachment;
const testRun = process.argv.includes("--test");
if (testRun) {
if (typeof axios !== "function") {
console.error("axios was not loaded.");
}
if (typeof format !== "function") {
console.error("format was not loaded.");
}
if (typeof subDays !== "function") {
console.error("subDays was not loaded.");
}
console.log("Debug: Nemecord would post now.");
process.exit(0);
}
const Raven = require("raven");
Raven.config(process.env.SENTRY_DSN);
const url = process.env.DISCORD_WEBHOOK_URL;
module.exports = async (req, res) => {
await axios
.get("https://nemestats.com/api/v2/PlayedGames/", {
params: {
gamingGroupId: process.env.GAMING_GROUP_ID,
datePlayedFrom: format(subDays(new Date(), 1), "yyyy-MM-dd"),
datePlayedTo: format(subDays(new Date(), 1), "yyyy-MM-dd")
}
})
.then(({ data: { playedGames } }) => {
const promises = playedGames.map(playData => {
return getAttachment(playData).then(attachment => {
return axios.post(url, attachment).catch(error => {
if (error) Raven.captureException(error);
});
});
});
return Promise.all(promises).then(() => {
res.end(`${playedGames.length} plays sent to Discord.`);
});
})
.catch(err => {
Raven.captureException(err);
res.statusCode = 404;
res.end(err.message);
});
};