-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ability to drop multiple cards in a row (#376)
- Create a `/multidrop` command - This will take the price of 10 drops from you and give you 11 cards to sort through - You then have a choice to keep the card or sacrifice it - Create the `multidrop keep` and `multidrop sacrifice` button events #262 Reviewed-on: https://git.vylpes.xyz/External/card-drop/pulls/376 Reviewed-by: VylpesTester <[email protected]> Co-authored-by: Ethan Lane <[email protected]> Co-committed-by: Ethan Lane <[email protected]>
- Loading branch information
Showing
8 changed files
with
348 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
import { AttachmentBuilder, ButtonInteraction, EmbedBuilder } from "discord.js"; | ||
import { ButtonEvent } from "../type/buttonEvent"; | ||
import AppLogger from "../client/appLogger"; | ||
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata"; | ||
import Inventory from "../database/entities/app/Inventory"; | ||
import EmbedColours from "../constants/EmbedColours"; | ||
import { readFileSync } from "fs"; | ||
import path from "path"; | ||
import ErrorMessages from "../constants/ErrorMessages"; | ||
import User from "../database/entities/app/User"; | ||
import { GetSacrificeAmount } from "../constants/CardRarity"; | ||
|
||
export default class Multidrop extends ButtonEvent { | ||
public override async execute(interaction: ButtonInteraction) { | ||
const action = interaction.customId.split(" ")[1]; | ||
|
||
switch (action) { | ||
case "keep": | ||
await this.Keep(interaction); | ||
break; | ||
case "sacrifice": | ||
await this.Sacrifice(interaction); | ||
break; | ||
default: | ||
await interaction.reply("Invalid action"); | ||
AppLogger.LogError("Button/Multidrop", `Invalid action, ${action}`); | ||
} | ||
} | ||
|
||
private async Keep(interaction: ButtonInteraction) { | ||
const cardNumber = interaction.customId.split(" ")[2]; | ||
let cardsRemaining = Number(interaction.customId.split(" ")[3]) || 0; | ||
const userId = interaction.customId.split(" ")[4]; | ||
|
||
if (interaction.user.id != userId) { | ||
await interaction.reply("You're not the user this drop was made for!"); | ||
return; | ||
} | ||
|
||
const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); | ||
|
||
if (!card) { | ||
await interaction.reply("Unable to find card."); | ||
AppLogger.LogWarn("Button/Multidrop/Keep", `Card not found, ${cardNumber}`); | ||
return; | ||
} | ||
|
||
if (cardsRemaining < 0) { | ||
await interaction.reply("Your multidrop has ran out! Please buy a new one!"); | ||
return; | ||
} | ||
|
||
const user = await User.FetchOneById(User, interaction.user.id); | ||
|
||
if (!user) { | ||
AppLogger.LogWarn("Button/Multidrop/Keep", ErrorMessages.UnableToFetchUser); | ||
await interaction.reply(ErrorMessages.UnableToFetchUser); | ||
return; | ||
} | ||
|
||
// Claim | ||
let inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, cardNumber); | ||
|
||
if (!inventory) { | ||
inventory = new Inventory(interaction.user.id, cardNumber, 1); | ||
} else { | ||
inventory.AddQuantity(1); | ||
} | ||
|
||
await inventory.Save(Inventory, inventory); | ||
|
||
// Pack has ran out | ||
if (cardsRemaining == 0) { | ||
const embed = new EmbedBuilder() | ||
.setDescription("Your multidrop has ran out! Please buy a new one!") | ||
.setColor(EmbedColours.Ok); | ||
|
||
await interaction.update({ | ||
embeds: [ embed ], | ||
attachments: [], | ||
components: [], | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// Drop next card | ||
const randomCard = CardDropHelperMetadata.GetRandomCard(); | ||
cardsRemaining -= 1; | ||
|
||
if (!randomCard) { | ||
AppLogger.LogWarn("Button/Multidrop/Keep", ErrorMessages.UnableToFetchCard); | ||
await interaction.reply(ErrorMessages.UnableToFetchCard); | ||
return; | ||
} | ||
|
||
await interaction.deferUpdate(); | ||
|
||
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); | ||
|
||
const row = CardDropHelperMetadata.GenerateMultidropButtons(randomCard, cardsRemaining, interaction.user.id, cardsRemaining < 0); | ||
|
||
await interaction.editReply({ | ||
embeds: [ embed ], | ||
files: [ attachment ], | ||
components: [ row ], | ||
}); | ||
} catch (e) { | ||
AppLogger.LogError("Button/Multidrop/Keep", `Error sending next drop for card ${randomCard.card.id}: ${e}`); | ||
|
||
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`); | ||
} | ||
} | ||
|
||
private async Sacrifice(interaction: ButtonInteraction) { | ||
const cardNumber = interaction.customId.split(" ")[2]; | ||
let cardsRemaining = Number(interaction.customId.split(" ")[3]) || 0; | ||
const userId = interaction.customId.split(" ")[4]; | ||
|
||
if (interaction.user.id != userId) { | ||
await interaction.reply("You're not the user this drop was made for!"); | ||
return; | ||
} | ||
|
||
const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber); | ||
|
||
if (!card) { | ||
await interaction.reply("Unable to find card."); | ||
AppLogger.LogWarn("Button/Multidrop/Sacrifice", `Card not found, ${cardNumber}`); | ||
return; | ||
} | ||
|
||
if (cardsRemaining < 0) { | ||
await interaction.reply("Your multidrop has ran out! Please buy a new one!"); | ||
return; | ||
} | ||
|
||
const user = await User.FetchOneById(User, interaction.user.id); | ||
|
||
if (!user) { | ||
AppLogger.LogWarn("Button/Multidrop/Sacrifice", ErrorMessages.UnableToFetchUser); | ||
await interaction.reply(ErrorMessages.UnableToFetchUser); | ||
return; | ||
} | ||
|
||
// Sacrifice | ||
const sacrificeAmount = GetSacrificeAmount(card.card.type); | ||
|
||
user.AddCurrency(sacrificeAmount); | ||
|
||
await user.Save(User, user); | ||
|
||
// Pack has ran out | ||
if (cardsRemaining == 0) { | ||
const embed = new EmbedBuilder() | ||
.setDescription("Your multidrop has ran out! Please buy a new one!") | ||
.setColor(EmbedColours.Ok); | ||
|
||
await interaction.update({ | ||
embeds: [ embed ], | ||
attachments: [], | ||
components: [], | ||
}); | ||
|
||
return; | ||
} | ||
|
||
// Drop next card | ||
const randomCard = CardDropHelperMetadata.GetRandomCard(); | ||
cardsRemaining -= 1; | ||
|
||
if (!randomCard) { | ||
AppLogger.LogWarn("Button/Multidrop/Sacrifice", ErrorMessages.UnableToFetchCard); | ||
await interaction.reply(ErrorMessages.UnableToFetchCard); | ||
return; | ||
} | ||
|
||
await interaction.deferUpdate(); | ||
|
||
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); | ||
|
||
const row = CardDropHelperMetadata.GenerateMultidropButtons(randomCard, cardsRemaining, interaction.user.id, cardsRemaining < 0); | ||
|
||
await interaction.editReply({ | ||
embeds: [ embed ], | ||
files: [ attachment ], | ||
components: [ row ], | ||
}); | ||
} catch (e) { | ||
AppLogger.LogError("Button/Multidrop/Sacrifice", `Error sending next drop for card ${randomCard.card.id}: ${e}`); | ||
|
||
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
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("multidrop") | ||
.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); | ||
|
||
const row = CardDropHelperMetadata.GenerateMultidropButtons(randomCard, cardsRemaining, interaction.user.id); | ||
|
||
await interaction.editReply({ | ||
embeds: [ embed ], | ||
files: [ attachment ], | ||
components: [ row ], | ||
}); | ||
} catch (e) { | ||
AppLogger.LogError("Commands/Multidrop", `Error sending next drop for card ${randomCard.card.id}: ${e}`); | ||
|
||
await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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 UnableToFetchUser = "Unable to fetch user, please try again."; | ||
|
||
public static readonly NotEnoughCurrency = (need: number, have: number) => `Not enough currency! You need ${need} currency, you have ${have}!`; | ||
} |
Oops, something went wrong.