galactic-bot/structure/extensions/Guild.js
2020-08-17 13:26:57 -07:00

226 lines
7.5 KiB
JavaScript

const escapeRegex = require('escape-string-regexp');
const { Structures } = require('discord.js');
const { Collection, Emojis } = require('../../util');
const safeCommands = ['command:settings', 'command:grant', 'command:revoke'];
const Guild = Structures.extend('Guild', (Guild) => {
class ExtendedGuild extends Guild {
constructor(...args) {
super(...args);
this._settings = null; //internal cache of current guild's settings; should ALWAYS stay the same as database.
this._permissions = null; //internal cache, should always match database.
this.callbacks = [];
this.webhooks = new Collection();
}
//Fetch and cache settings
async settings() {
if(!this._settings) this._settings = this.client.storageManager.mongodb.guilds.findOne({ guildId: this.id });
if(this._settings instanceof Promise) this._settings = await this._settings || null;
if(!this._settings) this._settings = { guildId: this.id, ...this.defaultConfig };
// else this._settings = Object.assign({}, { ...this.defaultConfig, ...this._settings }); //eslint-disable-line prefer-object-spread
else this._settings = { ...this.defaultConfig, ...this._settings }; //eslint-disable-line prefer-object-spread
return this._settings;
}
//Fetch and cache perms
async permissions() {
if(!this._permissions) this._permissions = this.client.storageManager.mongodb.permissions.findOne({ guildId: this.id });
if(this._permissions instanceof Promise) this._permissions = await this._permissions || null;
if(!this._permissions) this._permissions = { guildId: this.id };
return this._permissions;
}
async caseId() {
if(!this._settings) await this.settings();
return this._caseId = this._settings.caseId; //eslint-disable-line no-return-assign
}
/* Settings Wrapper */
async _deleteSettings() { //Delete whole entry - remove
try {
await this.client.storageManager.guilds.deleteOne({ guildId: this.id });
this._settings = { ...{}, ...this.defaultConfig }; //Create a new object so settings that change the _settings value won't replicate it towards the defaultConfig.
this._storageLog(`Database Delete (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
return true;
}
async _resetSettings() {
if(!this._settings) await this.settings();
try {
await this.client.storageManager.mongodb.guilds.updateOne(
{ guildId: this.id },
{ caseId: this._settings.caseId, guildId: this.id },
false //No upsert
);
this._settings = {
...this.defaultConfig,
...{ caseId: this._settings.caseId }
};
this._storageLog(`Database Reset (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
}
async _updateSettings(data) { //Update property (upsert true) - updateOne
if(!this._settings) await this.settings();
try {
await this.client.storageManager.mongodb.guilds.updateOne(
{ guildId: this.id },
data
);
this._settings = {
...this._settings,
...data
};
this._storageLog(`Database Update (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
return true;
}
async _removeSettings(value) { //Remove property
if(!this._settings) await this.settings();
if(this.defaultConfig[value]) {
await this._updateSettings({ [value]: this.defaultConfig[value] });
return undefined;
}
try {
await this.client.storageManager.mongodb.guilds.removeProperty(
{ guildId: this.id },
[ value ]
);
this._storageLog(`Database Remove (guild:${this.id}).`);
delete this._settings[value];
} catch(error) {
this._storageError(error);
}
return true;
}
/* Language Formatting */
format(index, parameters = {}, code = false) {
let language = 'en_us';
if (this._settings.locale) language = this._settings.locale;
parameters.prefix = this.prefix;
let template = this.client.localeLoader.template(language, index); //.languages[language][index];
for(const emoji of Object.keys(Emojis)) {
parameters[`emoji_${emoji}`] = Emojis[emoji];
}
if(!template) {
return `**Missing language index \`${language} [${index}]\` in languages. Contact a bot developer about this.**`;
}
for (const [param, val] of Object.entries(parameters)) {
template = template.replace(new RegExp(`{${escapeRegex(param.toLowerCase())}}`, 'gi'), val); //eslint-disable-line require-unicode-regexp
}
if(code) {
try {
template = eval(template); //eslint-disable-line no-eval
} catch(error) {
this.command.client.logger.error(`Error in locale ${language}:${index} while executing code.\n${error.stack || error}`);
}
}
return template;
}
/* Resolver Shortcuts */
async resolveMembers(members, strict) {
return this.client.resolver.resolveMembers(members, strict, this);
}
async resolveMember(member, strict) {
return this.client.resolver.resolveMembers(member, strict, this);
}
async resolveChannels(channels, strict) {
return this.client.resolver.resolveChannels(channels, strict, this);
}
async resolveChannel(channel, strict) {
return this.client.resolver.resolveChannel(channel, strict, this);
}
async resolveRoles(roles, strict) {
return this.client.resolver.resolveRoles(roles, strict, this);
}
async resolveRole(role, strict) {
return this.client.resolver.resolveRole(role, strict, this);
}
/* Logging */
_storageLog(log) {
this.client.logger.debug(log);
}
_storageError(error) {
this.client.logger.error(`Database Error (guild:${this.id}) : \n${error.stack || error}`);
}
_debugLog(log) {
this.client.logger.debug(`[${this.name}](${this.id}): ${log}`);
}
/* Lazy Developer Getters */
get defaultConfig() {
return JSON.parse(JSON.stringify(this.client.defaultConfig('GUILD')));
}
get prefix() {
return this._settings.prefix
|| this.client._options.bot.prefix;
}
get premium() { //GUILD SETTINGS MUST BE FETCHED
return this._settings.premium;
}
get debug() { //GUILD SETTINGS MUST BE FETCHED
return this._settings.debug;
}
}
return ExtendedGuild;
});
module.exports = Guild;