From 3a24cc1d723de3478aa5959b1d04cb81761e94f0 Mon Sep 17 00:00:00 2001 From: Vermium Sifell Date: Wed, 13 Apr 2022 02:43:07 +0200 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20shopRoles=20job?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/schedules/index.ts | 65 ++++----------------------------- src/schedules/jobs/shopRoles.ts | 58 +++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 58 deletions(-) create mode 100644 src/schedules/jobs/shopRoles.ts diff --git a/src/schedules/index.ts b/src/schedules/index.ts index 69fbe7a..daa65c3 100644 --- a/src/schedules/index.ts +++ b/src/schedules/index.ts @@ -1,66 +1,15 @@ import schedule from "node-schedule"; -import users from "../../database/schemas/user"; -import shopRolesSchema from "../../database/schemas/shopRole"; -import guilds from "../../database/schemas/guild"; + import logger from "../logger"; import { Client } from "discord.js"; +import shopRoles from "./jobs/shopRoles"; export default async (client: Client) => { - schedule.scheduleJob("*/5 * * * *", async () => { - shopRolesSchema.find().then(async (shopRoles: any) => { - shopRoles.map(async (shopRole: any) => { - const payed = new Date(shopRole.lastPayed); + const expression = "*/5 * * * *"; - const oneHourAfterPayed = payed.setHours(payed.getHours() + 1); - - if (new Date() > new Date(oneHourAfterPayed)) { - logger.debug( - `Role: ${shopRole.roleId} Expires: ${ - new Date() < new Date(oneHourAfterPayed) - } Last Payed: ${shopRole.lastPayed}` - ); - - // Get guild object - const guild = await guilds.findOne({ - guildId: shopRole.guildId, - }); - - if (guild === null) return; - const userDB = await users.findOne({ - userId: shopRole.userId, - guildId: shopRole.guildId, - }); - const { pricePerHour } = guild.shop.roles; - - if (userDB === null) return; - - if (userDB.credits < pricePerHour) { - const rGuild = client?.guilds?.cache?.get(`${shopRole.guildId}`); - const rMember = await rGuild?.members?.fetch(`${shopRole.userId}`); - - shopRolesSchema - .deleteOne({ _id: shopRole._id }) - .then(async () => - logger.debug(`Removed ${shopRole._id} from shopRoles`) - ); - - return await rMember?.roles - .remove(`${shopRole.roleId}`) - .then(async (test) => console.log("4", test)) - .catch(async (test) => console.log("5", test)); // Removes all roles - } - - shopRole.lastPayed = new Date(); - shopRole.save(); - userDB.credits -= pricePerHour; - userDB.save(); - await logger.debug( - `${shopRole.roleId} was payed one hour later. BEFORE: ${payed} AFTER: ${oneHourAfterPayed} UPDATED: ${shopRole.updatedAt} CREATED: ${shopRole.createdAt}` - ); - } - }); - }); - - await logger.debug("Checking schedules! (Every 5 minutes)"); + schedule.scheduleJob(expression, async () => { + logger.verbose(`Checking schedules! (${expression})`); + await shopRoles(client); }); + logger.info("Successfully started schedule engine!"); }; diff --git a/src/schedules/jobs/shopRoles.ts b/src/schedules/jobs/shopRoles.ts new file mode 100644 index 0000000..f96fa00 --- /dev/null +++ b/src/schedules/jobs/shopRoles.ts @@ -0,0 +1,58 @@ +import { Client } from "discord.js"; + +import logger from "../../logger"; + +import users from "../../database/schemas/user"; +import shopRoleSchema from "../../database/schemas/shopRole"; +import guilds from "../../database/schemas/guild"; + +export default async (client: Client) => { + shopRoleSchema.find().then(async (shopRoles: any) => { + shopRoles.map(async (shopRole: any) => { + const payed = new Date(shopRole?.lastPayed); + + const oneHourAfterPayed = payed?.setHours(payed?.getHours() + 1); + + if (new Date() > new Date(oneHourAfterPayed)) { + logger.debug( + `Role: ${shopRole?.roleId} Expires: ${ + new Date() < new Date(oneHourAfterPayed) + } Last Payed: ${shopRole?.lastPayed}` + ); + + // Get guild object + const guild = await guilds?.findOne({ + guildId: shopRole?.guildId, + }); + + if (guild === null) return; + const userDB = await users?.findOne({ + userId: shopRole?.userId, + guildId: shopRole?.guildId, + }); + const { pricePerHour } = guild.shop.roles; + + if (userDB === null) return; + + if (userDB?.credits < pricePerHour) { + const rGuild = client?.guilds?.cache?.get(`${shopRole?.guildId}`); + const rMember = await rGuild?.members?.fetch(`${shopRole?.userId}`); + + shopRoleSchema + ?.deleteOne({ _id: shopRole?._id }) + ?.then(async () => + logger?.verbose(`Removed ${shopRole?._id} from collection.`) + ); + + return rMember?.roles?.remove(`${shopRole?.roleId}`); + } + + shopRole.lastPayed = new Date(); + shopRole?.save()?.then(async () => { + userDB.credits -= pricePerHour; + userDB?.save(); + }); + } + }); + }); +};