galactic-bot/structure/client/DiscordClient.js

152 lines
4.4 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');
const LocaleLoader = require('../language/LocaleLoader.js');
2020-06-19 15:37:25 +02:00
const RateLimiter = require('./RateLimiter.js');
2020-04-08 18:08:46 +02:00
const StorageManager = require('../storage/StorageManager.js');
const ModerationManager = require('../moderation/ModerationManager.js');
const { Guild, GuildMember, User, Message, TextChannel, Role } = require('../../structure/extensions/'); //eslint-disable-line no-unused-vars
const { Command, Observer, Inhibitor, Setting } = require('../../structure/interfaces/');
const { DefaultGuild, DefaultUser } = require('../../util/defaults/');
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-19 12:12:10 +02:00
this.localeLoader = new LocaleLoader(this);
2020-06-19 15:37:25 +02:00
this.rateLimiter = new RateLimiter(this);
2020-04-08 18:08:46 +02:00
this.storageManager = new StorageManager(this, options.storage);
this.moderationManager = new ModerationManager(this);
2020-04-08 18:08:46 +02:00
this._options = options;
this._built = false;
//TODO: Default config for users and guilds.
this._defaultConfig = {};
this._permissionCheck = null;
2020-08-14 16:13:29 +02:00
this._evals = new Map();
process.on('message', this._handleMessage.bind(this));
}
2020-04-08 18:08:46 +02:00
async build() {
if(this._built) return undefined;
const beforeTime = Date.now();
2020-04-09 16:30:52 +02:00
await super.login(this._options.bot.token);
this.localeLoader.loadLanguages();
2020-04-19 12:12:10 +02:00
await this.storageManager.initialize();
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);
2020-04-09 16:30:52 +02:00
2020-06-16 00:15:13 +02:00
await this.moderationManager.initialize();
await this.dispatcher.dispatch();
2020-04-08 18:08:46 +02:00
2020-08-13 22:45:23 +02:00
this.logger.info(`Took ${Date.now()-beforeTime}ms to build the client.`);
2020-04-08 18:08:46 +02:00
this._built = true;
return this._built;
2020-04-08 18:08:46 +02:00
}
2020-08-14 16:13:29 +02:00
async resolveUsers() {
return this.resolver.resolveUsers(...arguments);
}
async resolveUser() {
return this.resolver.resolveUser(...arguments);
}
async _handleMessage(message) {
//Handle misc. messages.
2020-08-14 16:13:29 +02:00
if (message._evalResult) this.evalResult(message);
}
async managerEval(script) {
return new Promise((resolve, reject) => {
this._evals.set(script, { resolve, reject });
process.send({ _mEval: true, script });
});
}
evalResult({ script, result, error }) {
const promise = this._evals.get(script);
if (result) promise.resolve(result);
else promise.reject(error);
this._evals.delete(script);
}
defaultConfig(type) {
if(this._defaultConfig[type]) return this._defaultConfig[type];
const settings = this.registry.components.filter((c) => c.type === 'setting' && c.resolve === type);
let def = type === 'GUILD' ? DefaultGuild : DefaultUser;
for(const setting of settings.values()) {
if(setting.default !== null) {
def = {
...def,
...setting.default
};
}
}
this._defaultConfig[type] = def;
return def;
2020-07-28 05:00:24 +02:00
}
get permissions() {
if(this._permissionCheck) return this._permissionCheck;
return this._permissionCheck = this.registry.components.get('inhibitor:permissions'); //eslint-disable-line no-return-assign
}
2020-07-26 22:09:25 +02:00
get prefix() {
return this._options.prefix;
}
2020-08-14 21:45:03 +02:00
get storage() {
return this.storageManager;
}
2020-04-08 18:08:46 +02:00
}
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
});