-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathperiodic.js
138 lines (123 loc) · 5.08 KB
/
periodic.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
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const child_process = require("child_process");
const constants = require("./constants");
const models = require("./db/models");
const redis = require("./redis");
const logger = require("./logging")(".");
const routeUtils = require("./routes/utils");
module.exports = function () {
const jobs = {
recordLastActive: {
run: async function () {
try {
var lastActive = await redis.getAllLastActive();
for (let userId in lastActive) {
await models.User.updateOne(
{ id: userId },
{ $set: { lastActive: lastActive[userId] } }
).exec();
await models.Friend.updateMany(
{ friendId: userId },
{ $set: { lastActive: lastActive[userId] } }
).exec();
}
await redis.removeStaleUsers();
}
catch (e) {
logger.error(e);
}
},
interval: 1000 * 60 * 5
},
expireBans: {
run: async function () {
try {
var now = Date.now();
var bans = await models.Ban.find({ expires: { $lt: now }, auto: false })
.select("userId auto type");
var unbanUserIds = bans.map(b => b.userId);
if (unbanUserIds.length == 0)
return;
for (let ban of bans)
if (ban.type == "site")
await models.User.updateOne({ id: ban.userId }, { banned: false }).exec();
await models.Ban.deleteMany({ expires: { $lt: now }, auto: false }).exec();
await routeUtils.createNotification({
content: "Your ban has expired.",
icon: "ban"
}, unbanUserIds);
for (let userId of unbanUserIds)
await redis.cacheUserPermissions(userId);
}
catch (e) {
logger.error(e);
}
},
interval: 1000 * 60 * 5
},
expireGames: {
run: async function () {
try {
let oldGames = await models.Game.find({ endTime: { $lt: Date.now() - (1000 * 60 * 60 * 24 * 30) } })
.select("_id players");
for (let game of oldGames) {
for (let player of game.players)
models.User.updateOne({ id: player }, { $pull: { games: game._id } }).exec();
models.Game.deleteOne({ _id: game._id }).exec();
}
}
catch (e) {
logger.error(e);
}
},
interval: 1000 * 60 * 10
},
findNextRestart: {
run: async function () {
try {
var count = await models.Restart.count({ when: { $lt: Date.now() } })
await models.Restart.deleteMany({ when: { $lt: Date.now() } }).exec();
if (count > 0)
child_process.spawn(path.join(__dirname, "update.sh"));
else {
var restart = await models.Restart.find().sort("when");
if (restart[0])
constants.restart = restart[0].when;
else
constants.restart = null;
}
}
catch (e) {
logger.error(e);
}
},
interval: 1000 * 10
},
gamesWebhook: {
run: async function () {
try {
var games = await redis.getOpenPublicGames();
for (let game of games) {
if (!game.webhookPublished) {
await redis.gameWebhookPublished(game.id);
await axios.post("https://discord.com/api/webhooks/840041267399229480/qHJ93xbByPbi3YTrhUo60qhSHH5AVZoQleHFOikU9rHcf2CkUrt1LcGUpyGwdvn5OazQ", {
username: "EpicMafia",
content: `New ${game.type} game created: https://epicmafia.org/game/${game.id}`
});
}
}
}
catch (e) {
logger.error(e);
}
},
interval: 1000 * 10
}
};
for (let jobName in jobs) {
jobs[jobName].run();
setInterval(jobs[jobName].run, jobs[jobName].interval);
}
};