punishments.js 12 KB

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