(BROKEN) database loading and ascii art done, broken access to database subclass

This commit is contained in:
plane000
2018-10-16 20:42:46 +01:00
parent 0026048fc6
commit 66bc71138b
9 changed files with 1060 additions and 22 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,3 @@
config/
resources/
node_modules/
*.log

935
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,8 +9,12 @@
"author": "",
"license": "MIT",
"dependencies": {
"body-parser": "^1.18.3",
"colors": "^1.3.2",
"discord.js": "^11.4.2",
"request": "^2.88.0"
"express": "^4.16.4",
"request": "^2.88.0",
"sequelize": "^4.39.1",
"sqlite3": "^4.0.2"
}
}

View File

@@ -3,7 +3,8 @@ const fs = require('fs');
module.exports = class Config {
constructor(path) {
this.path = path;
this.file = './resources/config/config.json';
this.path = [ './resources/', './resources/config' ]
this.Config = {
token: 'YOUR TOKEN HERE'
};
@@ -11,9 +12,9 @@ module.exports = class Config {
load() {
Logger.info('Loading config');
if (fs.existsSync(this.path)) {
if (fs.existsSync(this.file)) {
try {
this.Config = JSON.parse(fs.readFileSync(this.path));
this.Config = JSON.parse(fs.readFileSync(this.file));
if (!this.Config.token);
Logger.info('Config loaded successfully');
@@ -23,7 +24,10 @@ module.exports = class Config {
} else {
try {
Logger.error('No config found');
fs.writeFileSync(this.path, JSON.stringify(this.Config, null, 4));
for (let folder of this.path) {
fs.mkdirSync(folder);
}
fs.writeFileSync(this.file, JSON.stringify(this.Config, null, 4));
Logger.warn(`Created config at: ${this.path}`);
Logger.panic('Config required to be complete');
} catch (e) {

View File

@@ -0,0 +1,42 @@
const Sequelize = require('sequelize');
const Logger = require('../logger');
let Connection;
let Server;
module.exports = class Database {
static get Connection() { return Connection; }
static get Server() { return Server; }
static async init() {
Logger.info('Connecting to SQLite Database');
Connection = new Sequelize('database', 'user', 'password', {
host: 'localhost',
dialect: 'sqlite',
logging: Logger.database,
operatorsAliases: false,
storage: './resources/database.sqlite',
});
Server = Connection.define('server', {
id: {
type: Sequelize.BIGINT,
primaryKey: true,
unique: true
},
name: Sequelize.BIGINT
}, {
tableName: 'server'
});
try {
await Connection.sync({force: false});
} catch (e) {
Logger.panic(`Failed to connect to SQLite Database, error: ${e}`)
}
Logger.info('Connected to SQLite Database');
}
}

28
src/database/database.js Normal file
View File

@@ -0,0 +1,28 @@
const BaseDatabase = require('./basedatabase');
class Database extends BaseDatabase {
static async exec(query) {
let connection = BaseDatabase.Connection;
let res;
let promise = new Promise((resolve, reject) => {
connection
.query(query)
.then(result => {
Logger.database(JSON.stringify(res, null, 4));
res = result[0][0].result;
resolve();
})
.catch(err => {
Logger.error('An error occured while querying a database: ' + err);
reject()
});
});
await promise;
return res;
}
}
Database.Server = require('./server').ServerTools;
module.exports = Database;

7
src/database/server.js Normal file
View File

@@ -0,0 +1,7 @@
const BaseDatabase = require('./basedatabase');
module.exports.ServerTools = class ServerTools extends BaseDatabase {
static lolxd() {
console.log('lolcx')
}
}

View File

@@ -1,22 +1,21 @@
const Discord = require('discord.js');
const Logger = require('./logger');
const Config = require('./config');
const Discord = require('discord.js');
const client = new Discord.Client();
const Database = require('./database/database');
Logger.init();
Logger.SetLevel(Logger.VERBOSE_LOGS);
let client;
let config = new Config('./config/config.json');
config.load();
init();
async function init() {
Logger.init();
Logger.SetLevel(Logger.VERBOSE_LOGS);
// client.on('ready', () => {
// console.log(`Logged in as ${client.user.tag}!`);
// });
let config = new Config();
config.load();
// client.on('message', msg => {
// if (msg.content === 'ping') {
// msg.reply('Pong!');
// }
// });
await Database.init();
Database.Server.lolxd();
// client.login('token');
client = new Discord.Client();
}

View File

@@ -13,6 +13,17 @@ module.exports = class Logger {
fs.writeFileSync(logPath, '');
}
fs.appendFileSync(logPath, '[SYSTEM STARTING UP] \n');
console.log(colours.rainbow(
'\t _ _ _ \n' +
'\t | | | | | \n' +
'\t _ __ _ _| | |_ __ | |_ _ __ \n' +
'\t| \'_ \\| | | | | | \'_ \\| __| \'__| \n' +
'\t| | | | |_| | | | |_) | |_| | \n' +
'\t|_| |_|\\__,_|_|_| .__/ \\__|_| \n' +
'\t | | \n' +
'\t |_| \n'));
}
static SetLevel(level) {
@@ -28,6 +39,14 @@ module.exports = class Logger {
static get INFO_LOGS() {return 2;}
static get WARN_LOGS() {return 3;}
static database(message) {
if (LogLevel > 0) return;
let d = new Date();
fs.appendFileSync(logPath, `[${d.toLocaleString()}] [${Dialect}] ${message} \n`);
console.log('[' + d.toLocaleString() + '] ['
+ colours.magenta(Dialect) + '] ' + message);
}
static middleware(message) {
if (LogLevel > 0) return;
let d = new Date();