punishments.js 5.6 KB

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