const { EmbedBuilder, } = require('discord.js'); const { Inventory, addInventory } = require('../structures/inventory'); const { Bank, addBank } = require('../structures/bank'); module.exports = { name: "work", debug: false, global: false, description: "work to earn your income", usage: "", permissions: { channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"], member: [], }, options: [], SlashCommand: { /** * * @param {require("../structures/QuarksBot")} client * @param {import("discord.js").Message} message * @param {string[]} args * @param {*} param3 */ run: async (client, interaction, args, { GuildDB }) => { if (GuildDB.customChannelStatus==true&&!GuildDB.allowedChannels.includes(interaction.channel_id)) { return interaction.send({ content: `You are not allowed to use the bot in this channel.` }); } const hasWorkRole = GuildDB.incomeRoles.some(data => { if (interaction.member.roles.includes(data.role)) return true; return false; }); if (!hasWorkRole) { const error = new EmbedBuilder() .setColor(client.config.Colors.Red) .setTitle('Missing Work!') .setDescription(`It appears you don't have any work, apply for some jobs to earn your income.`) return interaction.send({ embeds: [error] }) } let inventory = await client.dbo.collection("inventories").findOne({"inventory.userID":interaction.member.user.id}).then(inventory => inventory); if (!inventory) { // if the inventory previously didn't exists, this is the only information we require inventory = {guilds: {[guillDB.serverID]: {lastWork: new Date('2000-01-01T00:00:00')}}} // Early date ensures they can work // Register inventory for user let newInventory = new Inventory(); newInventory.createInventory(interaction.member.user.id, GuildDB.serverID); newInventory.save().catch(err => { if (err) return client.sendInternalError(err); }); } else inventory = inventory.inventory if (!client.exists(inventory.guilds[GuildDB.serverID])) { const success = addInventory(inventory.guilds, GuildDB.serverID, interaction.member.user.id, client); if (!success) return client.sendInternalError('Failed to add inventory'); } const now = new Date(); const msBetweenDates = Math.abs(inventory.lastWork - now) const hoursBetweenDates = msBetweenDates / (60 * 60 * 1000) if (hoursBetweenDates >= 24) { let workRoles = [] let workPays = [] for (let i = 0; i < GuildDB.incomeRoles.length; i++) { if (interaction.member.roles.includes(GuildDB.incomeRoles[i].role)) { workRoles.push(GuildDB.incomeRoles[i].role) workPays.push(GuildDB.incomeRoles[i].income) } } let banking = await client.dbo.collection("banks").findOne({"banking.userID": interaction.member.user.id}).then(banking => banking); if (!banking) { banking = { userID: interaction.member.user.id, guilds: { [GuildDB.serverID]: { account: { balance: GuildDB.startingBalance, cash: 0.00, } } } } // Register inventory for user let newBank = new Bank(); newBank.createBank(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance); newBank.save().catch(err => { if (err) return client.sendInternalError(err); }); } else banking = banking.banking; if (!client.exists(banking.guilds[GuildDB.serverID])) { const success = addBank(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance); if (!success) return client.sendInternalError('Failed to add bank'); } let job, earned; let newBalance; // get job with highest pay let highestPayIndex = workPays.indexOf(Math.max(workPays)) job = workRoles[highestPayIndex] earned = workPays[highestPayIndex] newBalance = banking.guilds[GuildDB.serverID].account.balance + earned client.dbo.collection("banks").updateOne({"banking.userID":interaction.member.user.id},{$set:{[`banking.guilds.${GuildDB.serverID}.account.balance`]:newBalance}}, function(err, res) { if (err) return client.sendInternalError(err); }); client.dbo.collection("inventories").updateOne({"inventory.userID":interaction.member.user.id}, {$set: {[`inventory.guilds.${GuildDB.serverID}.lastWork`]: now}}, function(err, res) { if (err) return client.sendInternalError(err); }); const success = new EmbedBuilder() .setColor('GREEN') .setDescription(`You worked your job <@&${job}> and earned $${earned}.`) return interaction.send({ embeds: [success] }) } else { const error = new EmbedBuilder() .setColor(client.config.Colors.Default) .setTitle('Take a rest...') .setDescription(`You've already worked a full day today. Take a rest and work tomorrow.`) return interaction.send({ embeds: [error] }) } }, }, }