Skip to content

Commit

Permalink
Full sqlite3 support, switched to better-sqlite3 module
Browse files Browse the repository at this point in the history
Signed-off-by: jNullj <iontankatchker@gmail.com>
  • Loading branch information
jNullj committed Apr 28, 2018
1 parent 3be77b0 commit 7b03518
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Command.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
const User = require('./User.js');
// Load database
const DB = require("./DB.js");

class Command {
static getPoints(id){
var user = new User(id);
console.log(user.isExists());
if(!user.isExists()){
if (user.getID() < 0) {
throw 'invalid user id';
}
DB.newUser(id);
user.save();
}else{
user.load();
Expand All @@ -22,6 +26,7 @@ class Command {
throw 'invalid user id';
}
result = user.setBDay(bdate);
DB.newUser(id);
user.save();
}else{
user.load(); // load before saving to not overwrite other data
Expand Down
74 changes: 74 additions & 0 deletions DB.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
var SQL = require('better-sqlite3'); // Load sqlite module
const DB_FILE = 'database.db'; // DB File name

class DB {
// 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 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.id == uid){
return true;
}else{
return false;
}
}
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);
}
}

module.exports = DB;
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Pikabot
A simple discord bot.

one dependency is sqlite3, to install it run the command `npm install sqlite3`
one dependency is better-sqlite3, to install it run the command `npm install better-sqlite3`
10 changes: 10 additions & 0 deletions User.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Load database
const DB = require("./DB.js");

class User {
constructor(id = -1 ,points = 0){
this.id = id;
Expand Down Expand Up @@ -36,12 +39,16 @@ class User {

// Cheak if the user exists in the db
isExists(){
return DB.isUserExist(this.id);
const fs = require('fs');
var path = './db/points/' + this.id + ".txt";
return fs.existsSync(path);
}
// Save user to db
save(){
DB.setPoints(this.id, this.points);
DB.setBirthday(this.id, this.birthday);
return;
const fs = require('fs');
// save points
var path = './db/points/' + this.id + ".txt";
Expand All @@ -54,6 +61,9 @@ class User {
}
// Load user from db
load(){
this.points = DB.getPoints(this.id);
this.birthday = DB.getBirthday(this.id);
return;
const fs = require('fs');
// load points
var path = './db/points/' + this.id + ".txt";
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ bot.on('ready', () => {
const User = require('./User.js');
// Load commands class
const Command = require('./Command.js');
// Load database
const DB = require("./DB.js");

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

Expand Down

0 comments on commit 7b03518

Please sign in to comment.