JaBa/commands/Economy/work.js

106 lines
3.6 KiB
JavaScript
Raw Normal View History

2021-12-10 21:39:54 +05:00
const Command = require("../../base/Command.js"),
Discord = require("discord.js");
class Work extends Command {
2021-12-26 19:29:37 +05:00
constructor(client) {
2021-12-10 21:39:54 +05:00
super(client, {
name: "work",
dirname: __dirname,
enabled: true,
guildOnly: true,
aliases: ["salary", "daily"],
2021-12-10 21:39:54 +05:00
memberPermissions: [],
2021-12-26 19:29:37 +05:00
botPermissions: ["SEND_MESSAGES", "EMBED_LINKS"],
2021-12-10 21:39:54 +05:00
nsfw: false,
ownerOnly: false,
cooldown: 1000
2021-12-10 21:39:54 +05:00
});
}
2021-12-26 19:29:37 +05:00
async run(message, args, data) {
2021-12-10 21:39:54 +05:00
// if the member is already in the cooldown db
const isInCooldown = data.memberData.cooldowns.work;
if (isInCooldown) {
/*if the timestamp recorded in the database indicating
when the member will be able to execute the order again
is greater than the current date, display an error message */
2021-12-26 19:29:37 +05:00
if (isInCooldown > Date.now()) return message.error("economy/work:COOLDOWN", {
time: message.convertTime(isInCooldown, "to", true)
});
2021-12-10 21:39:54 +05:00
};
if (Date.now() > data.memberData.cooldowns.work + (24 * 3600000)) data.memberData.workStreak = 0;
// Records in the database the time when the member will be able to execute the command again (in 12 hours)
const toWait = Date.now() + 43200000;
2021-12-10 21:39:54 +05:00
data.memberData.cooldowns.work = toWait;
data.memberData.markModified("cooldowns");
data.memberData.workStreak = (data.memberData.workStreak || 0) + 1;
await data.memberData.save();
const embed = new Discord.MessageEmbed()
2022-01-04 00:35:11 +05:00
.setFooter({
text: message.translate("economy/work:AWARD"),
iconURL: message.author.displayAvatarURL({
size: 512,
dynamic: true,
format: "png"
})
})
2021-12-10 21:39:54 +05:00
.setColor(data.config.embed.color);
const award = [
this.client.customEmojis.letters.a,
this.client.customEmojis.letters.w,
this.client.customEmojis.letters.a,
this.client.customEmojis.letters.r,
this.client.customEmojis.letters.d
];
let won = 200;
if (data.memberData.workStreak >= 5) {
won += 200;
2021-12-26 19:29:37 +05:00
embed.addField(message.translate("economy/work:SALARY"), message.translate("economy/work:SALARY_CONTENT", {
won: `${won} ${message.getNoun(won, message.translate("misc:NOUNS:CREDIT:1"), message.translate("misc:NOUNS:CREDIT:2"), message.translate("misc:NOUNS:CREDIT:5"))}`
2021-12-26 19:29:37 +05:00
}))
2021-12-10 21:39:54 +05:00
.addField(message.translate("economy/work:STREAK"), message.translate("economy/work:STREAK_CONTENT"));
data.memberData.workStreak = 0;
} else {
for (let i = 0; i < award.length; i++) {
if (data.memberData.workStreak > i) {
const letter = Discord.Util.parseEmoji(award[i]).name.split("_")[1];
award[i] = `:regional_indicator_${letter.toLowerCase()}:`;
};
};
2021-12-26 19:29:37 +05:00
embed.addField(message.translate("economy/work:SALARY"), message.translate("economy/work:SALARY_CONTENT", {
won: `${won} ${message.getNoun(won, message.translate("misc:NOUNS:CREDIT:1"), message.translate("misc:NOUNS:CREDIT:2"), message.translate("misc:NOUNS:CREDIT:5"))}`
2021-12-26 19:29:37 +05:00
}))
2021-12-10 21:39:54 +05:00
.addField(message.translate("economy/work:STREAK"), award.join(""));
};
data.memberData.money = data.memberData.money + won;
data.memberData.save();
2021-12-26 19:29:37 +05:00
const messageOptions = {
embeds: [embed]
2021-12-26 19:29:37 +05:00
};
2021-12-10 21:39:54 +05:00
if (!data.userData.achievements.work.achieved) {
data.userData.achievements.work.progress.now += 1;
if (data.userData.achievements.work.progress.now === data.userData.achievements.work.progress.total) {
2021-12-26 19:29:37 +05:00
messageOptions.files = [{
name: "unlocked.png",
attachment: "./assets/img/achievements/achievement_unlocked1.png"
}];
2021-12-10 21:39:54 +05:00
data.userData.achievements.work.achieved = true;
};
data.userData.markModified("achievements.work");
data.userData.save();
};
// Send the embed in the current channel
message.channel.send(messageOptions);
}
};
module.exports = Work;