152 lines
4.4 KiB
JavaScript
152 lines
4.4 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 LocaleLoader = require('../language/LocaleLoader.js');
|
|
const RateLimiter = require('./RateLimiter.js');
|
|
|
|
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/');
|
|
|
|
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.localeLoader = new LocaleLoader(this);
|
|
this.rateLimiter = new RateLimiter(this);
|
|
|
|
this.storageManager = new StorageManager(this, options.storage);
|
|
this.moderationManager = new ModerationManager(this);
|
|
|
|
this._options = options;
|
|
this._built = false;
|
|
|
|
//TODO: Default config for users and guilds.
|
|
this._defaultConfig = {};
|
|
this._permissionCheck = null;
|
|
|
|
this._evals = new Map();
|
|
|
|
process.on('message', this._handleMessage.bind(this));
|
|
|
|
}
|
|
|
|
async build() {
|
|
|
|
if(this._built) return undefined;
|
|
|
|
const beforeTime = Date.now();
|
|
await super.login(this._options.bot.token);
|
|
|
|
this.localeLoader.loadLanguages();
|
|
|
|
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);
|
|
|
|
await this.moderationManager.initialize();
|
|
await this.dispatcher.dispatch();
|
|
|
|
this.logger.info(`Took ${Date.now()-beforeTime}ms to build the client.`);
|
|
|
|
this._built = true;
|
|
return this._built;
|
|
|
|
}
|
|
|
|
async resolveUsers() {
|
|
|
|
return this.resolver.resolveUsers(...arguments);
|
|
|
|
}
|
|
|
|
async resolveUser() {
|
|
|
|
return this.resolver.resolveUser(...arguments);
|
|
|
|
}
|
|
|
|
async _handleMessage(message) {
|
|
//Handle misc. messages.
|
|
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;
|
|
|
|
}
|
|
|
|
get permissions() {
|
|
if(this._permissionCheck) return this._permissionCheck;
|
|
return this._permissionCheck = this.registry.components.get('inhibitor:permissions'); //eslint-disable-line no-return-assign
|
|
}
|
|
|
|
get prefix() {
|
|
return this._options.prefix;
|
|
}
|
|
|
|
get storage() {
|
|
return this.storageManager;
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
}); |