galactic-bot/structure/extensions/User.js

119 lines
3.9 KiB
JavaScript
Raw Normal View History

2020-04-08 18:08:46 +02:00
const { Structures } = require('discord.js');
const User = Structures.extend('User', (User) => {
class ExtendedUser extends User {
constructor(...args) {
super(...args);
2020-04-08 18:08:46 +02:00
this._settings = null; //internal cache of current users' settings; should ALWAYS stay the same as database.
this._cached = Date.now();
2020-04-08 18:08:46 +02:00
2020-06-16 00:15:13 +02:00
this._points = {
expirations: [],
points: null
};
2020-04-08 18:08:46 +02:00
}
2020-06-04 19:59:09 +02:00
async settings() {
if (!this._settings) this._settings = this.client.storageManager.mongodb.users.findOne({ userId: this.id });
2020-06-04 19:59:09 +02:00
if (this._settings instanceof Promise) this._settings = await this._settings || {};
if(!this._settings) this._settings = { userId: this.id, ...this.defaultConfig };
else this._settings = { ...this.defaultConfig, ...this._settings };
2020-06-04 19:59:09 +02:00
return this._settings;
}
2020-06-16 00:15:13 +02:00
async totalPoints(guild, point) { // point = { points: x, expiration: x, timestamp: x}
if(this._points.points === null) {
const find = await this.client.storageManager.mongodb.infractions.find(
{ guild: guild.id, target: this.id, points: { $gt: 0 } },
{ projection: { points: "$points", expiration: "$expiration", timestamp: "$timestamp" } }
);
2020-06-16 00:15:13 +02:00
this._points.points = 0;
2020-07-16 09:54:39 +02:00
if(find && find.length > 0) {
for(const { points, expiration, timestamp } of find) {
if(expiration > 0) {
this._points.expirations.push({ points, expiration: expiration*1000+timestamp });
} else {
this._points.points += points;
}
2020-06-16 00:15:13 +02:00
}
}
}
if(point) {
if(point.expiration > 0) {
this._points.expirations.push({ points: point.points, expiration: point.expiration*1000+point.timestamp });
2020-06-16 00:15:13 +02:00
} else {
this._points.points += point.points;
}
2020-06-04 19:59:09 +02:00
}
2020-06-16 00:15:13 +02:00
let points = this._points.expirations.map((e) => { //eslint-disable-line array-callback-return, consistent-return
if(e.expiration >= Date.now()) return e.points;
return 0;
});
if(points.length === 0) points = [ 0 ];
return points.reduce((p, v) => p+v) + this._points.points;
2020-06-04 19:59:09 +02:00
}
/* Settings Wrapper */
async _updateSettings(data) { //Update property (upsert true) - updateOne
if(!this._settings) await this.settings();
try {
await this.client.storageManager.mongodb.users.updateOne(
{ guildId: this.id },
data
);
this._settings = {
...this._settings,
...data
};
this._storageLog(`Database Update (guild:${this.id}).`);
} catch(error) {
this._storageError(error);
}
return true;
}
/* Logging */
_storageLog(log) {
this.client.logger.debug(log);
}
_storageError(error) {
this.client.logger.error(`Database Error (user:${this.id}) : \n${error.stack || error}`);
}
/* Lazy Getters */
get defaultConfig() {
return JSON.parse(JSON.stringify(this.client.defaultConfig('USER')));
}
2020-05-24 23:44:07 +02:00
get developer() {
return this.client._options.bot.owners.includes(this.id);
}
get timeSinceCached() {
return Date.now()-this._cached;
}
get prefix() {
return this.client._options.bot.prefix;
2020-04-19 21:54:57 +02:00
}
get display() {
return this.tag;
}
2020-04-08 18:08:46 +02:00
}
return ExtendedUser;
});
module.exports = User;