(BROKEN) database loading and ascii art done, broken access to database subclass
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,3 @@
|
|||||||
config/
|
resources/
|
||||||
node_modules/
|
node_modules/
|
||||||
*.log
|
*.log
|
||||||
|
|||||||
935
package-lock.json
generated
935
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -9,8 +9,12 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"body-parser": "^1.18.3",
|
||||||
"colors": "^1.3.2",
|
"colors": "^1.3.2",
|
||||||
"discord.js": "^11.4.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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ const fs = require('fs');
|
|||||||
|
|
||||||
module.exports = class Config {
|
module.exports = class Config {
|
||||||
constructor(path) {
|
constructor(path) {
|
||||||
this.path = path;
|
this.file = './resources/config/config.json';
|
||||||
|
this.path = [ './resources/', './resources/config' ]
|
||||||
this.Config = {
|
this.Config = {
|
||||||
token: 'YOUR TOKEN HERE'
|
token: 'YOUR TOKEN HERE'
|
||||||
};
|
};
|
||||||
@@ -11,9 +12,9 @@ module.exports = class Config {
|
|||||||
|
|
||||||
load() {
|
load() {
|
||||||
Logger.info('Loading config');
|
Logger.info('Loading config');
|
||||||
if (fs.existsSync(this.path)) {
|
if (fs.existsSync(this.file)) {
|
||||||
try {
|
try {
|
||||||
this.Config = JSON.parse(fs.readFileSync(this.path));
|
this.Config = JSON.parse(fs.readFileSync(this.file));
|
||||||
if (!this.Config.token);
|
if (!this.Config.token);
|
||||||
|
|
||||||
Logger.info('Config loaded successfully');
|
Logger.info('Config loaded successfully');
|
||||||
@@ -23,7 +24,10 @@ module.exports = class Config {
|
|||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
Logger.error('No config found');
|
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.warn(`Created config at: ${this.path}`);
|
||||||
Logger.panic('Config required to be complete');
|
Logger.panic('Config required to be complete');
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
42
src/database/basedatabase.js
Normal file
42
src/database/basedatabase.js
Normal 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
28
src/database/database.js
Normal 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
7
src/database/server.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const BaseDatabase = require('./basedatabase');
|
||||||
|
|
||||||
|
module.exports.ServerTools = class ServerTools extends BaseDatabase {
|
||||||
|
static lolxd() {
|
||||||
|
console.log('lolcx')
|
||||||
|
}
|
||||||
|
}
|
||||||
31
src/index.js
31
src/index.js
@@ -1,22 +1,21 @@
|
|||||||
|
const Discord = require('discord.js');
|
||||||
|
|
||||||
const Logger = require('./logger');
|
const Logger = require('./logger');
|
||||||
const Config = require('./config');
|
const Config = require('./config');
|
||||||
const Discord = require('discord.js');
|
const Database = require('./database/database');
|
||||||
const client = new Discord.Client();
|
|
||||||
|
|
||||||
Logger.init();
|
let client;
|
||||||
Logger.SetLevel(Logger.VERBOSE_LOGS);
|
|
||||||
|
|
||||||
let config = new Config('./config/config.json');
|
init();
|
||||||
config.load();
|
async function init() {
|
||||||
|
Logger.init();
|
||||||
|
Logger.SetLevel(Logger.VERBOSE_LOGS);
|
||||||
|
|
||||||
|
let config = new Config();
|
||||||
|
config.load();
|
||||||
|
|
||||||
// client.on('ready', () => {
|
await Database.init();
|
||||||
// console.log(`Logged in as ${client.user.tag}!`);
|
Database.Server.lolxd();
|
||||||
// });
|
|
||||||
|
|
||||||
// client.on('message', msg => {
|
client = new Discord.Client();
|
||||||
// if (msg.content === 'ping') {
|
}
|
||||||
// msg.reply('Pong!');
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
|
|
||||||
// client.login('token');
|
|
||||||
|
|||||||
@@ -13,6 +13,17 @@ module.exports = class Logger {
|
|||||||
fs.writeFileSync(logPath, '');
|
fs.writeFileSync(logPath, '');
|
||||||
}
|
}
|
||||||
fs.appendFileSync(logPath, '[SYSTEM STARTING UP] \n');
|
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) {
|
static SetLevel(level) {
|
||||||
@@ -28,6 +39,14 @@ module.exports = class Logger {
|
|||||||
static get INFO_LOGS() {return 2;}
|
static get INFO_LOGS() {return 2;}
|
||||||
static get WARN_LOGS() {return 3;}
|
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) {
|
static middleware(message) {
|
||||||
if (LogLevel > 0) return;
|
if (LogLevel > 0) return;
|
||||||
let d = new Date();
|
let d = new Date();
|
||||||
|
|||||||
Reference in New Issue
Block a user