Index moderation points by guild. Should've been done a while ago...

This commit is contained in:
nolan 2021-06-21 00:48:11 -07:00
parent 55095866de
commit e49e683b49
3 changed files with 27 additions and 17 deletions

View File

@ -50,7 +50,7 @@ module.exports = class AutoModeration extends Observer {
async _moderate(action, guild, channel, member, reason, filterResult) { async _moderate(action, guild, channel, member, reason, filterResult) {
const InfractionClass = CONSTANTS.Infractions[action.type]; const InfractionClass = CONSTANTS.Infractions[action.type];
const result = await this.client.moderationManager._handleTarget(InfractionClass, member, { return this.client.moderationManager._handleTarget(InfractionClass, member, {
guild, guild,
channel, channel,
executor: guild.me, executor: guild.me,
@ -58,7 +58,7 @@ module.exports = class AutoModeration extends Observer {
duration: action.duration, duration: action.duration,
points: action.points, points: action.points,
expiration: action.expiration, expiration: action.expiration,
silent: true, silent: true, //Won't DM Users.
force: false, force: false,
data: { data: {
automoderation: filterResult automoderation: filterResult

View File

@ -11,10 +11,7 @@ const User = Structures.extend('User', (User) => {
this._settings = null; //internal cache of current users' settings; should ALWAYS stay the same as database. this._settings = null; //internal cache of current users' settings; should ALWAYS stay the same as database.
this._cached = Date.now(); this._cached = Date.now();
this._points = { this._points = {};
expirations: [],
points: null
};
} }
@ -27,35 +24,48 @@ const User = Structures.extend('User', (User) => {
} }
async totalPoints(guild, point) { // point = { points: x, expiration: x, timestamp: x} async totalPoints(guild, point) { // point = { points: x, expiration: x, timestamp: x}
if(this._points.points === null) { 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( const find = await this.client.storageManager.mongodb.infractions.find(
{ guild: guild.id, target: this.id, points: { $gt: 0 } }, { 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" } } { projection: { points: "$points", expiration: "$expiration", timestamp: "$timestamp" } }
); );
this._points.points = 0; index.points = 0;
if(find && find.length > 0) { if(find && find.length > 0) {
for(const { points, expiration, timestamp } of find) { for(const { points, expiration, timestamp } of find) {
if(expiration > 0) { if(expiration > 0) {
this._points.expirations.push({ points, expiration: expiration*1000+timestamp }); index.expirations.push({ points, expiration: expiration*1000+timestamp });
} else { } else {
this._points.points += points; index.points += points;
} }
} }
} }
} }
if(point) { if(point) {
if(point.expiration > 0) { if(point.expiration > 0) {
this._points.expirations.push({ points: point.points, expiration: point.expiration*1000+point.timestamp }); index.expirations.push({ points: point.points, expiration: point.expiration*1000+point.timestamp });
} else { } else {
this._points.points += point.points; index.points += point.points;
} }
} }
let points = this._points.expirations.map((e) => { //eslint-disable-line array-callback-return, consistent-return
let expirationPoints = index.expirations.map((e) => {
if(e.expiration >= Date.now()) return e.points; if(e.expiration >= Date.now()) return e.points;
return 0; return 0;
}); });
if(points.length === 0) points = [ 0 ];
return points.reduce((p, v) => p+v) + this._points.points; if(expirationPoints.length === 0) expirationPoints = [0];
return expirationPoints.reduce((p, v) => p+v) + index.points;
} }
/* Settings Wrapper */ /* Settings Wrapper */

View File

@ -48,8 +48,8 @@ class Infraction {
this.reason = data.reason || 'N/A'; this.reason = data.reason || 'N/A';
this.points = data.points || 0; this.points = data.points || 0;
this.expiration = isNaN(data.expiration) ? null : Date.now() + data.expiration * 1000;
this.totalPoints = 0; this.totalPoints = 0;
this.expiration = data.expiration || 0;
this.data = data.data || {}; //Miscellaneous data that may need to be saved for future use. this.data = data.data || {}; //Miscellaneous data that may need to be saved for future use.
this.flags = data.arguments ? Object.keys(data.arguments) : []; this.flags = data.arguments ? Object.keys(data.arguments) : [];