-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDB.js
158 lines (153 loc) · 5.32 KB
/
DB.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
var SQL = require('better-sqlite3'); // Load sqlite module
const DB_FILE = 'database/database.db'; // DB File name
const DB_INIT_FILE = 'db_creation.sql'; // DB creation instructions
class DB {
static init_db() {
// check if db exists, if not create it
const fs = require('fs') // import fs to check if db exists and read db init file
try {
if (!fs.existsSync(DB_FILE)) {
console.log('DB file does not exist, creating new DB.')
// db does not exist, create it
const sql = fs.readFileSync(DB_INIT_FILE, 'utf8')
let db = this.connect()
db.exec(sql)
this.disconnect(db)
console.log('new DB created at ' + DB_FILE)
}
} catch(v) {
console.error(v)
}
}
// Connect to DB
static connect(){
return new SQL(DB_FILE);
}
// Disconnect to DB
static disconnect(db){
db.close();
}
// set points for user
static setPoints(uid, points){
var sql = `UPDATE users
SET points = ?
WHERE id = ?`;
var db = this.connect();
db.prepare(sql).run(points, uid);
this.disconnect(db);
}
// set birthday for user
static setBirthday(uid, date){
var sql = `UPDATE users
SET birthday = ?
WHERE id = ?`;
var db = this.connect();
db.prepare(sql).run(date, uid);
this.disconnect(db);
}
// get points for user
static getPoints(uid){
var sql = `SELECT points
FROM users
WHERE id = ?`;
var db = this.connect();
var row = db.prepare(sql).get(uid);
this.disconnect(db);
return row.points;
}
/**
* Get highscore list
* @param {number} limit - Maximum number of highscore entries to return
* @returns {Object[]} - Array of objects containing id and points
*/
static getHighscore(limit){
let sql = `SELECT CAST(id AS TEXT) id, points
FROM users
WHERE points > 0
ORDER BY points DESC
LIMIT ?`;
var db = this.connect();
var row = db.prepare(sql).all(limit);
this.disconnect(db);
return row;
}
// get birthday for user
static getBirthday(uid){
var sql = `SELECT birthday
FROM users
WHERE id = ?`;
var db = this.connect();
var row = db.prepare(sql).get(uid);
this.disconnect(db);
return row.birthday;
}
// cheak if user exists in db, returns bool
static isUserExist(uid){
var sql = `SELECT CAST(id AS TEXT) id
FROM users
WHERE id = ?`;
var db = this.connect();
var row = db.prepare(sql).get(uid);
this.disconnect(db);
if (row === undefined){ // accurding to better-sqlite3 returns undefined if results was empty
return false; // if row is undefined the sucssesful quary is empty, user doesnt exists
}else{
return true; // else the user was retrieved from the db, user exists in db
}
}
static newUser(uid){
console.log("New User With ID: " + uid);
var sql = `INSERT INTO users(id, points, birthday) VALUES(?,0,-1)`;
var db = this.connect();
db.prepare(sql).run(uid);
this.disconnect(db);
}
/**
* Find all users who have a birthday on a specified date
*
* @param {Date} date - Date to search for users who have birthday
* @returns {number[]} - Array of discord user ids who have birthday on provided date
*/
static BDayOnDate(date){
const result = [];
const month = date.getMonth() + 1; // Add 1 to convert from 0-based to 1-based index
const day = date.getDate();
const sql = `SELECT id
FROM users
WHERE SUBSTR(birthday, 6, 2) = ?
AND SUBSTR(birthday, 9) = ?`;
var db = this.connect();
const row = db.prepare(sql).all(month.toString().padStart(2, '0'), day.toString().padStart(2, '0'));
this.disconnect(db);
row.forEach((row) => {
result.push(row.id);
});
return result;
}
static isSelfAddChannel(channel_id){
var sql = `SELECT id
FROM selfAddChannels
WHERE id = ?`;
var db = this.connect();
var row = db.prepare(sql).get(channel_id);
this.disconnect(db);
if (row === undefined){ // accurding to better-sqlite3 returns undefined if results was empty
return false; // if row is undefined the sucssesful quary is empty, user doesnt exists
}else{
return true; // else the user was retrieved from the db, user exists in db
}
}
static getSelfAddChannels(){
var result = [];
var sql = `SELECT CAST(id AS TEXT) id
FROM selfAddChannels`;
var db = this.connect();
var row = db.prepare(sql).all();
this.disconnect(db);
row.forEach((row) => {
result.push(row.id);
});
return result;
}
}
module.exports = DB;