-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexport.js
130 lines (117 loc) · 4.94 KB
/
export.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
const userMap = require("./userMap");
const config = require("./config/config.json");
const sql = require("sqlite");
try {
process.chdir(config.dbStoreDirectory);
} catch (err) {
console.log('Directory not available')
}
sql.open("./users.sqlite");
module.exports = {
startAutobackup: function () {
setTimeout(function run() {
console.log("RUNNING AUTOBACKUP...");
writeAllToSQL();
setTimeout(run, config.autobackupTime * 60000);
}, config.autobackupTime * 60000);
},
write: function (userID) {
writeUserToSQL(userID);
},
backup: function () {
writeAllToSQL();
},
importUser: function (userID) {
importUser(userID);
},
importFile: function () {
importFile();
},
shutdown: function () {
shutdown();
}
}
//Analyzes the message cache of a specific user and exports it to users.sqlite
function writeUserToSQL(userID) {
//Message cache is not stored in the SQL database, so analyze it and update user class before writing to SQL
userMap.updateUserScore(userID);
return sql.get(`SELECT userID,points,score,totalMessages FROM users WHERE userID ="${userID}"`).then(row => {
if (!row) {
sql.run("INSERT INTO users (userID, points, score, totalMessages) VALUES (?, ?, ?, ?)", [userID, userMap.points(userID), userMap.score(userID), userMap.totalMessages(userID)]);
} else {
sql.run(`UPDATE users SET points = ${userMap.points(userID)}, score = ${userMap.score(userID)}, totalMessages = ${userMap.totalMessages(userID)} WHERE userID = ${userID}`);
}
//console.log("SQL: User " + userID + " entered");
}).catch(() => {
console.error;
sql.run("CREATE TABLE IF NOT EXISTS users (userID TEXT, points INTEGER, score INTEGER, totalMessages INTEGER)").then(() => {
sql.run("INSERT INTO users (userID, points, score, totalMessages) VALUES (?, ?, ?, ?)", [userID, userMap.points(userID), userMap.score(userID), userMap.totalMessages(userID)]);
});
});
}
//Analyzes the message cache stored in userMap and exports the resulting users into users.sqlite
async function writeAllToSQL() {
//Message cache is not stored in the SQL database, so analyze it and update user class before writing to SQL
/*
console.log("----- ANALYZING MESSAGE CACHE -----");
userMap.updateAllScores();
*/
console.log("----- EXPORTING USERS -----");
ids = userMap.getKeys();
let promises = [];
for (let userID of ids) {
promises.push(writeUserToSQL(userID));
}
Promise.all(promises)
.then(() => {
console.log("----- EXPORT COMPLETE -----");
})
}
//Imports a user with a given id from the SQL db if no user exists for that id,
//or reverts the user to the database version if user already exists in userMap
function importUser(userID) {
sql.get(`SELECT userID,points,score,totalMessages FROM users WHERE userID ="${userID}"`).then(row => {
if (!row) {
console.log("EXPORT.JS READUSER ERROR: readUser called on user not in database");
return;
}
userMap.createUser(row.userID, row.points, row.score, row.totalMessages);
console.log("SQL: Imported user " + row.userID);
}).catch(() => {
console.error;
sql.run("CREATE TABLE IF NOT EXISTS users (userID TEXT, points INTEGER, score INTEGER, totalMessages INTEGER)").then(() => {
sql.run("INSERT INTO users (userID, points, score, totalMessages) VALUES (?, ?, ?, ?)", [userID, userMap.points(userID), userMap.score(userID), userMap.totalMessages(userID)]);
});
});
}
//Initializes userMap data from users.sqlite
function importFile() {
sql.all(`SELECT userID,points,score,totalMessages FROM users`).then(rows => {
for (i = 0; i < rows.length; i++) {
userMap.createUser(rows[i].userID, rows[i].points, rows[i].score, rows[i].totalMessages);
}
}).catch(() => {
console.error;
sql.run("CREATE TABLE IF NOT EXISTS users (userID TEXT, points INTEGER, score INTEGER, totalMessages INTEGER)").then(() => {
sql.run("INSERT INTO users (userID, points, score, totalMessages) VALUES (?, ?, ?, ?)", [userID, userMap.points(userID), userMap.score(userID), userMap.totalMessages(userID)]);
});
});
}
//Waits for backup to complete, and shuts down the bot
async function shutdown() {
//Message cache is not stored in the SQL database, so analyze it and update user class before writing to SQL
console.log("----- EXPORTING USERS -----");
ids = userMap.getKeys();
let promises = [];
for (let userID of ids) {
promises.push(writeUserToSQL(userID));
}
Promise.all(promises)
.then(() => {
console.log("----- EXPORT COMPLETE -----");
setTimeout(() => {
console.log("===== SHUTOFF COMPLETE =====");
process.exit(0);
}, 2000);
})
}