Files
quarksBot/commands/work.js
T

147 lines
5.3 KiB
JavaScript

const { MessageEmbed, } = require('discord.js');
const { Inventory, addInventory } = require('../structures/inventory');
module.exports = {
name: "work",
debug: false,
description: "work to earn your income",
usage: "",
permissions: {
channel: ["VIEW_CHANNEL", "SEND_MESSAGES", "EMBED_LINKS"],
member: [],
},
SlashCommand: {
options: [],
/**
*
* @param {require("../structures/QuarksBot")} client
* @param {import("discord.js").MessageCreate} 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(`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 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 = {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) {
client.log(err);
const embed = new MessageEmbed()
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
.setColor("RED")
return interaction.send({ embeds: [embed] });
}
});
} else inventory = inventory.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("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);
const embed = new MessageEmbed()
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
.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);
const embed = new MessageEmbed()
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
.setColor("RED")
return interaction.send({ embeds: [embed] });
}
});
client.dbo.collection("inventories").updateOne({"inventory.userID":interaction.member.user.id}, {$set: {[`inventory.guilds.${guildDB.serverID}.lastWork`]: now}}, function(err, res) {
if (err) {
client.log(err);
const embed = new MessageEmbed()
.setDescriptions(`**Internal Error:** ${err}\nContact the Developers\nhttps://discord.gg/YCXhvy9uZw`)
.setColor("RED")
return interaction.send({ embeds: [embed] });
}
});
const success = new MessageEmbed()
.setColor('GREEN')
.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] })
}
},
},
}