punishments.js 5.1 KB

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