-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataManager.js
72 lines (65 loc) · 2.52 KB
/
DataManager.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
const config = require("./config.json");
const db = require('better-sqlite3')(config.dbpath, {verbose: console.log});
process.on("exit", () => db.close());
if (!db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = 'config';").get()) {
db.exec(require("fs").readFileSync("./init.sql", "utf8"));
}
const GuildConfig = (function() {
const cache = {};
db.prepare("SELECT guild_id, key, value FROM config;").all().forEach((row) => {
if (cache[row.guild_id] === undefined) cache[row.guild_id] = {};
cache[row.guild_id][row.key] = row.value;
});
function get(guild_id, key) {
if (cache[guild_id] === undefined) cache[guild_id] = {};
return cache[guild_id][key];
}
function set(guild_id, key, value) {
if (cache[guild_id] === undefined) cache[guild_id] = {};
let changes = 0;
if (value !== undefined) {
if (db.prepare("SELECT 1 FROM config WHERE (guild_id, key) = (?, ?);").get([guild_id, key])) {
changes = db.prepare("UPDATE config SET value = ? WHERE (guild_id, key) = (?, ?);").run([value, guild_id, key]);
} else {
changes = db.prepare("INSERT INTO config (guild_id, key, value) VALUES (?, ?, ?);").run([guild_id, key, value]);
}
} else {
changes = db.prepare("DELETE FROM config WHERE (guild_id, key) = (?, ?);").run([guild_id, key]);
}
if (changes) cache[guild_id][key] = value;
return changes;
}
return {
get,
set
}
})();
const Birthdays = (function() {
function getUsersByBirthday(guild_id, day, month) {
return db.prepare("SELECT user_id, year FROM birthdays WHERE (guild_id, day, month) = (?, ?, ?);").all([guild_id, day, month]);
}
function setUserBirthday(guild_id, user_id, day, month, year) {
if (db.prepare("SELECT 1 FROM birthdays WHERE (guild_id, user_id) = (?, ?);").get([guild_id, user_id])) {
db.prepare("UPDATE birthdays SET day = ?, month = ?, year = ? WHERE (guild_id, user_id) = (?, ?);").run([day, month, year, guild_id, user_id]);
} else {
db.prepare("INSERT INTO birthdays (guild_id, user_id, day, month, year) VALUES (?, ?, ?, ?, ?);").run([guild_id, user_id, day, month, year]);
}
}
function removeUserBirthday(guild_id, user_id) {
const info = db.prepare("DELETE FROM birthdays WHERE (guild_id, user_id) = (?, ?);").run([guild_id, user_id]);
return info.changes;
}
function getBirthdays(guild_id) {
return db.prepare("SELECT user_id, day, month, year FROM birthdays WHERE guild_id = ?;").all([guild_id]);
}
return {
getUsersByBirthday,
setUserBirthday,
removeUserBirthday,
getBirthdays
}
})();
module.exports = {
GuildConfig,
Birthdays
}