-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
52 lines (46 loc) · 1.69 KB
/
utils.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
const { ActivityLog, Token, User } = require('./models');
const { notifyAdmins } = require('./modules/notificator');
const jwt = require('jsonwebtoken');
const generateToken = async (user) => {
await Token.deleteMany({ userId: user._id });
const token = jwt.sign({ userId: user._id, username: user.username }, process.env.JWT_SECRET, { expiresIn: '1h' });
await new Token({ userId: user._id, token }).save();
return token;
};
const logActivity = async (userId, activityType, description, ipAddress) => {
try {
const user = await User.findById(userId);
const userIdentifier = user ? `${user.username} (${user.nickname || 'no nickname'} [${user._id})` : 'Unknown User';
const activityLog = new ActivityLog({
user_id: userId,
activity_type: activityType,
description,
ip_address: ipAddress
});
await activityLog.save();
notifyAdmins(`User activity: ${description}\nUser: ${userIdentifier}\n`, 'discord_log');
} catch (error) {
console.error("Error logging activity:", error);
}
};
const convertDurationToMilliseconds = (duration) => {
const unit = duration.slice(-1);
const value = parseInt(duration.slice(0, -1), 10);
switch (unit) {
case 'h':
return value * 60 * 60 * 1000;
case 'd':
return value * 24 * 60 * 60 * 1000;
case 'm':
return value * 30 * 24 * 60 * 60 * 1000;
case 'y':
return value * 365 * 24 * 60 * 60 * 1000;
default:
throw new Error('Invalid duration format');
}
};
module.exports = {
logActivity,
generateToken,
convertDurationToMilliseconds
};