galactic-bot/structure/extensions/User.js
2020-06-04 12:59:09 -05:00

60 lines
1.9 KiB
JavaScript

const { Structures } = require('discord.js');
const User = Structures.extend('User', (User) => {
class ExtendedUser extends User {
constructor(...args) {
super(...args);
this._totalPoints = null;
this._settings = null; //internal cache of current users' settings; should ALWAYS stay the same as database.
this._cached = Date.now();
}
async settings() {
if (!this._settings) this._settings = this.client.transactionHandler.send({ provider: 'mongodb', request: { collection: 'users', type: 'findOne', query: { user: this.id } } });
if (this._settings instanceof Promise) this._settings = await this._settings || {};
return this._settings;
}
async totalPoints(guild, points) {
if(this._totalPoints === null) {
const query = [{ $match: { $or: [{ expiration: { $gt: Math.floor(Date.now()/1000) } }, { expiration: 0 }], guild: guild.id, target: this.id } },
{ $group: { _id: null, pointSum: { $sum: "$points" } } }];
const aggregate = await this.client.transactionHandler.send({
provider: 'mongodb',
request: {
collection: 'infractions',
type: 'aggregate',
query
}
});
this._totalPoints = aggregate[0].pointSum;
}
this._totalPoints += points;
return this._totalPoints;
}
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;
}
}
return ExtendedUser;
});
module.exports = User;