diff --git a/commands/remove-income.js b/commands/remove-income.js index 9079011..7b50467 100644 --- a/commands/remove-income.js +++ b/commands/remove-income.js @@ -31,7 +31,7 @@ module.exports = { return interaction.send(`You are not allowed to use the bot in this channel.`); } - const searchIndex = roles.findIndex((role) => role.role==args[0].value); + const searchIndex = GuildDB.incomeRoles.findIndex((role) => role.role==args[0].value); if (searchIndex == -1) { let embed = new MessageEmbed() .setTitle('Role not found') diff --git a/commands/set-income.js b/commands/set-income.js index 43935f5..311b952 100644 --- a/commands/set-income.js +++ b/commands/set-income.js @@ -46,7 +46,7 @@ module.exports = { return interaction.send({ embeds: [embed] }); } - const searchIndex = roles.findIndex((role) => role.role==args[0].value); + const searchIndex = GuildDB.incomeRoles.findIndex((role) => role.role==args[0].value); if (searchIndex == -1) { let newIncome = { role: args[0].value, @@ -79,7 +79,7 @@ module.exports = { let successEmbed = new MessageEmbed() .setTitle('Success') - .setDescription(`Set <@&${args[0].value}>'s income to $${args[1].value}`) + .setDescription(`Updated <@&${args[0].value}>'s income to $${args[1].value}`) .setColor('GREEN'); return interaction.send({ embeds: [successEmbed] }); diff --git a/commands/work.js b/commands/work.js index 0eba3c5..1aebea2 100644 --- a/commands/work.js +++ b/commands/work.js @@ -1,4 +1,4 @@ -const { MessageEmbed } = require('discord.js'); +const { MessageEmbed, Guild } = require('discord.js'); module.exports = { name: "work", @@ -23,8 +23,113 @@ module.exports = { return interaction.send(`You are not allowed to use the bot in this channel.`); } - console.log(interaction.member) - return interaction.send('Debugging...') + const hasWorkRole = GuildDB.incomeRoles.some(data => { + if (interaction.member.roles.includes(data.role)) return true; + return false; + }); + + if (!hasWorkRole) { + const error = new MessageEmbed() + .setColor('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 = {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); + newInventory.save() + .catch(err => { + if (err) { + client.log(err); + let embed = new MessageEmbed() + .setTitle(err) + .setColor("RED") + + return interaction.send({ embeds: [embed] }); + } + }); + } else inventory = inventory.inventory + + const msBetweenDates = Math.abs(then.getTime() - now.getTime()) + 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("banking").findOne({"account.userID": interaction.member.user.id}).then(banking => banking); + + if (!banking) { + banking = { + account: { + userID: interaction.member.user.id, + balance: GuildDB.startingBalance, + cash: 0.00, + } + } + + client.dbo.collection("banking").insertOne(banking, function(err, res) { + if (err) { + client.log(err); + let embed = new MessageEmbed() + .setTitle(err) + .setColor("RED") + + return interaction.send({ embeds: [embed] }); + } + }); + } + + 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.account.balance + earned + + client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{"account.balance":newBalance}}, function(err, res) { + if (err) { + client.log(err); + let embed = new MessageEmbed() + .setTitle(err) + .setColor("RED") + + return interaction.send({ embeds: [embed] }); + } + }); + + const success = new MessageEmbed() + .setColor('GREEN') + .setTitle('Worked') + .setDescription(`You worked your job <@&${job}> and earned $${earned}.`) + + return interaction.send({ embeds: [success] }) + + } else { + const error = new MessageEmbed() + .setColor(client.config.EmbedColor) + .setTitle('Take a rest') + .setDescription(`You've already worked a full day today. Take a rest and work tomorrow.`) + + return interaction.send({ embeds: [error] }) + } }, }, } \ No newline at end of file diff --git a/structures/inventory.js b/structures/inventory.js index bca3664..79d00a6 100644 --- a/structures/inventory.js +++ b/structures/inventory.js @@ -4,8 +4,7 @@ let inventorySchema = mongoose.Schema({ inventory: { userID: String, items: [], - createdAt: Date, - updatedAt: Date + lastWork: Date, } }); @@ -24,8 +23,8 @@ inventorySchema.methods.createInventory = function (userID) { } } this.inventory.items.push(walletData); - this.inventory.createdAt = new Date(); -}; + this.inventory.lastWork = new Date('2000-01-01T00:00:00'); // garantee's they can work right after inventory create + }; inventorySchema.methods.addItem = function (item) { this.inventory.items.push(item);