punishments.js 8.6 KB

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