This repository has been archived by the owner on Jul 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.js
404 lines (355 loc) · 15.3 KB
/
helpers.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
require('dotenv').config();
const fs = require('fs');
const config = require('./config.json');
const notificationTemplates = {
invite: "invite",
friendRequest: "friendRequest",
messageRecieved: "messageRecieved"
};
module.exports = {
PullPlayerData: PullPlayerData,
PushPlayerData: PushPlayerData,
NotifyPlayer: NotifyPlayer,
ArePlayersAnyFriendType: ArePlayersAnyFriendType,
ArePlayersAcquantances: ArePlayersAcquantances,
ArePlayersFriends: ArePlayersFriends,
ArePlayersFavoriteFriends: ArePlayersFavoriteFriends,
RemoveAcquaintance: RemoveAcquaintance,
RemoveFriend: RemoveFriend,
RemoveFavoriteFriend: RemoveFavoriteFriend,
AddFriend: AddFriend,
AddFavoriteFriend: AddFavoriteFriend,
AddAcquaintance: AddAcquaintance,
getUserID: getUserID,
getAccountCount: getAccountCount,
auditLog: auditLog,
MergeArraysWithoutDuplication: MergeArraysWithoutDuplication,
BanPlayer: BanPlayer,
onPlayerReportedCallback: onPlayerReportedCallback,
check: check
};
/**
* Pulls the full account data of a player.
* @param {String} id The account ID of the player whose data should be retrieved.
* @returns {Object} The player's account data.
*/
async function PullPlayerData(id) {
const db = require('./index').mongoClient.db(process.env.MONGOOSE_DATABASE_NAME);
const account = await db.collection('accounts').findOne({_id: {$eq: id, $exists: true}});
return account;
}
/**
* Completely overwrites a player's account file/document.
* @param {String} id The ID of the player whose data should be updated.
* @param {Object} data The full data of the specified player's account
*/
async function PushPlayerData(id, data) {
const db = require('./index').mongoClient.db(process.env.MONGOOSE_DATABASE_NAME);
await db.collection('accounts').replaceOne({_id: {$eq: id, $exists: true}}, data, {upsert: true});
}
/**
* Sends a notification to a player. This does not send them a WebSocket message.
* @param {String} id The ID of the player to notify.
* @param {String} template The template to use for the notification.
* @param {Object} params The parameters of this notification. (Template specific.)
* @returns
*/
async function NotifyPlayer(id, template, params) {
if(!(Object.values(notificationTemplates).includes(template))) return false;
var data = await PullPlayerData(id);
if(data === null) return false;
const notification = {
template: template,
parameters: params
};
data.notifications.push(notification);
await PushPlayerData(id, data);
return true;
}
/**
* Checks whether the specified players have the other in their acquaintance, friend, or favorite friend lists.
* @param {String} player1 The ID of the first player.
* @param {String} player2 The ID of the second player.
* @returns {Boolean} Whether or not the players are any type of friend.
*/
async function ArePlayersAnyFriendType(player1, player2) {
var data = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
return data.private.acquaintances.includes(player2) ||
data.private.friends.includes(player2) ||
data.private.favoriteFriends.includes(player2) ||
data2.private.acquaintances.includes(player1) ||
data2.private.friends.includes(player1) ||
data2.private.favoriteFriends.includes(player1);
}
/**
* Checks whether one of the players has the other in their acquaintances list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @returns {Boolean} Whether or not the players are acquaintances.
*/
async function ArePlayersAcquantances(player1, player2) {
var data = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
return data.private.acquaintances.includes(player2) ||
data2.private.acquaintances.includes(player1);
}
/**
* Checks whether one of the players has the other in their friends list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @returns {Boolean} Whether or not the players are friends.
*/
async function ArePlayersFriends(player1, player2) {
var data = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
return data.private.friends.includes(player2) ||
data2.private.friends.includes(player1);
}
/**
* Checks whether one of the players has the other in their favorite friends list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @returns {Boolean} Whether or not the players are favorite friends.
*/
async function ArePlayersFavoriteFriends(player1, player2) {
var data = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
return data.private.favoriteFriends.includes(player2) ||
data2.private.favoriteFriends.includes(player1);
}
/**
* Removes a player from another player's acquaintance list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @param {Boolean} both Whether or not to remove the acquaintance from both players.
*/
async function RemoveAcquaintance(player1, player2, both) {
var data1 = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
var index1 = data1.private.acquaintances.findIndex(item => item === player2);
if(index1 >= 0) data1.private.acquaintances.splice(index1);
var index2 = data2.private.acquaintances.findIndex(item => item === player1);
if(index2 >= 0 && both) data2.private.acquaintances.splice(index2);
await PushPlayerData(player1, data1);
await PushPlayerData(player2, data2);
}
/**
* Removes a player from another player's friends list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @param {Boolean} both Whether or not to remove the friend from both players.
*/
async function RemoveFriend(player1, player2, both) {
var data1 = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
var index1 = data1.private.friends.findIndex(item => item === player2);
if(index1 >= 0) data1.private.friends.splice(index1);
var index2 = data2.private.friends.findIndex(item => item === player1);
if(index2 >= 0 && both) data2.private.friends.splice(index2);
await PushPlayerData(player1, data1);
await PushPlayerData(player2, data2);
}
/**
* Removes a player from another player's favorite friends list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @param {Boolean} both Whether or not to remove the favorite friend from both players.
*/
async function RemoveFavoriteFriend(player1, player2, both) {
var data1 = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
var index1 = data1.private.favoriteFriends.findIndex(item => item === player2);
if(index1 >= 0) data1.private.favoriteFriends.splice(index1);
var index2 = data2.private.favoriteFriends.findIndex(item => item === player1);
if(index2 >= 0 && both) data2.private.favoriteFriends.splice(index2);
await PushPlayerData(player1, data1);
await PushPlayerData(player2, data2);
}
/**
* Adds a player to another player's acquaintances list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @param {Boolean} both Whether or not the addition is mutual.
*/
async function AddAcquaintance(player1, player2, both) {
var data1 = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
if(!data1.private.acquaintances.includes(player2))
{
data1.private.acquaintances.push(player2);
await PushPlayerData(player1, data1);
}
if(!data2.private.acquaintances.includes(player1) && both) {
data2.private.acquaintances.push(player1);
await PushPlayerData(player2, data2);
}
}
/**
* Adds a player to another player's friends list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @param {Boolean} both Whether or not the addition is mutual.
*/
async function AddFriend(player1, player2, both) {
var data1 = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
if(!data1.private.friends.includes(player2))
{
data1.private.friends.push(player2);
await PushPlayerData(player1, data1);
}
if(!data2.private.friends.includes(player1) && both) {
data2.private.friends.push(player1);
await PushPlayerData(player2, data2);
}
}
/**
* Adds a player to another player's favorite friends list.
* @param {String} player1 The ID of player A.
* @param {String} player2 The ID of player B.
* @param {Boolean} both Whether or not the addition is mutual.
*/
async function AddFavoriteFriend(player1, player2, both) {
var data1 = await PullPlayerData(player1);
var data2 = await PullPlayerData(player2);
if(!data1.private.favoriteFriends.includes(player2))
{
data1.private.favoriteFriends.push(player2);
await PushPlayerData(player1, data1);
}
if(!data2.private.favoriteFriends.includes(player1) && both) {
data2.private.favoriteFriends.push(player1);
await PushPlayerData(player2, data2);
}
}
/**
* Retrieves the account ID associated with the given username.
* @param {String} username The username of the account to fetch.
* @returns {String|null} The ID of the account associated with that username.
*/
async function getUserID(username) {
const db = require('./index').mongoClient.db(process.env.MONGOOSE_DATABASE_NAME);
const all = await db.collection('accounts').find({}).toArray();
username = username.toLowerCase();
for(const item of all) {
if(item.public.username.toLowerCase() === username.toLowerCase()) return item._id;
}
return null;
}
/**
* Fetches the number of accounts in the database.
* @returns {Number} The total number of accounts in the database.
*/
async function getAccountCount() {
const db = require('./index').mongoClient.db(process.env.MONGOOSE_DATABASE_NAME);
const count = await db.collection('accounts').countDocuments();
return count - 1;
}
/**
* Logs an audit event, used for security investigations and verifying user reports.
* @param {String} message The audit event to log.
* @param {Boolean} isRaw Whether or not to wrap the text in a code block for Discord webhooks.
*/
function auditLog(message, isRaw) {
const file = fs.readFileSync("./data/audit.json");
let data = JSON.parse(file);
const ts = Date.now();
const log = `${ts} - ${message}`;
data.push(log);
const final = JSON.stringify(data, null, " ");
fs.writeFileSync("./data/audit.json", final);
console.log(log);
if (!process.env.AUDIT_SERVER_ID || !process.env.AUDIT_WEBHOOK_URI) return;
const globalAuditMessage =
isRaw ?
`API audit log from server.\nID: \`${process.env.AUDIT_SERVER_ID}\`\nMessage:\n${message}` :
`API audit log from server.\nID: \`${process.env.AUDIT_SERVER_ID}\`\nMessage:\`${message}\``;
fetch(
process.env.AUDIT_WEBHOOK_URI,
{
'method': 'POST',
'headers': {
'Content-Type': 'application/json'
},
'body': JSON.stringify({
'content': globalAuditMessage
})
}
);
}
/**
* Merges two arrays without duplication.
* @param {any[]} a
* @param {any[]} b
* @returns {any[]} The union of both arrays with no duplicates.
*/
function MergeArraysWithoutDuplication(a, b) {
return a.concat(b.filter((item) => a.indexOf(item) < 0));
}
/**
* Called when a player is reported, used to perform administrative actions and potentially ban the player.
* @param {Object} reportData Information about the report event.
* @param {Number} reportData.timestamp The timestamp of the report.
* @param {String} reportData.reportingUser The ID of the user who reported the player.
* @param {String} reportData.reportedUser The ID of the user who was reported.
* @param {String} reportData.reason The reason for the report.
*/
async function onPlayerReportedCallback(reportData) {
var reportedData = await PullPlayerData(reportData.reportedUser);
var reportingData = await PullPlayerData(reportData.reportingUser);
if(
reportingData.private.availableTags.includes("Community Support") ||
reportingData.private.availableTags.includes("Community Support Team") ||
reportingData.private.availableTags.includes("Developer") ||
reportingData.private.availableTags.includes("Moderator") ||
reportingData.private.availableTags.includes("Founder")
) {
await BanPlayer(reportData.reportedUser, reportData.reason, 1, reportData.reportingUser);
auditLog(`!! MODERATOR ACTION !! Moderator ${reportingData.nickname} (@${reportingData.username}) reported user ${reportedData.nickname} (@${reportedData.username}) for the reason of ${reportData.reason}, resulting in them being automatically timed out for 1 hour.`);
var index = reportingData.auth.reportedUsers.findIndex(item => item === reportData.reportedUser);
if(index >= 0) {
reportingData.auth.reportedUsers.splice(index);
await PushPlayerData(reportData.reportingUser, reportingData);
}
} else if (reportedData.auth.recievedReports.length >= config.timeout_at_report_count) {
await BanPlayer(reportData.reportedUser, `Automated timeout for recieving ${config.timeout_at_report_count} or more reports. This timeout will not affect your moderation history unless it is found to be 100% justified.`, 6, reportData.reportingUser);
auditLog(`!! MODERATION ACTION !! User ${reportingData.nickname} (@${reportedData.username}) was timed out for 6 hours for recieving ${config.timeout_at_report_count} reports. Please investigate!`);
}
}
/**
* Bans a player from the game for the specified duration.
* @param {String} id The ID of the player to ban.
* @param {String} reason The reason for the ban.
* @param {Number} duration The duration of the ban in hours.
* @param {Boolean} moderator Did a moderator ban the player?
* @returns
*/
async function BanPlayer(id, reason, duration, moderator) {
let data = await PullPlayerData(id);
const endTS = Date.now() + (duration * 60); //convert duration from hours to a unix timestamp
const ban = {
reason: reason,
endTS: endTS,
moderator: moderator
};
data.auth.bans.push(ban);
await PushPlayerData(id, data);
let clients = require('./routers/ws/WebSocketServerV2').ws_connected_clients;
if(!Object.keys(clients).includes(id)) return;
clients[id].socket.close();
}
/**
* Checks a string for potential profanity. This is not a foolproof method, and should not be used as a replacement for human moderation.
* Susceptible to the [Scunthorpe Problem](https://en.wikipedia.org/wiki/Scunthorpe_problem).
* @param {String} string The string to check for potential profanity.
* @returns {Boolean} Whether or not the string contains the potential for profanity.
*/
function check(string) {
const words = require('./data/badwords/array');
const tlc = string.toLowerCase();
for(const word of words) {
if(tlc.includes(word)) return true;
}
return false;
}