♻️ shopRoles job
This commit is contained in:
parent
b1f04d8103
commit
3a24cc1d72
2 changed files with 65 additions and 58 deletions
|
@ -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!");
|
||||
};
|
||||
|
|
58
src/schedules/jobs/shopRoles.ts
Normal file
58
src/schedules/jobs/shopRoles.ts
Normal file
|
@ -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();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
Loading…
Add table
Reference in a new issue