🧑‍💻 shopRoles job splitted

This commit is contained in:
Axel Olausson Holtenäs 2022-06-11 19:45:07 +02:00
parent e94b48a6d2
commit f70bbc9171
No known key found for this signature in database
GPG key ID: 7BF6826B76382CBA
5 changed files with 139 additions and 114 deletions

12
src/jobs/shop/index.ts Normal file
View file

@ -0,0 +1,12 @@
// Dependencies
import { Client } from "discord.js";
import * as roles from "./modules/roles";
export const options = {
schedule: "*/5 * * * * *", // https://crontab.guru/
};
export const execute = async (client: Client) => {
await roles.execute(client);
};

View file

@ -0,0 +1,10 @@
import { Client } from "discord.js";
import logger from "../../../../../logger";
import { IShopRole } from "../../../../../interfaces/ShopRole";
export const execute = async (_client: Client, role: IShopRole) => {
const { roleId } = role;
logger.silly(`Shop role ${roleId} is not due for payment.`);
};

View file

@ -0,0 +1,85 @@
import { Client } from "discord.js";
import logger from "../../../../../logger";
import { IShopRole } from "../../../../../interfaces/ShopRole";
import guildSchema from "../../../../../models/guild";
import userSchema from "../../../../../models/user";
import shopRoleSchema from "../../../../../models/shopRole";
export const execute = async (client: Client, role: IShopRole) => {
const { guildId, userId, roleId } = role;
if (!userId) throw new Error("User ID not found for shop role.");
const guildData = await guildSchema.findOne({ guildId });
if (!guildData) throw new Error("Guild not found.");
const userData = await userSchema.findOne({ guildId, userId });
if (!userData) throw new Error("User not found.");
const rGuild = client.guilds.cache.get(guildId);
if (!rGuild) throw new Error("Guild not found.");
const rMember = await rGuild.members.fetch(userId);
if (!rMember) throw new Error("Member not found.");
const rRole = rMember.roles.cache.get(roleId);
if (!rRole) throw new Error("Role not found.");
logger.debug(`Shop role ${roleId} is due for payment.`);
const { pricePerHour } = guildData.shop.roles;
if (userData.credits < pricePerHour) {
await rMember.roles
.remove(roleId)
.then(async () => {
await shopRoleSchema
.deleteOne({
userId,
roleId,
guildId,
})
.then(async () => {
logger.silly(
`Shop role document ${roleId} has been deleted from user ${userId}.`
);
})
.catch(async (err) => {
throw new Error(
`Error deleting shop role document ${roleId} from user ${userId}.`,
err
);
});
})
.catch(async (err) => {
throw new Error(
`Error removing role ${roleId} from user ${userId}.`,
err
);
});
throw new Error("User does not have enough credits.");
}
userData.credits -= pricePerHour;
await userData
.save()
.then(async () => {
logger.silly(`User ${userId} has been updated.`);
role.lastPayed = new Date();
await role
.save()
.then(async () => {
logger.silly(`Shop role ${roleId} has been updated.`);
})
.catch(async (err) => {
throw new Error(`Error updating shop role ${roleId}.`, err);
});
logger.debug(`Shop role ${roleId} has been paid.`);
})
.catch(async (err) => {
throw new Error(`Error updating user ${userId}.`, err);
});
};

View file

@ -0,0 +1,32 @@
import { Client } from "discord.js";
import { IShopRole } from "../../../../interfaces/ShopRole";
import shopRoleSchema from "../../../../models/shopRole";
import * as overDueForPayment from "./components/overDueForPayment";
import * as dueForPayment from "./components/dueForPayment";
export const execute = async (client: Client) => {
const roles = await shopRoleSchema.find();
await Promise.all(
roles.map(async (role: IShopRole) => {
const { lastPayed } = role;
const nextPayment = new Date(
lastPayed.setHours(lastPayed.getHours() + 1)
);
const now = new Date();
if (nextPayment > now) {
await dueForPayment.execute(client, role);
return;
}
if (nextPayment < now) {
await overDueForPayment.execute(client, role);
}
})
);
};

View file

@ -1,114 +0,0 @@
// Dependencies
import { Client } from "discord.js";
import logger from "../logger";
// Schemas
import userSchema from "../models/user";
import shopRoleSchema from "../models/shopRole";
import guildSchema from "../models/guild";
export const options = {
schedule: "*/5 * * * *", // https://crontab.guru/
};
export const execute = async (client: Client) => {
const roles = await shopRoleSchema.find();
await Promise.all(
roles.map(async (role) => {
const { guildId, userId, roleId } = role;
const lastPayment = new Date(role.lastPayed);
const nextPayment = new Date(
lastPayment.setHours(lastPayment.getHours() + 1)
);
if (new Date() < nextPayment) {
logger.silly(`Shop role ${roleId} is not due for payment.`);
}
const guildData = await guildSchema.findOne({ guildId });
if (!guildData) {
logger.error(`Guild ${guildId} not found.`);
return;
}
if (!userId) {
logger.error(`User ID not found for shop role ${roleId}.`);
return;
}
const userData = await userSchema.findOne({ guildId, userId });
if (!userData) {
logger.error(`User ${userId} not found for shop role ${roleId}.`);
return;
}
const rGuild = client?.guilds?.cache?.get(guildId);
const rMember = await rGuild?.members?.fetch(userId);
if (!rMember) {
logger.error(`Member ${userId} not found for shop role ${roleId}.`);
return;
}
const rRole = rMember.roles.cache.get(roleId);
if (!rMember || !rRole) {
logger.error(`Member ${userId} not found for shop role ${roleId}.`);
await shopRoleSchema
.deleteOne({
userId,
roleId,
guildId,
})
.then(async () => {
logger.silly(
`Shop role document ${roleId} has been deleted from user ${userId}.`
);
})
.catch(async (error) => {
logger.error(
`Error deleting shop role document ${roleId} from user ${userId}.`,
error
);
});
return;
}
if (new Date() > nextPayment) {
logger.silly(
`Shop role ${roleId} is due for payment. Withdrawing credits from user ${userId}.`
);
const { pricePerHour } = guildData.shop.roles;
if (userData.credits < pricePerHour) {
logger.error(
`User ${userId} does not have enough credits to pay for shop role ${roleId}.`
);
if (!rMember) {
logger.error(`Member ${userId} not found for shop role ${roleId}.`);
return;
}
rMember.roles.remove(roleId);
return;
}
userData.credits -= pricePerHour;
await userData
.save()
.then(async () => {
role.lastPayed = new Date();
await role
.save()
.then(async () => {
logger.silly(`Shop role ${roleId} has been paid for.`);
})
.catch(async (err) => {
logger.error(
`Error saving shop role ${roleId} last payed date.`,
err
);
});
logger.silly(
`Shop role ${roleId} has been paid for. Keeping role ${roleId} for user ${userId}.`
);
})
.catch(async (err) => {
logger.error(
`Error saving user ${userId} credits for shop role ${roleId}.`,
err
);
});
}
})
);
};