-
Notifications
You must be signed in to change notification settings - Fork 1
/
users.js
101 lines (90 loc) · 2.6 KB
/
users.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
const fs = require('fs');
const path = require('path');
const { copyFileSync } = require('fs');
module.exports = {
checkFirstInteraction,
banUser,
checkBanStatus,
};
const userDataPath = './user';
const userDataFile = 'users.json';
const banListPath = './';
const banListFile = 'ban-list.json';
const characterFile = './character.json';
const chatLog = './chatlog.txt';
// Function to check if it's the first time a user interacts
function checkFirstInteraction(user) {
const userId = user.id;
const userData = getUserData();
if (userData.indexOf(userId) === -1) {
// Run new user function
newUser(userId);
return true;
} else {
return false;
}
}
// Function to create a new user folder, add user ID to array, and copy character.json file
function newUser(userId) {
const userDirectory = path.join(userDataPath, userId);
fs.mkdirSync(userDirectory);
copyFileSync(characterFile, path.join(userDirectory, 'character.json'));
copyFileSync(chatLog, path.join(userDirectory, 'chatlog.txt'));
const userData = getUserData();
userData.push(userId);
saveUserData(userData);
}
// Function to retrieve user data array from disk
function getUserData() {
let userData = [];
try {
const usersFile = path.join(userDataPath, userDataFile);
if (fs.existsSync(usersFile)) {
userData = JSON.parse(fs.readFileSync(usersFile));
}
} catch (error) {
console.error(error);
}
return userData;
}
// Function to save user data array to disk
function saveUserData(userData) {
const usersFile = path.join(userDataPath, userDataFile);
fs.writeFileSync(usersFile, JSON.stringify(userData), (err) => {
if (err) console.error(err);
});
}
// Function to ban a user
function banUser(userId) {
const banList = getBanList();
if (banList.indexOf(userId) === -1) {
banList.push(userId);
saveBanList(banList);
}
}
// Function to retrieve ban list array from disk
function getBanList() {
let banList = [];
try {
const banListFilepath = path.join(banListPath, banListFile);
if (fs.existsSync(banListFilepath)) {
banList = JSON.parse(fs.readFileSync(banListFilepath));
}
} catch (error) {
console.error(error);
}
return banList;
}
// Function to save ban list array to disk
function saveBanList(banList) {
const banListFilepath = path.join(banListPath, banListFile);
fs.writeFileSync(banListFilepath, JSON.stringify(banList), (err) => {
if (err) console.error(err);
});
}
// Function to check if a user is banned
function checkBanStatus(user) {
const userId = user.id;
const banList = getBanList();
return banList.indexOf(userId) !== -1;
}