galactic-bot/structure/extensions/User.js

133 lines
4.2 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
this._points = {};
2020-06-16 00:15:13 +02:00
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}
let index = this._points[guild.id];
if(!index) {
this._points[guild.id] = {
expirations: [],
points: null
};
index = this._points[guild.id];
}
if(index.points === null) {
const find = await this.client.storageManager.mongodb.infractions.find(
{ guild: guild.id, target: this.id, resolved: false, points: { $gt: 0 }, $or: [ { expiration: 0 }, { expiration: { $gte: Date.now() } }] },
{ projection: { points: "$points", expiration: "$expiration", timestamp: "$timestamp" } }
);
index.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) {
index.expirations.push({ points, expiration: expiration*1000+timestamp });
2020-07-16 09:54:39 +02:00
} else {
index.points += points;
2020-07-16 09:54:39 +02:00
}
2020-06-16 00:15:13 +02:00
}
}
}
2020-06-16 00:15:13 +02:00
if(point) {
if(point.expiration > 0) {
index.expirations.push({ points: point.points, expiration: point.expiration*1000+point.timestamp });
2020-06-16 00:15:13 +02:00
} else {
index.points += point.points;
2020-06-16 00:15:13 +02:00
}
2020-06-04 19:59:09 +02:00
}
let expirationPoints = index.expirations.map((e) => {
2020-06-16 00:15:13 +02:00
if(e.expiration >= Date.now()) return e.points;
return 0;
});
if(expirationPoints.length === 0) expirationPoints = [0];
return expirationPoints.reduce((p, v) => p+v) + index.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;
}
get mention() {
return `<@${this.id}>`;
}
2020-04-08 18:08:46 +02:00
}
return ExtendedUser;
});
module.exports = User;