diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 6f74d3a..6799a2c 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -10,6 +10,7 @@ import path from "path"; import AppLogger from "../client/appLogger"; import User from "../database/entities/app/User"; import CardConstants from "../constants/CardConstants"; +import ErrorMessages from "../constants/ErrorMessages"; export default class Drop extends Command { constructor() { @@ -22,14 +23,13 @@ export default class Drop extends Command { public override async execute(interaction: CommandInteraction) { if (!CoreClient.AllowDrops) { - await interaction.reply("Bot is currently syncing, please wait until its done."); + await interaction.reply(ErrorMessages.BotSyncing); return; } if (await Config.GetValue("safemode") == "true") { - AppLogger.LogWarn("Commands/Drop", "Safe Mode is active, refusing to send next drop."); - - await interaction.reply("Safe Mode has been activated, please resync to continue."); + AppLogger.LogWarn("Commands/Drop", ErrorMessages.SafeMode); + await interaction.reply(ErrorMessages.SafeMode); return; } @@ -43,16 +43,15 @@ export default class Drop extends Command { } if (user.Currency < CardConstants.ClaimCost) { - await interaction.reply(`Not enough currency! You need ${CardConstants.ClaimCost} currency, you have ${user.Currency}!`); + await interaction.reply(ErrorMessages.NotEnoughCurrency(CardConstants.ClaimCost, user.Currency)); return; } const randomCard = CardDropHelperMetadata.GetRandomCard(); if (!randomCard) { - AppLogger.LogWarn("Commands/Drop", "Unable to fetch card, please try again. (randomCard is null)"); - - await interaction.reply("Unable to fetch card, please try again."); + AppLogger.LogWarn("Commands/Drop", ErrorMessages.UnableToFetchCard); + await interaction.reply(ErrorMessages.UnableToFetchCard); return; } diff --git a/src/commands/multidrop.ts b/src/commands/multidrop.ts new file mode 100644 index 0000000..77cd4b2 --- /dev/null +++ b/src/commands/multidrop.ts @@ -0,0 +1,75 @@ +import { AttachmentBuilder, CommandInteraction, SlashCommandBuilder } from "discord.js"; +import { Command } from "../type/command"; +import { CoreClient } from "../client/client"; +import ErrorMessages from "../constants/ErrorMessages"; +import Config from "../database/entities/app/Config"; +import AppLogger from "../client/appLogger"; +import User from "../database/entities/app/User"; +import CardConstants from "../constants/CardConstants"; +import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; +import { readFileSync } from "fs"; +import path from "path"; +import Inventory from "../database/entities/app/Inventory"; + +export default class Multidrop extends Command { + constructor() { + super(); + + this.CommandBuilder = new SlashCommandBuilder() + .setName("mutlidrop") + .setDescription("Drop 11 cards for the price of 10!"); + } + + public override async execute(interaction: CommandInteraction) { + if (!CoreClient.AllowDrops) { + await interaction.reply(ErrorMessages.BotSyncing); + return; + } + + if (await Config.GetValue("safemode") == "true") { + AppLogger.LogWarn("Commands/Multidrop", ErrorMessages.SafeMode); + await interaction.reply(ErrorMessages.SafeMode); + return; + } + + let user = await User.FetchOneById(User, interaction.user.id); + + if (!user) { + user = new User(interaction.user.id, CardConstants.StartingCurrency); + await user.Save(User, user); + + AppLogger.LogInfo("Commands/Multidrop", `New user (${interaction.user.id}) saved to the database`); + } + + if (user.Currency < CardConstants.MultidropCost) { + await interaction.reply(ErrorMessages.NotEnoughCurrency(CardConstants.MultidropCost, user.Currency)); + return; + } + + user.RemoveCurrency(CardConstants.MultidropCost); + await user.Save(User, user); + + const randomCard = CardDropHelperMetadata.GetRandomCard(); + const cardsRemaining = CardConstants.MultidropQuantity - 1; + + if (!randomCard) { + AppLogger.LogWarn("Commands/Multidrop", ErrorMessages.UnableToFetchCard); + await interaction.reply(ErrorMessages.UnableToFetchCard); + return; + } + + await interaction.deferReply(); + + try { + const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", randomCard.card.path)); + const imageFileName = randomCard.card.path.split("/").pop()!; + + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id); + const quantityClaimed = inventory ? inventory.Quantity : 0; + + const embed = CardDropHelperMetadata.GenerateMultidropEmbed(randomCard, quantityClaimed, imageFileName, cardsRemaining, undefined, user.Currency); + } + } +} \ No newline at end of file diff --git a/src/constants/CardConstants.ts b/src/constants/CardConstants.ts index 0a87e22..3f9723b 100644 --- a/src/constants/CardConstants.ts +++ b/src/constants/CardConstants.ts @@ -3,4 +3,8 @@ export default class CardConstants { public static readonly TimerGiveAmount = 10; public static readonly DailyCurrency = 100; public static readonly StartingCurrency = 300; + + // Multidrop + public static readonly MultidropCost = this.ClaimCost * 10; + public static readonly MultidropQuantity = 11; } \ No newline at end of file diff --git a/src/constants/ErrorMessages.ts b/src/constants/ErrorMessages.ts new file mode 100644 index 0000000..a7f4ee6 --- /dev/null +++ b/src/constants/ErrorMessages.ts @@ -0,0 +1,7 @@ +export default class ErrorMessages { + public static readonly BotSyncing = "Bot is currently syncing, please wait until its done."; + public static readonly SafeMode = "Safe Mode has been activated, please resync to continue."; + public static readonly UnableToFetchCard = "Unable to fetch card, please try again."; + + public static readonly NotEnoughCurrency = (need: number, have: number) => `Not enough currency! You need ${need} currency, you have ${have}!`; +} \ No newline at end of file diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index bc59e93..bc88c88 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -89,7 +89,7 @@ export default class CardDropHelperMetadata { const hexCode = Number("0x" + drop.card.colour); if (hexCode) { - colour = hexCode; + colour = hexCode; } else { AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`); } @@ -149,4 +149,25 @@ export default class CardDropHelperMetadata { .setLabel("Reroll") .setStyle(ButtonStyle.Secondary)); } + + public static GenerateMultidropEmbed(drop: DropResult, quantityClaimed: number, imageFileName: string, cardsRemaining: number, claimedBy?: string, currency?: number): EmbedBuilder { + const dropEmbed = this.GenerateDropEmbed(drop, quantityClaimed, imageFileName, claimedBy, currency); + + dropEmbed.setFooter({ text: `${dropEmbed.data.footer?.text} ยท ${cardsRemaining} Remaining`}); + + return dropEmbed; + } + + public static GenerateMultidropButtons(drop: DropResult, cardsRemaining: number, disabled = false): ActionRowBuilder { + return new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`multidrop keep ${drop.card.id} ${cardsRemaining}`) + .setLabel("Keep") + .setStyle(ButtonStyle.Primary) + .setDisabled(disabled), + new ButtonBuilder() + .setCustomId(`multidrop sacrifice ${drop.card.id} ${cardsRemaining}`) + .setStyle(ButtonStyle.Secondary)); + } }