Skip to content

Commit 7b03518

Browse files
committed
Full sqlite3 support, switched to better-sqlite3 module
Signed-off-by: jNullj <[email protected]>
1 parent 3be77b0 commit 7b03518

File tree

5 files changed

+92
-1
lines changed

5 files changed

+92
-1
lines changed

Command.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
const User = require('./User.js');
2+
// Load database
3+
const DB = require("./DB.js");
24

35
class Command {
46
static getPoints(id){
57
var user = new User(id);
8+
console.log(user.isExists());
69
if(!user.isExists()){
710
if (user.getID() < 0) {
811
throw 'invalid user id';
912
}
13+
DB.newUser(id);
1014
user.save();
1115
}else{
1216
user.load();
@@ -22,6 +26,7 @@ class Command {
2226
throw 'invalid user id';
2327
}
2428
result = user.setBDay(bdate);
29+
DB.newUser(id);
2530
user.save();
2631
}else{
2732
user.load(); // load before saving to not overwrite other data

DB.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
var SQL = require('better-sqlite3'); // Load sqlite module
2+
const DB_FILE = 'database.db'; // DB File name
3+
4+
class DB {
5+
// Connect to DB
6+
static connect(){
7+
return new SQL(DB_FILE);
8+
}
9+
// Disconnect to DB
10+
static disconnect(db){
11+
db.close();
12+
}
13+
// set points for user
14+
static setPoints(uid, points){
15+
var sql = `UPDATE users
16+
SET points = ?
17+
WHERE id = ?`;
18+
var db = this.connect();
19+
db.prepare(sql).run(points, uid);
20+
this.disconnect(db);
21+
}
22+
// set birthday for user
23+
static setBirthday(uid, date){
24+
var sql = `UPDATE users
25+
SET birthday = ?
26+
WHERE id = ?`;
27+
var db = this.connect();
28+
db.prepare(sql).run(date, uid);
29+
this.disconnect(db);
30+
}
31+
// get points for user
32+
static getPoints(uid){
33+
var sql = `SELECT points
34+
FROM users
35+
WHERE id = ?`;
36+
var db = this.connect();
37+
var row = db.prepare(sql).get(uid);
38+
this.disconnect(db);
39+
return row.points;
40+
}
41+
// get birthday for user
42+
static getBirthday(uid){
43+
var sql = `SELECT birthday
44+
FROM users
45+
WHERE id = ?`;
46+
var db = this.connect();
47+
var row = db.prepare(sql).get(uid);
48+
this.disconnect(db);
49+
return row.birthday;
50+
}
51+
// cheak if user exists in db, returns bool
52+
static isUserExist(uid){
53+
var sql = `SELECT CAST(id AS TEXT) id
54+
FROM users
55+
WHERE id = ?`;
56+
var db = this.connect();
57+
var row = db.prepare(sql).get(uid);
58+
this.disconnect(db);
59+
if (row.id == uid){
60+
return true;
61+
}else{
62+
return false;
63+
}
64+
}
65+
static newUser(uid){
66+
console.log("New User With ID: " + uid);
67+
var sql = `INSERT INTO users(id, points, birthday) VALUES(?,0,-1)`;
68+
var db = this.connect();
69+
db.prepare(sql).run(uid);
70+
this.disconnect(db);
71+
}
72+
}
73+
74+
module.exports = DB;

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Pikabot
22
A simple discord bot.
33

4-
one dependency is sqlite3, to install it run the command `npm install sqlite3`
4+
one dependency is better-sqlite3, to install it run the command `npm install better-sqlite3`

User.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Load database
2+
const DB = require("./DB.js");
3+
14
class User {
25
constructor(id = -1 ,points = 0){
36
this.id = id;
@@ -36,12 +39,16 @@ class User {
3639

3740
// Cheak if the user exists in the db
3841
isExists(){
42+
return DB.isUserExist(this.id);
3943
const fs = require('fs');
4044
var path = './db/points/' + this.id + ".txt";
4145
return fs.existsSync(path);
4246
}
4347
// Save user to db
4448
save(){
49+
DB.setPoints(this.id, this.points);
50+
DB.setBirthday(this.id, this.birthday);
51+
return;
4552
const fs = require('fs');
4653
// save points
4754
var path = './db/points/' + this.id + ".txt";
@@ -54,6 +61,9 @@ class User {
5461
}
5562
// Load user from db
5663
load(){
64+
this.points = DB.getPoints(this.id);
65+
this.birthday = DB.getBirthday(this.id);
66+
return;
5767
const fs = require('fs');
5868
// load points
5969
var path = './db/points/' + this.id + ".txt";

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ bot.on('ready', () => {
1414
const User = require('./User.js');
1515
// Load commands class
1616
const Command = require('./Command.js');
17+
// Load database
18+
const DB = require("./DB.js");
1719

1820
var bday_cheak = 1; // last date birthday was cheaked
1921

0 commit comments

Comments
 (0)