From e49e683b49763fc433471055ec9049f92ffcb603 Mon Sep 17 00:00:00 2001 From: nolan Date: Mon, 21 Jun 2021 00:48:11 -0700 Subject: [PATCH] Index moderation points by guild. Should've been done a while ago... --- .../components/observers/Automoderation.js | 4 +- structure/extensions/User.js | 38 ++++++++++++------- structure/moderation/interfaces/Infraction.js | 2 +- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/structure/client/components/observers/Automoderation.js b/structure/client/components/observers/Automoderation.js index 28d47f0..ba9a8dc 100644 --- a/structure/client/components/observers/Automoderation.js +++ b/structure/client/components/observers/Automoderation.js @@ -50,7 +50,7 @@ module.exports = class AutoModeration extends Observer { async _moderate(action, guild, channel, member, reason, filterResult) { const InfractionClass = CONSTANTS.Infractions[action.type]; - const result = await this.client.moderationManager._handleTarget(InfractionClass, member, { + return this.client.moderationManager._handleTarget(InfractionClass, member, { guild, channel, executor: guild.me, @@ -58,7 +58,7 @@ module.exports = class AutoModeration extends Observer { duration: action.duration, points: action.points, expiration: action.expiration, - silent: true, + silent: true, //Won't DM Users. force: false, data: { automoderation: filterResult diff --git a/structure/extensions/User.js b/structure/extensions/User.js index ff856b8..697836e 100644 --- a/structure/extensions/User.js +++ b/structure/extensions/User.js @@ -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._cached = Date.now(); - this._points = { - expirations: [], - points: null - }; + this._points = {}; } @@ -27,35 +24,48 @@ const User = Structures.extend('User', (User) => { } 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( - { 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" } } ); - this._points.points = 0; + index.points = 0; if(find && find.length > 0) { for(const { points, expiration, timestamp } of find) { if(expiration > 0) { - this._points.expirations.push({ points, expiration: expiration*1000+timestamp }); + index.expirations.push({ points, expiration: expiration*1000+timestamp }); } else { - this._points.points += points; + index.points += points; } } } } + if(point) { 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 { - 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; 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 */ diff --git a/structure/moderation/interfaces/Infraction.js b/structure/moderation/interfaces/Infraction.js index 2372fb5..799b54a 100644 --- a/structure/moderation/interfaces/Infraction.js +++ b/structure/moderation/interfaces/Infraction.js @@ -48,8 +48,8 @@ class Infraction { this.reason = data.reason || 'N/A'; this.points = data.points || 0; + this.expiration = isNaN(data.expiration) ? null : Date.now() + data.expiration * 1000; this.totalPoints = 0; - this.expiration = data.expiration || 0; this.data = data.data || {}; //Miscellaneous data that may need to be saved for future use. this.flags = data.arguments ? Object.keys(data.arguments) : [];