diff --git a/index.js b/index.js index d2eb20a..7aaa68f 100644 --- a/index.js +++ b/index.js @@ -3,4 +3,4 @@ require('babel-register')({ presets: [ 'env' ] }); -require('./src/index'); +require('./src/index').init(); diff --git a/src/events.js b/src/events.js index 05c4a06..564171b 100644 --- a/src/events.js +++ b/src/events.js @@ -1,22 +1,57 @@ import {Logger} from './logger'; import {Config} from './config'; +import { resolve } from 'path'; +import { rejects } from 'assert'; -export class Events { +export class Events { // extends rate limits async init(client) { this.client = client; this.client.login(Config.Token); } async handleEvents() { - this.client.on('ready', () => {this.handleReady()}); - this.client.on('message', async (message) => {this.handleMessage(message)}); + this.client.on('ready', async () => { + this.handle(undefined, + [this.handleReady] + ); + }); + + this.client.on('message', async (message) => { + this.handle(message, + [this.handleMessage] + ); + }); } - async handleReady() { + async handle(obj = [null], callbacks = [undefined]) { + let doNext = false; + if (callbacks.length == 0) { + return; + } + + const next = function() { + doNext = true; + } + + await callbacks[0](obj, next); + callbacks.splice(0, 1); + + if (doNext) { + this.handle(obj, callbacks); + } + } + + async handleReady(obj, next) { this.client.user.setPresence('online'); this.client.user.setActivity(Config.NowPlaying); Logger.info(`Discord client logged in as ${this.client.user.tag}`); Logger.ready(); + + await new Promise((resolve, reject) => { + setTimeout(resolve, 1000); + }); + + next(); } async handleMessage(...args) { diff --git a/src/index.js b/src/index.js index 8d716e6..ccb212f 100644 --- a/src/index.js +++ b/src/index.js @@ -5,8 +5,9 @@ import {Config} from './config'; import {Database} from './database/database'; import {Events} from './events'; -init(); -async function init() { +let client; + +export async function init() { Logger.init(); Logger.SetLevel(Logger.VERBOSE_LOGS); @@ -15,7 +16,7 @@ async function init() { await Database.init(); - const client = new Discord.Client(); + client = new Discord.Client(); const eventHandler = new Events(); await eventHandler.init(client); diff --git a/src/ratelimits.js b/src/ratelimits.js new file mode 100644 index 0000000..e69de29