punishments.js 7.8 KB

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