168 lines
6.2 KiB
JavaScript
168 lines
6.2 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 = {
|
|
userID: interaction.member.user.id,
|
|
guidls: {
|
|
[guildID]: {
|
|
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) {
|
|
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 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) {
|
|
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.guilds[guildDB.serverID].account.balance + earned
|
|
|
|
client.dbo.collection("banking").updateOne({"account.userID":interaction.member.user.id},{$set:{[`banking.guilds.${guildDB.serverID}.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] })
|
|
}
|
|
},
|
|
},
|
|
} |