JaBa/commands/Economy/bank.js

111 lines
4 KiB
JavaScript
Raw Permalink Normal View History

2024-09-19 23:58:06 +05:00
const { SlashCommandBuilder, InteractionContextType, ApplicationIntegrationType } = require("discord.js");
2022-08-09 23:48:33 +05:00
const BaseCommand = require("../../base/BaseCommand");
class Bank extends BaseCommand {
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../base/Client")} client
2022-08-09 23:48:33 +05:00
*/
constructor(client) {
super({
command: new SlashCommandBuilder()
.setName("bank")
.setDescription(client.translate("economy/bank:DESCRIPTION"))
.setDescriptionLocalizations({
2023-07-05 00:58:06 +05:00
uk: client.translate("economy/bank:DESCRIPTION", null, "uk-UA"),
ru: client.translate("economy/bank:DESCRIPTION", null, "ru-RU"),
})
2024-09-19 23:58:06 +05:00
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall])
2024-09-14 19:52:56 +05:00
.setContexts([InteractionContextType.Guild])
2023-07-05 00:58:06 +05:00
.addStringOption(option =>
option
.setName("option")
.setDescription(client.translate("economy/bank:OPTION"))
.setDescriptionLocalizations({
uk: client.translate("economy/bank:OPTION", null, "uk-UA"),
ru: client.translate("economy/bank:OPTION", null, "ru-RU"),
})
.setRequired(true)
.setChoices({ name: client.translate("economy/bank:DEPOSIT"), value: "deposit" }, { name: client.translate("economy/bank:WITHDRAW"), value: "withdraw" }),
)
.addStringOption(option =>
option
.setName("credits")
.setDescription(client.translate("misc:OPTION_NAN_ALL"))
.setDescriptionLocalizations({
uk: client.translate("misc:OPTION_NAN_ALL", null, "uk-UA"),
ru: client.translate("misc:OPTION_NAN_ALL", null, "ru-RU"),
})
.setRequired(true),
),
2022-08-09 23:48:33 +05:00
dirname: __dirname,
ownerOnly: false,
2022-08-09 23:48:33 +05:00
});
}
2022-08-09 23:48:33 +05:00
/**
*
2023-11-05 16:03:23 +05:00
* @param {import("../../base/Client")} client
2022-08-09 23:48:33 +05:00
* @param {import("discord.js").ChatInputCommandInteraction} interaction
*/
2024-02-09 23:26:57 +05:00
async execute(client, interaction) {
const memberData = interaction.data.member,
choice = interaction.options.getString("option");
2022-08-09 23:48:33 +05:00
if (choice === "deposit") {
2024-02-09 23:26:57 +05:00
const credits = interaction.options.getString("credits").toLowerCase() === "all" ? memberData.money : interaction.options.getString("credits");
2022-08-09 23:48:33 +05:00
if (isNaN(credits) || credits < 1) return interaction.error("misc:OPTION_NAN_ALL");
2024-02-09 23:26:57 +05:00
if (memberData.money < credits)
2023-07-05 00:58:06 +05:00
return interaction.error("economy/bank:NOT_ENOUGH_CREDIT", {
money: `**${credits}** ${client.functions.getNoun(credits, interaction.translate("misc:NOUNS:CREDIT:1"), interaction.translate("misc:NOUNS:CREDIT:2"), interaction.translate("misc:NOUNS:CREDIT:5"))}`,
});
2022-08-09 23:48:33 +05:00
2024-02-09 23:26:57 +05:00
memberData.money -= credits;
memberData.bankSold += credits;
2024-02-09 23:26:57 +05:00
await memberData.save();
2022-10-09 14:26:22 +05:00
2022-08-09 23:48:33 +05:00
const info = {
user: interaction.translate("economy/transactions:BANK"),
amount: credits,
date: Date.now(),
type: "send",
2022-08-09 23:48:33 +05:00
};
2024-02-09 23:26:57 +05:00
memberData.transactions.push(info);
2022-08-09 23:48:33 +05:00
interaction.success("economy/bank:SUCCESS_DEP", {
money: `**${credits}** ${client.functions.getNoun(credits, interaction.translate("misc:NOUNS:CREDIT:1"), interaction.translate("misc:NOUNS:CREDIT:2"), interaction.translate("misc:NOUNS:CREDIT:5"))}`,
2022-08-09 23:48:33 +05:00
});
} else {
2024-02-09 23:26:57 +05:00
const credits = interaction.options.getString("credits") === "all" ? memberData.bankSold : interaction.options.getString("credits");
2022-08-09 23:48:33 +05:00
if (isNaN(credits) || credits < 1) return interaction.error("misc:OPTION_NAN_ALL");
2024-02-09 23:26:57 +05:00
if (memberData.bankSold < credits)
2023-07-05 00:58:06 +05:00
return interaction.error("economy/bank:NOT_ENOUGH_BANK", {
money: `**${credits}** ${client.functions.getNoun(credits, interaction.translate("misc:NOUNS:CREDIT:1"), interaction.translate("misc:NOUNS:CREDIT:2"), interaction.translate("misc:NOUNS:CREDIT:5"))}`,
});
2022-08-09 23:48:33 +05:00
const info = {
user: interaction.translate("economy/transactions:BANK"),
amount: credits,
date: Date.now(),
type: "got",
2022-08-09 23:48:33 +05:00
};
2024-02-09 23:26:57 +05:00
memberData.transactions.push(info);
2022-08-09 23:48:33 +05:00
2024-02-09 23:26:57 +05:00
memberData.money += credits;
memberData.bankSold -= credits;
2024-02-09 23:26:57 +05:00
await memberData.save();
2022-08-09 23:48:33 +05:00
interaction.success("economy/bank:SUCCESS_WD", {
money: `**${credits}** ${client.functions.getNoun(credits, interaction.translate("misc:NOUNS:CREDIT:1"), interaction.translate("misc:NOUNS:CREDIT:2"), interaction.translate("misc:NOUNS:CREDIT:5"))}`,
2022-08-09 23:48:33 +05:00
});
}
}
}
2023-07-05 00:58:06 +05:00
module.exports = Bank;