galactic-bot/structure/extensions/Guild.js

193 lines
6.0 KiB
JavaScript

const escapeRegex = require('escape-string-regexp');
const { Structures } = require('discord.js');
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.
}
//Fetch and cache settings
async settings() {
if(!this._settings) this._settings = this.client.transactionHandler.send({ provider: 'mongodb', request: { collection: 'guilds', type: 'findOne', query: { guildId: this.id } } });
if(this._settings instanceof Promise) this._settings = await this._settings || null;
if(!this._settings) this._settings = { guildId: this.id, ...this.client.defaultConfig } ;
return this._settings;
}
//Fetch and cache perms
async permissions() {
if(!this._permissions) this._permissions = this.client.transactionHandler.send({ provider: 'mongodb', request: { collection: 'permissions', type: 'findOne', query: { 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;
}
/* Settings Wrapper */
async _dbDelete(collection) { //Delete whole entry - remove
try {
await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'remove',
collection,
query: {
guildId: this.id
}
}
});
this._settings = this.client.defaultConfig;
this._storageLog(`Database Delete (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
}
async _dbUpdateOne(data, collection) { //Update property (upsert true) - updateOne
var index = this.dbIndex(collection); //eslint-disable-line no-unused-vars
try {
await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'updateOne',
collection,
query: {
guildId: this.id
},
data
}
});
index = {
...index,
...data
};
this._storageLog(`Database Update (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
}
async _dbRemoveProperty(value, collection) { //Remove property
const index = this.dbIndex(collection);
if(collection === 'guild' && this.client.defaultConfig[value]) {
await this._updateSettings(this.client.defaultConfig[value]);
return undefined;
}
try {
await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'removeProperty',
collection,
query: {
guildId: this.id
},
data: [
value
]
}
});
delete index[value];
this._storageLog(`Database Remove (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
}
_dbIndex(collection) {
return {
'guilds': this._settings,
'permissions': this._permissions
}[collection];
}
/* Permissions Wrapper */
/* Language Formatting */
format(key, parameters = {}) {
const language = this._settings.locale || 'en_us'; //this._settings.language or something idk
let value = this.client.localeLoader.languages[language][key];
for(const [param, val] of Object.entries(parameters)) {
value = value.replace(new RegExp(`{${escapeRegex(param.toLowerCase())}}`, 'gi'), val);
}
return value;
}
/* Resolver Shortcuts */
async resolveMembers(members, strict = false) {
return await this.client.resolver.resolveMembers(members, this, strict);
}
async resolveMember(member, strict) {
let [ result ] = await this.resolveMembers([ member ], strict);
return result;
}
async resolveChannels(channels, strict = false) {
return await this.client.resolver.resolveChannels(channels, this, strict);
}
async resolveChannel(channel, strict) {
let [ result ] = await this.resolveMembers([ channel ], strict);
return result;
}
async resolveRoles(roles, strict = false) {
return await this.client.resolver.resolveRoles(roles, this, strict);
}
async resolveRole(role, strict) {
let [ result ] = await this.resolveRoles([ role ], strict);
return result;
}
/* Logging */
_storageLog(log) {
this.client.logger.debug(log);
}
_storageError(error) {
this.client.logger.error(`Database Error (guild:${this.id}) : \n${error.stack || error}`);
}
get prefix() {
return this._settings.prefix;
}
}
return ExtendedGuild;
});
module.exports = Guild;