galactic-bot/structure/extensions/User.js

80 lines
2.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.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;
}
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.transactionHandler.send({
2020-06-04 19:59:09 +02:00
provider: 'mongodb',
request: {
collection: 'infractions',
2020-06-16 00:15:13 +02:00
type: 'find',
query: { guild: guild.id, target: this.id, points: { $gt: 0 } }, //maybe filter out expirations that are already expired? not sure how to do that.
projection: { points: "$points", expiration: "$expiration", timestamp: "$timestamp" }
2020-06-04 19:59:09 +02:00
}
});
2020-06-16 00:15:13 +02:00
this._points.points = 0;
for(const { points, expiration, timestamp } of find) {
if(expiration > 0) {
this._points.expirations.push({ points, expiration: expiration*1000+timestamp });
2020-06-16 00:15:13 +02:00
} else {
this._points.points += points;
}
}
}
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
}
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;