punishments.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. 'use strict';
  2. const coreClass = require("../core");
  3. const async = require('async');
  4. const mongoose = require('mongoose');
  5. module.exports = class extends coreClass {
  6. constructor(name, moduleManager) {
  7. super(name, moduleManager);
  8. this.dependsOn = ["cache", "db", "utils"];
  9. }
  10. initialize() {
  11. return new Promise((resolve, reject) => {
  12. this.setStage(1);
  13. this.cache = this.moduleManager.modules['cache'];
  14. this.db = this.moduleManager.modules['db'];
  15. this.io = this.moduleManager.modules['io'];
  16. this.utils = this.moduleManager.modules['utils'];
  17. async.waterfall([
  18. (next) => {
  19. this.setStage(2);
  20. this.cache.hgetall('punishments', next);
  21. },
  22. (punishments, next) => {
  23. this.setStage(3);
  24. if (!punishments) return next();
  25. let punishmentIds = Object.keys(punishments);
  26. async.each(punishmentIds, (punishmentId, next) => {
  27. this.db.models.punishment.findOne({_id: punishmentId}, (err, punishment) => {
  28. if (err) next(err);
  29. else if (!punishment) this.cache.hdel('punishments', punishmentId, next);
  30. else next();
  31. });
  32. }, next);
  33. },
  34. (next) => {
  35. this.setStage(4);
  36. this.db.models.punishment.find({}, next);
  37. },
  38. (punishments, next) => {
  39. this.setStage(5);
  40. async.each(punishments, (punishment, next) => {
  41. if (punishment.active === false || punishment.expiresAt < Date.now()) return next();
  42. this.cache.hset('punishments', punishment._id, this.cache.schemas.punishment(punishment, punishment._id), next);
  43. }, next);
  44. }
  45. ], async (err) => {
  46. if (err) {
  47. err = await utils.getError(err);
  48. reject(err);
  49. } else {
  50. resolve();
  51. }
  52. });
  53. });
  54. }
  55. /**
  56. * Gets all punishments in the cache that are active, and removes those that have expired
  57. *
  58. * @param {Function} cb - gets called once we're done initializing
  59. */
  60. async getPunishments(cb) {
  61. try { await this._validateHook(); } catch { return; }
  62. let punishmentsToRemove = [];
  63. async.waterfall([
  64. (next) => {
  65. this.cache.hgetall('punishments', next);
  66. },
  67. (punishmentsObj, next) => {
  68. let punishments = [];
  69. for (let id in punishmentsObj) {
  70. let obj = punishmentsObj[id];
  71. obj.punishmentId = id;
  72. punishments.push(obj);
  73. }
  74. punishments = punishments.filter(punishment => {
  75. if (punishment.expiresAt < Date.now()) punishmentsToRemove.push(punishment);
  76. return punishment.expiresAt > Date.now();
  77. });
  78. next(null, punishments);
  79. },
  80. (punishments, next) => {
  81. async.each(
  82. punishmentsToRemove,
  83. (punishment, next2) => {
  84. this.cache.hdel('punishments', punishment.punishmentId, () => {
  85. next2();
  86. });
  87. },
  88. () => {
  89. next(null, punishments);
  90. }
  91. );
  92. }
  93. ], (err, punishments) => {
  94. if (err && err !== true) return cb(err);
  95. cb(null, punishments);
  96. });
  97. }
  98. /**
  99. * Gets a punishment by id
  100. *
  101. * @param {String} id - the id of the punishment we are trying to get
  102. * @param {Function} cb - gets called once we're done initializing
  103. */
  104. async getPunishment(id, cb) {
  105. try { await this._validateHook(); } catch { return; }
  106. async.waterfall([
  107. (next) => {
  108. if (!mongoose.Types.ObjectId.isValid(id)) return next('Id is not a valid ObjectId.');
  109. this.cache.hget('punishments', id, next);
  110. },
  111. (punishment, next) => {
  112. if (punishment) return next(true, punishment);
  113. this.db.models.punishment.findOne({_id: id}, next);
  114. },
  115. (punishment, next) => {
  116. if (punishment) {
  117. this.cache.hset('punishments', id, punishment, next);
  118. } else next('Punishment not found.');
  119. },
  120. ], (err, punishment) => {
  121. if (err && err !== true) return cb(err);
  122. cb(null, punishment);
  123. });
  124. }
  125. /**
  126. * Gets all punishments from a userId
  127. *
  128. * @param {String} userId - the userId of the punishment(s) we are trying to get
  129. * @param {Function} cb - gets called once we're done initializing
  130. */
  131. async getPunishmentsFromUserId(userId, cb) {
  132. try { await this._validateHook(); } catch { return; }
  133. async.waterfall([
  134. (next) => {
  135. this.getPunishments(next);
  136. },
  137. (punishments, next) => {
  138. punishments = punishments.filter((punishment) => {
  139. return punishment.type === 'banUserId' && punishment.value === userId;
  140. });
  141. next(null, punishments);
  142. }
  143. ], (err, punishments) => {
  144. if (err && err !== true) return cb(err);
  145. cb(null, punishments);
  146. });
  147. }
  148. async addPunishment(type, value, reason, expiresAt, punishedBy, cb) {
  149. try { await this._validateHook(); } catch { return; }
  150. async.waterfall([
  151. (next) => {
  152. const punishment = new this.db.models.punishment({
  153. type,
  154. value,
  155. reason,
  156. active: true,
  157. expiresAt,
  158. punishedAt: Date.now(),
  159. punishedBy
  160. });
  161. punishment.save((err, punishment) => {
  162. if (err) return next(err);
  163. next(null, punishment);
  164. });
  165. },
  166. (punishment, next) => {
  167. this.cache.hset('punishments', punishment._id, this.cache.schemas.punishment(punishment, punishment._id), (err) => {
  168. next(err, punishment);
  169. });
  170. },
  171. (punishment, next) => {
  172. // DISCORD MESSAGE
  173. next(null, punishment);
  174. }
  175. ], (err, punishment) => {
  176. cb(err, punishment);
  177. });
  178. }
  179. async removePunishmentFromCache(punishmentId, cb) {
  180. try { await this._validateHook(); } catch { return; }
  181. async.waterfall([
  182. (next) => {
  183. const punishment = new this.db.models.punishment({
  184. type,
  185. value,
  186. reason,
  187. active: true,
  188. expiresAt,
  189. punishedAt: Date.now(),
  190. punishedBy
  191. });
  192. punishment.save((err, punishment) => {
  193. console.log(err);
  194. if (err) return next(err);
  195. next(null, punishment);
  196. });
  197. },
  198. (punishment, next) => {
  199. this.cache.hset('punishments', punishment._id, punishment, next);
  200. },
  201. (punishment, next) => {
  202. // DISCORD MESSAGE
  203. next();
  204. }
  205. ], (err) => {
  206. cb(err);
  207. });
  208. }
  209. }