Add guards to the drop and reroll commands in case the card fetcher fails (#449)
All checks were successful
Test / build (push) Successful in 44s

#446

Reviewed-on: #449
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2025-05-24 10:39:34 +01:00 committed by Vylpes
parent ecc879cf13
commit ead53ba062
4 changed files with 40 additions and 9 deletions

View file

@ -11,6 +11,7 @@ import User from "../database/entities/app/User";
import CardConstants from "../constants/CardConstants";
import GetCardsHelper from "../helpers/DropHelpers/GetCardsHelper";
import DropEmbedHelper from "../helpers/DropHelpers/DropEmbedHelper";
import {DropResult} from "../contracts/SeriesMetadata";
export default class Reroll extends ButtonEvent {
public override async execute(interaction: ButtonInteraction) {
@ -32,7 +33,7 @@ export default class Reroll extends ButtonEvent {
user = new User(interaction.user.id, CardConstants.StartingCurrency);
await user.Save(User, user);
AppLogger.LogInfo("Commands/Drop", `New user (${interaction.user.id}) saved to the database`);
AppLogger.LogInfo("Button/Reroll", `New user (${interaction.user.id}) saved to the database`);
}
if (!user.RemoveCurrency(CardConstants.ClaimCost)) {
@ -40,9 +41,15 @@ export default class Reroll extends ButtonEvent {
return;
}
await user.Save(User, user);
let randomCard: DropResult | undefined;
const randomCard = await GetCardsHelper.FetchCard(interaction.user.id);
try {
randomCard = await GetCardsHelper.FetchCard(interaction.user.id);
} catch (e) {
AppLogger.CatchError("Button/Reroll", e);
await interaction.reply("Unable to fetch card, please try again.");
}
if (!randomCard) {
await interaction.reply("Unable to fetch card, please try again.");
@ -75,6 +82,8 @@ export default class Reroll extends ButtonEvent {
const row = DropEmbedHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id);
await user.Save(User, user);
await interaction.editReply({
embeds: [ embed ],
files: files,

View file

@ -12,6 +12,7 @@ import CardConstants from "../constants/CardConstants";
import ErrorMessages from "../constants/ErrorMessages";
import GetCardsHelper from "../helpers/DropHelpers/GetCardsHelper";
import DropEmbedHelper from "../helpers/DropHelpers/DropEmbedHelper";
import {DropResult} from "../contracts/SeriesMetadata";
export default class Drop extends Command {
constructor() {
@ -48,9 +49,16 @@ export default class Drop extends Command {
return;
}
await user.Save(User, user);
let randomCard: DropResult | undefined;
try {
randomCard = await GetCardsHelper.FetchCard(interaction.user.id);
} catch (e) {
AppLogger.CatchError("Commands/Drop", e);
await interaction.reply(ErrorMessages.UnableToFetchCard);
}
const randomCard = await GetCardsHelper.FetchCard(interaction.user.id);
if (!randomCard) {
AppLogger.LogWarn("Commands/Drop", ErrorMessages.UnableToFetchCard);
@ -82,6 +90,9 @@ export default class Drop extends Command {
const row = DropEmbedHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id);
await user.Save(User, user);
await interaction.editReply({
embeds: [ embed ],
files: files,

View file

@ -55,7 +55,7 @@ export default class GetCardsHelper {
.find(x => x.cards.includes(card));
if (!series) {
AppLogger.LogWarn("CardDropHelperMetadata/GetRandomCardByRarity", `Series not found for card ${card.id}`);
AppLogger.LogError("CardDropHelperMetadata/GetRandomCardByRarity", `Series not found for card ${card.id}`);
return undefined;
}

View file

@ -31,7 +31,7 @@ export default class GetUnclaimedCardsHelper {
public static async GetRandomCardByRarityUnclaimed(rarity: CardRarity, userId: string): Promise<DropResult | undefined> {
const claimedCards = await Inventory.FetchAllByUserId(userId);
if (!claimedCards) {
if (!claimedCards || claimedCards.length == 0) {
// They don't have any cards, so safe to get any random card
return GetCardsHelper.GetRandomCardByRarity(rarity);
}
@ -41,16 +41,27 @@ export default class GetUnclaimedCardsHelper {
.filter(x => x.type == rarity)
.filter(x => !claimedCards.find(y => y.CardNumber == x.id));
if (!allCards) return undefined;
if (!allCards) {
AppLogger.LogError("CardDropHelperMetadata/GetRandomCardByRarityUnclaimed", `No cards found to randomise from, User Id: ${userId}, rarity: ${rarity}`);
return undefined;
};
const randomCardIndex = Math.floor(Math.random() * allCards.length);
const card = allCards[randomCardIndex];
if (!card) {
AppLogger.LogError("CardDropHelperMetadata/GetRandomCardByRarityUnclaimed", `Card not found in index, ${randomCardIndex} of ${allCards.length}, User Id: ${userId}, rarity: ${rarity}`);
return undefined;
}
const series = CoreClient.Cards
.find(x => x.cards.includes(card));
if (!series) {
AppLogger.LogWarn("CardDropHelperMetadata/GetRandomCardByRarityUnclaimed", `Series not found for card ${card.id}`);
AppLogger.LogError("CardDropHelperMetadata/GetRandomCardByRarityUnclaimed", `Series not found for card ${card.id}, User Id: ${userId}, rarity: ${rarity}`);
return undefined;
}