From c1d4a93be7fdd3199ff02ca5eb58d782c90b3396 Mon Sep 17 00:00:00 2001 From: Braeden Sowinski Date: Tue, 28 Mar 2023 17:26:42 -0700 Subject: [PATCH] income --- commands/collect-income.js | 118 +++++++++++++++++++++++++++++++++++++ structures/user.js | 4 +- 2 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 commands/collect-income.js diff --git a/commands/collect-income.js b/commands/collect-income.js new file mode 100644 index 0000000..75808e8 --- /dev/null +++ b/commands/collect-income.js @@ -0,0 +1,118 @@ +const { EmbedBuilder, } = require('discord.js'); +const { User, addUser } = require('../structures/user'); + +module.exports = { + name: "collect-income", + debug: false, + global: false, + description: "collect 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.`, flags: (1 << 6) }); + } + + const hasIncomeRole = GuildDB.incomeRoles.some(data => { + if (interaction.member.roles.includes(data.role)) return true; + return false; + }); + + if (!hasIncomeRole) { + const error = new EmbedBuilder() + .setColor(client.config.Colors.Red) + .setTitle('Missing Income!') + .setDescription(`It appears you don't have any income`) + + return interaction.send({ embeds: [error] }) + } + + let banking = await client.dbo.collection("users").findOne({"user.userID": interaction.member.user.id}).then(banking => banking); + + if (!banking) { + banking = { + userID: interaction.member.user.id, + guilds: { + [GuildDB.serverID]: { + bankAccount: { + balance: GuildDB.startingBalance, + cash: 0.00, + }, + lastIncome: new Date('2000-01-01T00:00:00'), + } + } + } + + // Register inventory for user + let newBank = new User(); + newBank.createBank(interaction.member.user.id, GuildDB.serverID, GuildDB.startingBalance, 0); + newBank.save().catch(err => { + if (err) return client.sendInternalError(interaction, err); + }); + + } else banking = banking.user; + + if (!client.exists(banking.guilds[GuildDB.serverID])) { + const success = addUser(banking.guilds, GuildDB.serverID, interaction.member.user.id, client, GuildDB.startingBalance); + if (!success) return client.sendInternalError(interaction, 'Failed to add bank'); + } + + let now = new Date(); + let diff = (now - inventory.guilds[GuildDB.serverID].lastIncome) / 1000; + diff /= (60 * 60); + let hoursBetweenDates = Math.abs(Math.round(diff)); + + if (hoursBetweenDates >= GuildDB.incomeLimiter) { + let roles = []; + let income = []; + for (let i = 0; i < GuildDB.incomeRoles.length; i++) { + if (interaction.member.roles.includes(GuildDB.incomeRoles[i].role)) { + roles.push(GuildDB.incomeRoles[i].role) + income.push(GuildDB.incomeRoles[i].income) + } + } + + let totalIncome = income.reduce((x, y) => x + y, 0) + + let newData = banking.guilds[GuildDB.serverID]; + newData.bankAccount.balance += totalIncome; + newData.lastIncome = now; + + client.dbo.collection("banks").updateOne({"banking.userID":interaction.member.user.id},{$set:{[`banking.guilds.${GuildDB.serverID}`]: newData}}, function(err, res) { + if (err) return client.sendInternalError(interaction, err); + }); + + let description = `**You collected**`; + for (let i = 0; i < roles.length; i++) { + description += `\n<@&${roles[i]}> - $**${income[i].toFixed(2)}**` + } + + const success = new EmbedBuilder() + .setColor(client.config.Colors.Green) + .setDescription(description) + + 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] }) + } + }, + }, +} \ No newline at end of file diff --git a/structures/user.js b/structures/user.js index f20e400..7da1aef 100644 --- a/structures/user.js +++ b/structures/user.js @@ -15,6 +15,7 @@ userSchema.methods.createUser = function (userID, guildID, startingBalance, cash balance: startingBalance, cash: cash, }, + lastIncome: new Date('2000-01-01T00:00:00'), }; }; @@ -30,7 +31,8 @@ function addUser(guilds, guildID, userID, client, startingBalance) { bankAccount: { balance: startingBalance, cash: 0.00, - } + }, + lastIncome: new Date('2000-01-01T00:00:00') } client.dbo.collection("users").updateOne({"user.userID":userID}, {$set: {"user.guilds": updatedGuilds}}, function(err, res) {