This commit is contained in:
Erik 2021-06-21 12:00:40 +03:00
commit 19e5333ad2
6 changed files with 56 additions and 18 deletions

View File

@ -107,7 +107,8 @@ class AuditLogObserver extends Observer {
}
async _fetchFirstEntry(guild, user, type) {
const audit = await guild.fetchAuditLogs({ limit: 2 }); //Just incase >_>
if(!guild.me.hasPermission('VIEW_AUDIT_LOG')) return null;
const audit = await guild.fetchAuditLogs({ limit: 1 });
if(audit.entries.size === 0) return null;
const entry = audit.entries.filter((e) => e?.target?.id === user.id).first();

View File

@ -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

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._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 */

View File

@ -364,6 +364,7 @@ class ModerationManager {
const executor = guild.members.resolve(i.executor) || guild.me;
await new undoClass(this.client, {
type: undoClass.type,
reason: `AUTO-${Constant.Opposites[i.type]} from Case ${i.case}`,
channel: guild.channels.resolve(i.channel),
hyperlink: i.modLogMessage && i.modLogChannel ? `https://discord.com/channels/${i.guild}/${i.modLogChannel}/${i.modLogMessage}` : null,

View File

@ -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) : [];

View File

@ -1,3 +1,5 @@
const { ObjectId } = require('mongodb');
class MongodbTable {
constructor(client, provider, opts = {}) {
@ -12,6 +14,7 @@ class MongodbTable {
//Data Search
find(query, opts = {}) { //opts: { projection: ... }
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.find(query, opts, async (error, cursor) => {
@ -22,6 +25,7 @@ class MongodbTable {
}
findOne(query, opts = {}) { //opts: { projection: ..., sort: ... }
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.findOne(query, opts, async (error, item) => {
@ -32,6 +36,7 @@ class MongodbTable {
}
aggregate(query) {
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.aggregate(query, (error, item) => {
@ -42,6 +47,7 @@ class MongodbTable {
}
random(query, amount = 1) {
query = this._handleData(query);
if(amount > 100) amount = 100;
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
@ -55,6 +61,7 @@ class MongodbTable {
//Data Manipulation
insertOne(data) {
data = this._handleData(data);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.insertOne(data, (error, result) => {
@ -67,6 +74,7 @@ class MongodbTable {
//NOTE: insertMany?
deleteOne(query) {
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.deleteOne(query, (error, result) => {
@ -77,6 +85,7 @@ class MongodbTable {
}
deleteMany(query) {
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.deleteMany(query, (error, result) => {
@ -87,6 +96,7 @@ class MongodbTable {
}
updateOne(query, data, upsert = true) {
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.updateOne(query, { $set: data }, { upsert }, async(error, result) => {
@ -99,6 +109,7 @@ class MongodbTable {
}
removeProperty(query, data) {
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
@ -114,6 +125,7 @@ class MongodbTable {
}
push(query, data, upsert = true) {
query = this._handleData(query);
return new Promise((resolve, reject) => {
if(!this.provider._initialized) return reject(new Error('MongoDB is not connected.'));
this.collection.updateOne(query, { $push: data }, { upsert }, async(error, result) => {
@ -153,6 +165,20 @@ class MongodbTable {
});
}
//Lazy Function
_handleData(data) { //Convert data._id to Mongo ObjectIds (gets converted to plaintext through shard communication)
if(data._id) {
if(typeof data._id === 'string') data._id = ObjectId(data._id);
else if(typeof data._id === 'object') data._id = {
$in: Object.values(data._id)[0].map((id) => {
return ObjectId(id);
})
};
}
return data;
}
//Getters
get collection() {