From 3f1a80e83bbd1000e2addf562ec3a7539bad3106 Mon Sep 17 00:00:00 2001 From: plane000 Date: Fri, 26 Oct 2018 21:13:06 +0100 Subject: [PATCH 1/4] Added discord methods in events.js and renamed some variables in config then adapted the code around that. Redid date structures in Logger --- package.json | 1 + src/config.js | 8 ++++---- src/events.js | 15 +++++++++++++++ src/index.js | 11 ++++++----- src/logger.js | 24 +++++++++++++++--------- 5 files changed, 41 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index b080aaf..4633e4e 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "colors": "^1.3.2", "discord.js": "^11.4.2", "express": "^4.16.4", + "moment": "^2.22.2", "regenerator-runtime": "^0.12.1", "request": "^2.88.0", "sequelize": "^4.39.1", diff --git a/src/config.js b/src/config.js index c88a84b..61f93c9 100644 --- a/src/config.js +++ b/src/config.js @@ -6,8 +6,8 @@ export class Config { constructor(path) { this.file = './resources/config/config.json'; this.path = [ './resources/', './resources/config' ] - this.Config = { - token: 'YOUR TOKEN HERE' + this.Configuration = { + Token: 'YOUR TOKEN HERE' }; } @@ -15,8 +15,8 @@ export class Config { Logger.info('Loading config'); if (fs.existsSync(this.file)) { try { - this.Config = JSON.parse(fs.readFileSync(this.file)); - if (!this.Config.token); + this.Configuration = JSON.parse(fs.readFileSync(this.file)); + if (!this.Configuration.Token) Logger.panic('Token is missing from config file'); Logger.info('Config loaded successfully'); } catch (e) { diff --git a/src/events.js b/src/events.js index e69de29..c2efc96 100644 --- a/src/events.js +++ b/src/events.js @@ -0,0 +1,15 @@ +import {Logger} from './logger'; +import {Config} from './config'; + +export class Events { + async init(client) { + this.client = client; + this.client.login(Config.Configuration.Token); + } + + async handle() { + this.client.on('ready', () => { + console.log('cx'); + }); + } +} diff --git a/src/index.js b/src/index.js index 1203807..b8e4d20 100644 --- a/src/index.js +++ b/src/index.js @@ -3,8 +3,7 @@ import Discord from 'discord.js'; import {Logger} from './logger'; import {Config} from './config'; import {Database} from './database/database'; - -let client; +import {Events} from './events'; init(); async function init() { @@ -15,8 +14,10 @@ async function init() { config.load(); await Database.init(); - Logger.debug(JSON.stringify(await Database.Guilds.newGuild(1234, "Hello"), null, 4)); - Logger.debug(JSON.stringify(await Database.Guilds.deleteGuild(1234), null, 4)); + // Logger.debug(JSON.stringify(await Database.Guilds.newGuild(1234, "Hello"), null, 4)); + // Logger.debug(JSON.stringify(await Database.Guilds.deleteGuild(1234), null, 4)); - client = new Discord.Client(); + const client = new Discord.Client(); + const eventHandler = new Events(client); + eventHandler.handle(); } diff --git a/src/logger.js b/src/logger.js index 2b2c4e8..ac6b62f 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,9 +1,11 @@ import colours from 'colors/safe'; +import moment from 'moment'; import fs from 'fs'; let LogLevel = 1; let Dialect = 'SQLITE'; let logPath = 'logs.log'; +let dateFormat = 'DD-MM-YY HH:MM:ss' export class Logger { static init(path) { @@ -34,6 +36,10 @@ export class Logger { Dialect = dialect; } + static SetDateFormat(format) { + dateFormat = format; + } + static get VERBOSE_LOGS() {return 0;} static get DEBUG_LOGS() {return 1;} static get INFO_LOGS() {return 2;} @@ -41,7 +47,7 @@ export class Logger { static database(message) { if (LogLevel > 0) return; - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [${Dialect}] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.magenta(Dialect) + '] ' + message); @@ -49,7 +55,7 @@ export class Logger { static middleware(message) { if (LogLevel > 0) return; - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [HTTP-MIDDLEWARE] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.blue('HTTP-MIDDLEWARE') + '] ' + message); @@ -57,22 +63,22 @@ export class Logger { static debug(message) { if (LogLevel > 1) return; - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [DEBUG] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.cyan('DEBUG') + '] ' + message); } static ready() { - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [READY] \n`); console.log('[' + d.toLocaleString() + '] [' + colours.rainbow('READY') + ']'); } - + static info(message) { if (LogLevel > 2) return; - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [INFO] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.green('INFO') + '] ' + message); @@ -80,21 +86,21 @@ export class Logger { static warn(message) { if (LogLevel > 3) return; - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [WARN] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.yellow('WARN') + '] ' + message); } static error(message) { - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [ERROR] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.red('ERROR') + '] ' + message); } static panic(message) { - let d = new Date(); + let d = moment().format(dateFormat); fs.appendFileSync(logPath, `[${d.toLocaleString()}] [PANIC] ${message} \n`); console.log('[' + d.toLocaleString() + '] [' + colours.red('PANIC') + '] ' + message); From 5c9cf2a3104ea0cc77220af2f266d51c5b93a72b Mon Sep 17 00:00:00 2001 From: plane000 Date: Fri, 26 Oct 2018 21:14:40 +0100 Subject: [PATCH 2/4] Fixed bug where 'on' from the discord client was undefined --- src/index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index b8e4d20..533e9e9 100644 --- a/src/index.js +++ b/src/index.js @@ -18,6 +18,8 @@ async function init() { // Logger.debug(JSON.stringify(await Database.Guilds.deleteGuild(1234), null, 4)); const client = new Discord.Client(); - const eventHandler = new Events(client); + + const eventHandler = new Events(); + await eventHandler.init(client); eventHandler.handle(); } From cdd6c15e284eb4d8bd7a3850cc894a629355021a Mon Sep 17 00:00:00 2001 From: plane000 Date: Fri, 26 Oct 2018 23:02:15 +0100 Subject: [PATCH 3/4] Discord login works and fixed config instance --- src/config.js | 29 ++++++++++++++++++----------- src/events.js | 5 +++-- src/index.js | 4 ++-- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/config.js b/src/config.js index 61f93c9..6fdabc5 100644 --- a/src/config.js +++ b/src/config.js @@ -2,21 +2,28 @@ import fs from 'fs'; import {Logger} from './logger'; +let file; +let path; +let Configuration; + export class Config { - constructor(path) { - this.file = './resources/config/config.json'; - this.path = [ './resources/', './resources/config' ] - this.Configuration = { + static get Configuration() {return Configuration;} + static get Token() {return Configuration.Token;} + + static init() { + file = './resources/config/config.json'; + path = [ './resources/', './resources/config' ] + Configuration = { Token: 'YOUR TOKEN HERE' }; } - load() { + static load() { Logger.info('Loading config'); - if (fs.existsSync(this.file)) { + if (fs.existsSync(file)) { try { - this.Configuration = JSON.parse(fs.readFileSync(this.file)); - if (!this.Configuration.Token) Logger.panic('Token is missing from config file'); + Configuration = JSON.parse(fs.readFileSync(file)); + if (!Configuration.Token) Logger.panic('Token is missing from config file'); Logger.info('Config loaded successfully'); } catch (e) { @@ -25,11 +32,11 @@ export class Config { } else { try { Logger.error('No config found'); - for (let folder of this.path) { + for (let folder of path) { fs.mkdirSync(folder); } - fs.writeFileSync(this.file, JSON.stringify(this.Config, null, 4)); - Logger.warn(`Created config at: ${this.path}`); + fs.writeFileSync(file, JSON.stringify(Configuration, null, 4)); + Logger.warn(`Created config at: ${path}`); Logger.panic('Config required to be complete'); } catch (e) { Logger.panic(`Could not create config: ${e}`); diff --git a/src/events.js b/src/events.js index c2efc96..9168dec 100644 --- a/src/events.js +++ b/src/events.js @@ -4,12 +4,13 @@ import {Config} from './config'; export class Events { async init(client) { this.client = client; - this.client.login(Config.Configuration.Token); + this.client.login(Config.Token); } async handle() { this.client.on('ready', () => { - console.log('cx'); + Logger.info(`Discord client logged in as ${this.client.user.tag}`); + Logger.ready(); }); } } diff --git a/src/index.js b/src/index.js index 533e9e9..512d0f0 100644 --- a/src/index.js +++ b/src/index.js @@ -10,8 +10,8 @@ async function init() { Logger.init(); Logger.SetLevel(Logger.VERBOSE_LOGS); - const config = new Config(); - config.load(); + Config.init(); + Config.load(); await Database.init(); // Logger.debug(JSON.stringify(await Database.Guilds.newGuild(1234, "Hello"), null, 4)); From 0c0f69202d7f4ffc2e7f3853f1a713a0539aa2d9 Mon Sep 17 00:00:00 2001 From: plane000 Date: Sat, 27 Oct 2018 14:27:03 +0100 Subject: [PATCH 4/4] Minor bug fixes now successful connenct to discord --- package.json | 2 -- src/config.js | 5 ++++- src/events.js | 20 +++++++++++++++----- src/index.js | 4 +--- test/index.js | 3 +++ 5 files changed, 23 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 4633e4e..05b101a 100644 --- a/package.json +++ b/package.json @@ -15,10 +15,8 @@ "body-parser": "^1.18.3", "colors": "^1.3.2", "discord.js": "^11.4.2", - "express": "^4.16.4", "moment": "^2.22.2", "regenerator-runtime": "^0.12.1", - "request": "^2.88.0", "sequelize": "^4.39.1", "sqlite3": "^4.0.2" } diff --git a/src/config.js b/src/config.js index 6fdabc5..760db83 100644 --- a/src/config.js +++ b/src/config.js @@ -9,12 +9,14 @@ let Configuration; export class Config { static get Configuration() {return Configuration;} static get Token() {return Configuration.Token;} + static get NowPlaying() {return Configuration.NowPlaying;} static init() { file = './resources/config/config.json'; path = [ './resources/', './resources/config' ] Configuration = { - Token: 'YOUR TOKEN HERE' + Token: 'BOT TOKEN HERE', + NowPlaying: 'PLAYING GAME HERE' }; } @@ -24,6 +26,7 @@ export class Config { try { Configuration = JSON.parse(fs.readFileSync(file)); if (!Configuration.Token) Logger.panic('Token is missing from config file'); + if (!Configuration.NowPlaying) Logger.panic('NowPlaying is missing from config file'); Logger.info('Config loaded successfully'); } catch (e) { diff --git a/src/events.js b/src/events.js index 9168dec..05c4a06 100644 --- a/src/events.js +++ b/src/events.js @@ -7,10 +7,20 @@ export class Events { this.client.login(Config.Token); } - async handle() { - this.client.on('ready', () => { - Logger.info(`Discord client logged in as ${this.client.user.tag}`); - Logger.ready(); - }); + async handleEvents() { + this.client.on('ready', () => {this.handleReady()}); + this.client.on('message', async (message) => {this.handleMessage(message)}); + } + + async handleReady() { + this.client.user.setPresence('online'); + this.client.user.setActivity(Config.NowPlaying); + Logger.info(`Discord client logged in as ${this.client.user.tag}`); + Logger.ready(); + } + + async handleMessage(...args) { + Logger.info(args[0]); + args[0].channel.send('lol u homo') } } diff --git a/src/index.js b/src/index.js index 512d0f0..8d716e6 100644 --- a/src/index.js +++ b/src/index.js @@ -14,12 +14,10 @@ async function init() { Config.load(); await Database.init(); - // Logger.debug(JSON.stringify(await Database.Guilds.newGuild(1234, "Hello"), null, 4)); - // Logger.debug(JSON.stringify(await Database.Guilds.deleteGuild(1234), null, 4)); const client = new Discord.Client(); const eventHandler = new Events(); await eventHandler.init(client); - eventHandler.handle(); + eventHandler.handleEvents(); } diff --git a/test/index.js b/test/index.js index e69de29..051d8f0 100644 --- a/test/index.js +++ b/test/index.js @@ -0,0 +1,3 @@ + // Logger.debug(JSON.stringify(await Database.Guilds.newGuild(1234, "Hello"), null, 4)); + // Logger.debug(JSON.stringify(await Database.Guilds.deleteGuild(1234), null, 4)); + \ No newline at end of file