work command + income-role fix

This commit is contained in:
SowinskiBraeden committed 2022-07-27 15:10:28 -07:00
1 parent 5db634f9cd
commit 5f051a15e0
4 files changed
+113 -9

No files matched your search

+1 -1
View File
@@ -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')
+2 -2
View File
@@ -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] });
+108 -3
View File
@@ -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] })
}
},
},
}
+2 -3
View File
@@ -4,8 +4,7 @@ let inventorySchema = mongoose.Schema({
inventory: {
userID: String,
items: [],
createdAt: Date,
updatedAt: Date
lastWork: Date,
}
});
@@ -24,7 +23,7 @@ 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) {