| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331 | const CoreClass = require("../core.js");const async = require("async");const mongoose = require("mongoose");class PunishmentsModule extends CoreClass {    constructor() {        super("punishments");    }    initialize() {        return new Promise(async (resolve, reject) => {            this.setStage(1);            this.cache = this.moduleManager.modules["cache"];            this.db = this.moduleManager.modules["db"];            this.io = this.moduleManager.modules["io"];            this.utils = this.moduleManager.modules["utils"];            const punishmentModel = await this.db.runJob("GET_MODEL", {                modelName: "punishment",            });            const punishmentSchema = await this.cache.runJob("GET_SCHEMA", {                schemaName: "punishment",            });            async.waterfall(                [                    (next) => {                        this.setStage(2);                        this.cache                            .runJob("HGETALL", { table: "punishments" })                            .then((punishments) => {                                next(null, punishments);                            })                            .catch(next);                    },                    (punishments, next) => {                        this.setStage(3);                        if (!punishments) return next();                        let punishmentIds = Object.keys(punishments);                        async.each(                            punishmentIds,                            (punishmentId, next) => {                                punishmentModel.findOne(                                    { _id: punishmentId },                                    (err, punishment) => {                                        if (err) next(err);                                        else if (!punishment)                                            this.cache                                                .runJob("HDEL", {                                                    table: "punishments",                                                    key: punishmentId,                                                })                                                .then(() => {                                                    next();                                                })                                                .catch(next);                                        else next();                                    }                                );                            },                            next                        );                    },                    (next) => {                        this.setStage(4);                        punishmentModel.find({}, next);                    },                    (punishments, next) => {                        this.setStage(5);                        async.each(                            punishments,                            (punishment, next) => {                                if (                                    punishment.active === false ||                                    punishment.expiresAt < Date.now()                                )                                    return next();                                this.cache                                    .runJob("HSET", {                                        table: "punishments",                                        key: punishment._id,                                        value: punishmentSchema(                                            punishment,                                            punishment._id                                        ),                                    })                                    .then(() => {                                        next();                                    })                                    .catch(next);                            },                            next                        );                    },                ],                async (err) => {                    if (err) {                        err = await utils.runJob("GET_ERROR", { error: err });                        reject(new Error(err));                    } else {                        resolve();                    }                }            );        });    }    /**     * Gets all punishments in the cache that are active, and removes those that have expired     *     * @param {Function} cb - gets called once we're done initializing     */    GET_PUNISHMENTS() {        //cb        return new Promise((resolve, reject) => {            let punishmentsToRemove = [];            async.waterfall(                [                    (next) => {                        this.cache                            .runJob("HGETALL", { table: "punishments" })                            .then((punishmentsObj) =>                                next(null, punishmentsObj)                            )                            .catch(next);                    },                    (punishmentsObj, next) => {                        let punishments = [];                        for (let id in punishmentsObj) {                            let obj = punishmentsObj[id];                            obj.punishmentId = id;                            punishments.push(obj);                        }                        punishments = punishments.filter((punishment) => {                            if (punishment.expiresAt < Date.now())                                punishmentsToRemove.push(punishment);                            return punishment.expiresAt > Date.now();                        });                        next(null, punishments);                    },                    (punishments, next) => {                        async.each(                            punishmentsToRemove,                            (punishment, next2) => {                                this.cache                                    .runJob("HDEL", {                                        table: "punishments",                                        key: punishment.punishmentId,                                    })                                    .finally(() => {                                        next2()                                    });                            },                            () => {                                next(null, punishments);                            }                        );                    },                ],                (err, punishments) => {                    if (err && err !== true) return reject(new Error(err));                    resolve(punishments);                }            );        });    }    /**     * Gets a punishment by id     *     * @param {String} id - the id of the punishment we are trying to get     * @param {Function} cb - gets called once we're done initializing     */    GET_PUNISHMENT() {        //id, cb        return new Promise(async (resolve, reject) => {            const punishmentModel = await db.runJob("GET_MODEL", {                modelName: "punishment",            });            async.waterfall(                [                    (next) => {                        if (!mongoose.Types.ObjectId.isValid(payload.id))                            return next("Id is not a valid ObjectId.");                        this.cache                            .runJob("HGET", {                                table: "punishments",                                key: payload.id,                            })                            .then((punishment) => {                                next(null, punishment);                            })                            .catch(next);                    },                    (punishment, next) => {                        if (punishment) return next(true, punishment);                        punishmentModel.findOne({ _id: payload.id }, next);                    },                    (punishment, next) => {                        if (punishment) {                            this.cache                                .runJob("HSET", {                                    table: "punishments",                                    key: payload.id,                                    value: punishment,                                })                                .then((punishment) => {                                    next(null, punishment);                                })                                .catch(next);                        } else next("Punishment not found.");                    },                ],                (err, punishment) => {                    if (err && err !== true) return reject(new Error(err));                    resolve(punishment);                }            );        });    }    /**     * Gets all punishments from a userId     *     * @param {String} userId - the userId of the punishment(s) we are trying to get     * @param {Function} cb - gets called once we're done initializing     */    GET_PUNISHMENTS_FROM_USER_ID(payload) {        //userId, cb        return new Promise((resolve, reject) => {            async.waterfall(                [                    (next) => {                        this.runJob("GET_PUNISHMENTS", {})                            .then((punishments) => {                                next(null, punishments);                            })                            .catch(next);                    },                    (punishments, next) => {                        punishments = punishments.filter((punishment) => {                            return (                                punishment.type === "banUserId" &&                                punishment.value === payload.userId                            );                        });                        next(null, punishments);                    },                ],                (err, punishments) => {                    if (err && err !== true) return reject(new Error(err));                    resolve(punishments);                }            );        });    }    ADD_PUNISHMENT(payload) {        //type, value, reason, expiresAt, punishedBy, cb        return new Promise(async (resolve, reject) => {            const punishmentModel = await db.runJob("GET_MODEL", {                modelName: "punishment",            });            const punishmentSchema = await cache.runJob("GET_SCHEMA", {                schemaName: "punishment",            });            async.waterfall(                [                    (next) => {                        const punishment = new punishmentModel({                            type: payload.type,                            value: payload.value,                            reason: payload.reason,                            active: true,                            expiresAt: payload.expiresAt,                            punishedAt: Date.now(),                            punishedBy: payload.punishedBy,                        });                        punishment.save((err, punishment) => {                            if (err) return next(err);                            next(null, punishment);                        });                    },                    (punishment, next) => {                        this.cache                            .runJob("HSET", {                                table: "punishments",                                key: punishment._id,                                value: punishmentSchema(                                    punishment,                                    punishment._id                                ),                            })                            .then(() => {                                next();                            })                            .catch(next);                    },                    (punishment, next) => {                        // DISCORD MESSAGE                        next(null, punishment);                    },                ],                (err, punishment) => {                    if (err) return reject(new Error(err));                    resolve(punishment);                }            );        });    }}module.exports = new PunishmentsModule();
 |