galactic-bot/structure/client/DiscordClient.js

71 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-04-08 18:08:46 +02:00
const { Client } = require('discord.js');
2020-04-09 16:30:52 +02:00
const options = require('../../options.json');
2020-04-14 17:05:56 +02:00
const Registry = require('./Registry.js');
2020-04-08 18:08:46 +02:00
const EventHooker = require('./EventHooker.js');
2020-04-14 17:05:56 +02:00
const Dispatcher = require('./Dispatcher.js');
2020-04-08 18:08:46 +02:00
const Resolver = require('./Resolver.js');
2020-04-13 22:38:10 +02:00
const Logger = require('./Logger.js');
2020-04-14 17:05:56 +02:00
const TransactionHandler = require('./TransactionHandler.js');
2020-04-19 12:12:10 +02:00
const LocaleLoader = require('../../language/LocaleLoader.js');
2020-04-08 18:08:46 +02:00
const { Guild, User, Message } = require('../../structure/extensions/'); //eslint-disable-line
const { Command, Observer, Inhibitor, Setting } = require('../../structure/interfaces/');
2020-04-08 18:08:46 +02:00
class DiscordClient extends Client {
2020-04-09 16:30:52 +02:00
constructor(options) {
2020-04-08 18:08:46 +02:00
2020-04-09 16:30:52 +02:00
super(options.bot.clientOptions);
this.registry = new Registry(this);
2020-04-08 18:08:46 +02:00
this.eventHooker = new EventHooker(this);
this.dispatcher = new Dispatcher(this);
this.resolver = new Resolver(this);
2020-04-13 22:38:10 +02:00
this.logger = new Logger(this);
2020-04-14 17:05:56 +02:00
this.transactionHandler = new TransactionHandler(this);
2020-04-19 12:12:10 +02:00
this.localeLoader = new LocaleLoader(this);
2020-04-08 18:08:46 +02:00
this._options = options;
this._built = false;
process.on('message', this._handleMessage.bind(this));
}
async _handleMessage(message) {
if(message._storage) this.transactionHandler._receive(message);
2020-04-08 18:08:46 +02:00
}
async build() {
if(this._built) return undefined;
2020-04-09 16:30:52 +02:00
await super.login(this._options.bot.token);
2020-04-19 21:53:47 +02:00
await this.localeLoader.loadLanguages();
2020-04-19 12:12:10 +02:00
await this.registry.loadComponents('components/inhibitors/', Inhibitor);
await this.registry.loadComponents('components/commands/', Command);
await this.registry.loadComponents('components/observers/', Observer);
2020-04-19 12:12:10 +02:00
// await this.registry.loadComponents('components/settings/', Setting);
2020-04-09 16:30:52 +02:00
await this.dispatcher.dispatch();
2020-04-08 18:08:46 +02:00
this._built = true;
}
}
module.exports = DiscordClient;
2020-04-09 16:30:52 +02:00
const client = new DiscordClient(options);
2020-04-12 14:35:27 +02:00
client.build();
process.on("unhandledRejection", (error) => {
console.error("Unhandled promise rejection:", error); //eslint-disable-line no-console
});