galactic-bot/structure/client/DiscordClient.js
2020-04-19 03:12:10 -07:00

71 lines
2.1 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 Logger = require('./Logger.js');
const TransactionHandler = require('./TransactionHandler.js');
const LocaleLoader = require('../../language/LocaleLoader.js');
const { Guild, User, Message } = require('../../structure/extensions/'); //eslint-disable-line
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.logger = new Logger(this);
this.transactionHandler = new TransactionHandler(this);
this.localeLoader = new LocaleLoader(this);
this._options = options;
this._built = false;
process.on('message', this._handleMessage.bind(this));
}
async _handleMessage(message) {
if(message._storage) this.transactionHandler._receive(message);
}
async build() {
if(this._built) return undefined;
await super.login(this._options.bot.token);
await this.localeLoader.loadLanguage('en_us');
await this.registry.loadComponents('components/inhibitors/', Inhibitor);
await this.registry.loadComponents('components/commands/', Command);
await this.registry.loadComponents('components/observers/', Observer);
// await this.registry.loadComponents('components/settings/', Setting);
await this.dispatcher.dispatch();
this._built = true;
}
}
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
});