2020-04-19 12:12:10 +02:00
|
|
|
const escapeRegex = require('escape-string-regexp');
|
2020-04-08 18:08:46 +02:00
|
|
|
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.
|
2020-05-07 01:26:16 +02:00
|
|
|
this._permissions = null; //internal cache, should always match database.
|
2020-04-08 18:08:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-07 17:08:07 +02:00
|
|
|
//Fetch and cache settings
|
2020-04-19 10:27:13 +02:00
|
|
|
async settings() {
|
2020-05-08 08:50:54 +02:00
|
|
|
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 } ;
|
2020-04-19 10:27:13 +02:00
|
|
|
return this._settings;
|
2020-05-07 01:26:16 +02:00
|
|
|
}
|
2020-04-19 10:27:13 +02:00
|
|
|
|
2020-05-07 17:08:07 +02:00
|
|
|
//Fetch and cache perms
|
2020-05-08 08:50:54 +02:00
|
|
|
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 };
|
2020-05-07 01:26:16 +02:00
|
|
|
return this._permissions;
|
2020-04-19 10:27:13 +02:00
|
|
|
}
|
2020-04-19 12:12:10 +02:00
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
/* Settings Wrapper */
|
2020-05-06 01:40:46 +02:00
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
async _dbDelete(collection) { //Delete whole entry - remove
|
2020-05-06 01:40:46 +02:00
|
|
|
try {
|
|
|
|
await this.client.transactionHandler.send({
|
|
|
|
provider: 'mongodb',
|
|
|
|
request: {
|
|
|
|
type: 'remove',
|
2020-05-07 01:26:16 +02:00
|
|
|
collection,
|
2020-05-06 01:40:46 +02:00
|
|
|
query: {
|
|
|
|
guildId: this.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
this._settings = this.client.defaultConfig;
|
|
|
|
this._storageLog(`Database Delete (guild:${this.id}).`);
|
|
|
|
} catch(error) {
|
|
|
|
this._storageError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
async _dbUpdateOne(data, collection) { //Update property (upsert true) - updateOne
|
2020-05-08 08:50:54 +02:00
|
|
|
var index = this.dbIndex(collection); //eslint-disable-line no-unused-vars
|
2020-05-06 01:40:46 +02:00
|
|
|
try {
|
|
|
|
await this.client.transactionHandler.send({
|
|
|
|
provider: 'mongodb',
|
|
|
|
request: {
|
|
|
|
type: 'updateOne',
|
2020-05-07 01:26:16 +02:00
|
|
|
collection,
|
2020-05-06 01:40:46 +02:00
|
|
|
query: {
|
|
|
|
guildId: this.id
|
|
|
|
},
|
|
|
|
data
|
|
|
|
}
|
|
|
|
});
|
2020-05-07 01:26:16 +02:00
|
|
|
index = {
|
|
|
|
...index,
|
2020-05-06 01:40:46 +02:00
|
|
|
...data
|
|
|
|
};
|
|
|
|
this._storageLog(`Database Update (guild:${this.id}).`);
|
|
|
|
} catch(error) {
|
|
|
|
this._storageError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
async _dbRemoveProperty(value, collection) { //Remove property
|
|
|
|
const index = this.dbIndex(collection);
|
|
|
|
if(collection === 'guild' && this.client.defaultConfig[value]) {
|
2020-05-06 01:40:46 +02:00
|
|
|
await this._updateSettings(this.client.defaultConfig[value]);
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.client.transactionHandler.send({
|
|
|
|
provider: 'mongodb',
|
|
|
|
request: {
|
|
|
|
type: 'removeProperty',
|
2020-05-07 01:26:16 +02:00
|
|
|
collection,
|
2020-05-06 01:40:46 +02:00
|
|
|
query: {
|
|
|
|
guildId: this.id
|
|
|
|
},
|
|
|
|
data: [
|
|
|
|
value
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
2020-05-07 01:26:16 +02:00
|
|
|
delete index[value];
|
2020-05-06 01:40:46 +02:00
|
|
|
this._storageLog(`Database Remove (guild:${this.id}).`);
|
|
|
|
} catch(error) {
|
|
|
|
this._storageError(error);
|
|
|
|
}
|
|
|
|
}
|
2020-05-07 01:26:16 +02:00
|
|
|
|
2020-05-08 08:50:54 +02:00
|
|
|
_dbIndex(collection) {
|
2020-05-07 01:26:16 +02:00
|
|
|
return {
|
|
|
|
'guilds': this._settings,
|
|
|
|
'permissions': this._permissions
|
|
|
|
}[collection];
|
2020-05-06 01:40:46 +02:00
|
|
|
}
|
2020-05-07 01:26:16 +02:00
|
|
|
|
|
|
|
/* Permissions Wrapper */
|
|
|
|
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
|
|
|
|
/* Language Formatting */
|
|
|
|
|
2020-04-19 12:15:38 +02:00
|
|
|
format(key, parameters = {}) {
|
2020-04-19 21:54:50 +02:00
|
|
|
|
2020-05-07 17:08:07 +02:00
|
|
|
const language = this._settings.locale || 'en_us'; //this._settings.language or something idk
|
2020-04-19 12:12:10 +02:00
|
|
|
|
|
|
|
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;
|
2020-04-19 21:54:50 +02:00
|
|
|
|
2020-04-19 12:12:10 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
/* Resolver Shortcuts */
|
|
|
|
|
2020-04-11 10:06:39 +02:00
|
|
|
async resolveMembers(members, strict = false) {
|
|
|
|
|
|
|
|
return await this.client.resolver.resolveMembers(members, this, strict);
|
2020-04-17 17:23:13 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async resolveMember(member, strict) {
|
|
|
|
|
|
|
|
let [ result ] = await this.resolveMembers([ member ], strict);
|
|
|
|
return result;
|
|
|
|
|
2020-04-11 10:06:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async resolveChannels(channels, strict = false) {
|
|
|
|
|
|
|
|
return await this.client.resolver.resolveChannels(channels, this, strict);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:23:13 +02:00
|
|
|
async resolveChannel(channel, strict) {
|
|
|
|
|
|
|
|
let [ result ] = await this.resolveMembers([ channel ], strict);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:06:39 +02:00
|
|
|
async resolveRoles(roles, strict = false) {
|
|
|
|
|
|
|
|
return await this.client.resolver.resolveRoles(roles, this, strict);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:23:13 +02:00
|
|
|
async resolveRole(role, strict) {
|
|
|
|
|
|
|
|
let [ result ] = await this.resolveRoles([ role ], strict);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Logging */
|
|
|
|
|
|
|
|
_storageLog(log) {
|
2020-05-07 17:08:07 +02:00
|
|
|
this.client.logger.debug(log);
|
2020-05-06 01:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_storageError(error) {
|
2020-05-07 17:08:07 +02:00
|
|
|
this.client.logger.error(`Database Error (guild:${this.id}) : \n${error.stack || error}`);
|
2020-05-06 01:40:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
get prefix() {
|
|
|
|
return this._settings.prefix;
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ExtendedGuild;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Guild;
|