galactic-bot/structure/extensions/Guild.js

211 lines
6.7 KiB
JavaScript
Raw Normal View History

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');
2020-06-18 16:07:39 +02:00
const { Collection } = require('../../util');
2020-04-08 18:08:46 +02:00
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.
2020-06-04 19:59:09 +02:00
2020-05-24 23:43:47 +02:00
this.callbacks = [];
2020-06-18 16:07:39 +02:00
this.webhooks = new Collection();
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() {
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 };
else this._settings = { ...this.client.defaultConfig, ...this._settings };
2020-04-19 10:27:13 +02:00
return this._settings;
}
2020-04-19 10:27:13 +02:00
2020-05-07 17:08:07 +02:00
//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;
2020-04-19 10:27:13 +02:00
}
2020-04-19 12:12:10 +02:00
2020-06-04 19:59:09 +02:00
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.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'remove',
collection: 'guilds',
query: {
guildId: this.id
}
}
});
this._settings = this.client.defaultConfig;
this._storageLog(`Database Delete (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
return true;
}
async _updateSettings(data) { //Update property (upsert true) - updateOne
try {
await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'updateOne',
collection: 'guilds',
query: {
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.client.defaultConfig[value]) {
await this._updateSettings({ [value]: this.client.defaultConfig[value] });
return undefined;
}
try {
await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
type: 'removeProperty',
collection: 'guilds',
query: {
guildId: this.id
},
data: [
value
]
}
});
this._storageLog(`Database Remove (guild:${this.id}).`);
delete this._settings[value];
} catch(error) {
this._storageError(error);
}
return true;
}
/* Language Formatting */
2020-05-24 23:43:47 +02:00
format(index, parameters = {}, code = false) {
2020-04-19 21:54:50 +02:00
2020-05-24 23:43:47 +02:00
let language = 'en_us';
if (this._settings.locale) language = this._settings.locale;
2020-04-19 12:12:10 +02:00
2020-05-24 23:43:47 +02:00
parameters.prefix = this.prefix;
let template = this.client.localeLoader.template(language, index);
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())}}`, 'giu'), val);
2020-04-19 12:12:10 +02:00
}
2020-05-24 23:43:47 +02:00
if (code) {
try {
// eslint-disable-next-line no-eval
template = eval(template);
} catch (error) {
this.client.logger.error(`Error in locale ${language}:${index} while executing code.\n${error.stack || error}`);
}
}
return template;
2020-04-19 21:54:50 +02:00
2020-04-19 12:12:10 +02:00
}
/* Resolver Shortcuts */
async resolveMembers(members, strict) {
2020-04-11 10:06:39 +02:00
return this.client.resolver.resolveMembers(members, strict, this);
}
async resolveMember(member, strict) {
return this.client.resolver.resolveMembers(member, strict, this);
2020-04-11 10:06:39 +02:00
}
resolveChannels(channels, strict) {
2020-04-11 10:06:39 +02:00
return this.client.resolver.resolveChannels(channels, strict, this);
2020-04-11 10:06:39 +02:00
}
2020-05-22 22:13:47 +02:00
resolveChannel(channel, strict) {
return this.client.resolver.resolveChannel(channel, strict, this);
}
async resolveRoles(roles, strict) {
2020-04-11 10:06:39 +02:00
return this.client.resolver.resolveRoles(roles, strict, this);
2020-04-11 10:06:39 +02:00
}
async resolveRole(role, strict) {
2020-05-22 22:13:47 +02:00
return this.client.resolver.resolveRole(role, strict, this);
}
/* Logging */
_storageLog(log) {
2020-05-07 17:08:07 +02:00
this.client.logger.debug(log);
}
_storageError(error) {
2020-05-07 17:08:07 +02:00
this.client.logger.error(`Database Error (guild:${this.id}) : \n${error.stack || error}`);
}
get prefix() {
2020-05-24 05:50:35 +02:00
return this._settings.prefix
|| this.client._options.bot.prefix;
}
get premium() { //GUILD SETTINGS MUST BE FETCHED
return this._settings.premium;
}
2020-04-08 18:08:46 +02:00
}
return ExtendedGuild;
});
module.exports = Guild;