🐛 53-not-charging-users-for-custom-roles
This commit is contained in:
parent
566b2997bb
commit
173aae2faa
1 changed files with 17 additions and 21 deletions
|
@ -1,47 +1,43 @@
|
|||
const schedule = require('node-schedule');
|
||||
const { shopRoles, credits, guilds } = require('../helpers/database/models');
|
||||
const { shopRoles, users, guilds } = require('../helpers/database/models');
|
||||
const logger = require('./logger');
|
||||
|
||||
module.exports = async (client) => {
|
||||
// schedule.scheduleJob('*/1 * * *', function () {
|
||||
// console.log('The answer to life, the universe, and everything!');
|
||||
// });
|
||||
|
||||
schedule.scheduleJob('*/30 * * * *', async () => {
|
||||
shopRoles.find().then(async (data) => {
|
||||
data.map(async (role) => {
|
||||
const payed = new Date(role.lastPayed);
|
||||
shopRoles.find().then(async (shopRoles) => {
|
||||
shopRoles.map(async (shopRole) => {
|
||||
const payed = new Date(shopRole.lastPayed);
|
||||
|
||||
oneHourAfterPayed = payed.setHours(payed.getHours() + 1);
|
||||
|
||||
if (new Date() > oneHourAfterPayed) {
|
||||
// Get guild object
|
||||
const guild = await guilds.findOne({
|
||||
guildId: role.guildId,
|
||||
guildId: shopRole.guildId,
|
||||
});
|
||||
|
||||
const userObject = await credits.findOne({
|
||||
userId: role.userId,
|
||||
guildId: role.guildId,
|
||||
const userDB = await users.findOne({
|
||||
userId: shopRole.userId,
|
||||
guildId: shopRole.guildId,
|
||||
});
|
||||
const { pricePerHour } = guild.shop.roles;
|
||||
|
||||
if (userObject.balance < pricePerHour) {
|
||||
const rGuild = await client.guilds.cache.get(`${role.guildId}`);
|
||||
const rMember = await rGuild.members.fetch(`${role.userId}`);
|
||||
if (userDB.credits < pricePerHour) {
|
||||
const rGuild = await client.guilds.cache.get(`${shopRole.guildId}`);
|
||||
const rMember = await rGuild.members.fetch(`${shopRole.userId}`);
|
||||
|
||||
await rMember.roles
|
||||
.remove(`${role.roleId}`)
|
||||
.remove(`${shopRole.roleId}`)
|
||||
.then(console.log)
|
||||
.catch(console.error); // Removes all roles
|
||||
}
|
||||
|
||||
role.lastPayed = new Date();
|
||||
role.save();
|
||||
userObject.balance -= pricePerHour;
|
||||
userObject.save();
|
||||
shopRole.lastPayed = new Date();
|
||||
shopRole.save();
|
||||
userDB.credits -= pricePerHour;
|
||||
userDB.save();
|
||||
await logger.debug(
|
||||
`${role.roleId} was payed one hour later. BEFORE: ${payed} AFTER: ${oneHourAfterPayed} UPDATED: ${role.updatedAt} CREATED: ${role.createdAt}`
|
||||
`${shopRole.roleId} was payed one hour later. BEFORE: ${payed} AFTER: ${oneHourAfterPayed} UPDATED: ${shopRole.updatedAt} CREATED: ${shopRole.createdAt}`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
Loading…
Add table
Reference in a new issue