galactic-bot/structure/client/DiscordClient.js
2020-04-12 15:35:27 +03:00

69 lines
1.8 KiB
JavaScript

const { Client } = require('discord.js');
const options = require('../../options.json');
const Registry = require('./Registry.js')
const EventHooker = require('./EventHooker.js');
const Dispatcher = require('./Dispatcher.js')
const Resolver = require('./Resolver.js');
const Transporter = require('./Transporter.js');
const { Guild, User, Message } = require('../../structure/extensions/');
const { Command, Observer, Inhibitor, Setting } = require('../../structure/interfaces/');
class DiscordClient extends Client {
constructor(options) {
super(options.bot.clientOptions);
this.registry = new Registry(this);
this.eventHooker = new EventHooker(this);
this.dispatcher = new Dispatcher(this);
this.resolver = new Resolver(this);
this.transporter = new Transporter(this);
this._options = options;
this._built = false;
}
async build() {
if(this._built) return undefined;
console.log('Building Discord client');
await super.login(this._options.bot.token);
await this.registry.loadComponents('components/commands/', Command);
await this.registry.loadComponents('components/observers', Observer);
await this.dispatcher.dispatch();
this._built = true;
this.on('ready', () => {
console.log('Client websocket is ready.');
});
this.registry.on('componentUpdate', (comp, type) => {
console.log(`[registry][${type}] ${comp.resolveable}`)
});
console.log('Client built');
}
}
module.exports = DiscordClient;
const client = new DiscordClient(options);
client.build();
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error); //eslint-disable-line no-console
});