galactic-bot/structure/extensions/User.js

60 lines
1.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-06-04 19:59:09 +02:00
this._totalPoints = null;
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-04 19:59:09 +02:00
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;
}
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
}
2020-04-08 18:08:46 +02:00
}
return ExtendedUser;
});
module.exports = User;