punishments.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. const CoreClass = require("../core.js");
  2. const async = require("async");
  3. const mongoose = require("mongoose");
  4. class PunishmentsModule extends CoreClass {
  5. constructor() {
  6. super("punishments");
  7. }
  8. initialize() {
  9. return new Promise(async (resolve, reject) => {
  10. this.setStage(1);
  11. this.cache = this.moduleManager.modules["cache"];
  12. this.db = this.moduleManager.modules["db"];
  13. this.io = this.moduleManager.modules["io"];
  14. this.utils = this.moduleManager.modules["utils"];
  15. const punishmentModel = await this.db.runJob("GET_MODEL", {
  16. modelName: "punishment",
  17. });
  18. const punishmentSchema = await this.cache.runJob("GET_SCHEMA", {
  19. schemaName: "punishment",
  20. });
  21. async.waterfall(
  22. [
  23. (next) => {
  24. this.setStage(2);
  25. this.cache
  26. .runJob("HGETALL", { table: "punishments" })
  27. .then((punishments) => next(null, punishments))
  28. .catch(next);
  29. },
  30. (punishments, next) => {
  31. this.setStage(3);
  32. if (!punishments) return next();
  33. let punishmentIds = Object.keys(punishments);
  34. async.each(
  35. punishmentIds,
  36. (punishmentId, next) => {
  37. punishmentModel.findOne(
  38. { _id: punishmentId },
  39. (err, punishment) => {
  40. if (err) next(err);
  41. else if (!punishment)
  42. this.cache
  43. .runJob("HDEL", {
  44. table: "punishments",
  45. key: punishmentId,
  46. })
  47. .then(() => next())
  48. .catch(next);
  49. else next();
  50. }
  51. );
  52. },
  53. next
  54. );
  55. },
  56. (next) => {
  57. this.setStage(4);
  58. punishmentModel.find({}, next);
  59. },
  60. (punishments, next) => {
  61. this.setStage(5);
  62. async.each(
  63. punishments,
  64. (punishment, next) => {
  65. if (
  66. punishment.active === false ||
  67. punishment.expiresAt < Date.now()
  68. )
  69. return next();
  70. this.cache
  71. .runJob("HSET", {
  72. table: "punishments",
  73. key: punishment._id,
  74. value: punishmentSchema(
  75. punishment,
  76. punishment._id
  77. ),
  78. })
  79. .then(() => next())
  80. .catch(next);
  81. },
  82. next
  83. );
  84. },
  85. ],
  86. async (err) => {
  87. if (err) {
  88. err = await utils.runJob("GET_ERROR", { error: err });
  89. reject(new Error(err));
  90. } else {
  91. resolve();
  92. }
  93. }
  94. );
  95. });
  96. }
  97. /**
  98. * Gets all punishments in the cache that are active, and removes those that have expired
  99. *
  100. * @param {Function} cb - gets called once we're done initializing
  101. */
  102. GET_PUNISHMENTS() {
  103. //cb
  104. return new Promise((resolve, reject) => {
  105. let punishmentsToRemove = [];
  106. async.waterfall(
  107. [
  108. (next) => {
  109. this.cache
  110. .runJob("HGETALL", { table: "punishments" })
  111. .then((punishmentsObj) =>
  112. next(null, punishmentsObj)
  113. )
  114. .catch(next);
  115. },
  116. (punishmentsObj, next) => {
  117. let punishments = [];
  118. for (let id in punishmentsObj) {
  119. let obj = punishmentsObj[id];
  120. obj.punishmentId = id;
  121. punishments.push(obj);
  122. }
  123. punishments = punishments.filter((punishment) => {
  124. if (punishment.expiresAt < Date.now())
  125. punishmentsToRemove.push(punishment);
  126. return punishment.expiresAt > Date.now();
  127. });
  128. next(null, punishments);
  129. },
  130. (punishments, next) => {
  131. async.each(
  132. punishmentsToRemove,
  133. (punishment, next2) => {
  134. this.cache
  135. .runJob("HDEL", {
  136. table: "punishments",
  137. key: punishment.punishmentId,
  138. })
  139. .finally(() => next2());
  140. },
  141. () => {
  142. next(null, punishments);
  143. }
  144. );
  145. },
  146. ],
  147. (err, punishments) => {
  148. if (err && err !== true) return reject(new Error(err));
  149. resolve(punishments);
  150. }
  151. );
  152. });
  153. }
  154. /**
  155. * Gets a punishment by id
  156. *
  157. * @param {String} id - the id of the punishment we are trying to get
  158. * @param {Function} cb - gets called once we're done initializing
  159. */
  160. GET_PUNISHMENT() {
  161. //id, cb
  162. return new Promise(async (resolve, reject) => {
  163. const punishmentModel = await db.runJob("GET_MODEL", {
  164. modelName: "punishment",
  165. });
  166. async.waterfall(
  167. [
  168. (next) => {
  169. if (!mongoose.Types.ObjectId.isValid(payload.id))
  170. return next("Id is not a valid ObjectId.");
  171. this.cache
  172. .runJob("HGET", {
  173. table: "punishments",
  174. key: payload.id,
  175. })
  176. .then((punishment) => next(null, punishment))
  177. .catch(next);
  178. },
  179. (punishment, next) => {
  180. if (punishment) return next(true, punishment);
  181. punishmentModel.findOne({ _id: payload.id }, next);
  182. },
  183. (punishment, next) => {
  184. if (punishment) {
  185. this.cache
  186. .runJob("HSET", {
  187. table: "punishments",
  188. key: payload.id,
  189. value: punishment,
  190. })
  191. .then((punishment) => next(null, punishment))
  192. .catch(next);
  193. } else next("Punishment not found.");
  194. },
  195. ],
  196. (err, punishment) => {
  197. if (err && err !== true) return reject(new Error(err));
  198. resolve(punishment);
  199. }
  200. );
  201. });
  202. }
  203. /**
  204. * Gets all punishments from a userId
  205. *
  206. * @param {String} userId - the userId of the punishment(s) we are trying to get
  207. * @param {Function} cb - gets called once we're done initializing
  208. */
  209. GET_PUNISHMENTS_FROM_USER_ID(payload) {
  210. //userId, cb
  211. return new Promise((resolve, reject) => {
  212. async.waterfall(
  213. [
  214. (next) => {
  215. this.runJob("GET_PUNISHMENTS", {})
  216. .then((punishments) => next(null, punishments))
  217. .catch(next);
  218. },
  219. (punishments, next) => {
  220. punishments = punishments.filter((punishment) => {
  221. return (
  222. punishment.type === "banUserId" &&
  223. punishment.value === payload.userId
  224. );
  225. });
  226. next(null, punishments);
  227. },
  228. ],
  229. (err, punishments) => {
  230. if (err && err !== true) return reject(new Error(err));
  231. resolve(punishments);
  232. }
  233. );
  234. });
  235. }
  236. ADD_PUNISHMENT(payload) {
  237. //type, value, reason, expiresAt, punishedBy, cb
  238. return new Promise(async (resolve, reject) => {
  239. const punishmentModel = await db.runJob("GET_MODEL", {
  240. modelName: "punishment",
  241. });
  242. const punishmentSchema = await cache.runJob("GET_SCHEMA", {
  243. schemaName: "punishment",
  244. });
  245. async.waterfall(
  246. [
  247. (next) => {
  248. const punishment = new punishmentModel({
  249. type: payload.type,
  250. value: payload.value,
  251. reason: payload.reason,
  252. active: true,
  253. expiresAt: payload.expiresAt,
  254. punishedAt: Date.now(),
  255. punishedBy: payload.punishedBy,
  256. });
  257. punishment.save((err, punishment) => {
  258. if (err) return next(err);
  259. next(null, punishment);
  260. });
  261. },
  262. (punishment, next) => {
  263. this.cache
  264. .runJob("HSET", {
  265. table: "punishments",
  266. key: punishment._id,
  267. value: punishmentSchema(
  268. punishment,
  269. punishment._id
  270. ),
  271. })
  272. .then(() => next())
  273. .catch(next);
  274. },
  275. (punishment, next) => {
  276. // DISCORD MESSAGE
  277. next(null, punishment);
  278. },
  279. ],
  280. (err, punishment) => {
  281. if (err) return reject(new Error(err));
  282. resolve(punishment);
  283. }
  284. );
  285. });
  286. }
  287. }
  288. module.exports = new PunishmentsModule();