Compare commits

...

8 commits
v0.9.0 ... main

Author SHA1 Message Date
fc3c98f1bb v0.9.2
All checks were successful
Deploy To Production / build (push) Successful in 56s
Deploy To Production / deploy (push) Successful in 17s
2025-05-28 18:17:48 +01:00
c9f7c443cf Fix effects helper returning an error when the buttons are disabled (#454)
All checks were successful
Test / build (push) Successful in 45s
-This caused an issue with the updated embed after confirming to buy an effect it checking you had enough currency after taking the currency away

#453

Reviewed-on: #454
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2025-05-28 16:14:42 +01:00
434f162a01 Update unclaimed card filter to fallback to any card if all cards are claimed (#452)
All checks were successful
Test / build (push) Successful in 1m2s
#451

Reviewed-on: #452
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2025-05-28 16:07:38 +01:00
7d9c1bda62 v0.9.1
All checks were successful
Deploy To Production / build (push) Successful in 1m1s
Deploy To Production / deploy (push) Successful in 18s
Test / build (push) Successful in 45s
2025-05-25 17:00:43 +01:00
8e63f26747 Update inventory dropdown logic to use a range around the current page so that we can remain within the 25 pages discord allows (#450)
All checks were successful
Test / build (push) Successful in 44s
#443

Reviewed-on: #450
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2025-05-24 11:51:56 +01:00
ead53ba062 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>
2025-05-24 10:39:34 +01:00
ecc879cf13 Fix sacrificing a dropped card taking from your inventory first (#448)
All checks were successful
Test / build (push) Successful in 46s
- Fix sacrificing a dropped card trying to take it from your inventory instead of just directly from the drop
    - Did it by creating a separate subaction for the sacrifice command, which does the same logic except without checking your inventory, since it wont be coming from there.
- Fix the sacrificed card image showing outside of the embed

#435, #436

Reviewed-on: #448
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2025-05-24 10:05:49 +01:00
f2c949c78a Fix multidrop not handling externally hosted images correctly (#447)
All checks were successful
Test / build (push) Successful in 48s
- Fix multidrop command not handling externally hosted images correctly
    -  It was trying to find the `https://` link in the file system, which it couldn't find
    - Fixed by copying existing logic from the drop command into the multidrop command

#442

Reviewed-on: #447
Reviewed-by: VylpesTester <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
2025-05-24 10:04:24 +01:00
13 changed files with 205 additions and 27 deletions

View file

@ -1,6 +1,6 @@
{
"name": "card-drop",
"version": "0.9.0",
"version": "0.9.2",
"main": "./dist/bot.js",
"typings": "./dist",
"scripts": {

View file

@ -98,10 +98,18 @@ export default class Multidrop extends ButtonEvent {
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 files = [];
let imageFileName = "";
if (!(randomCard.card.path.startsWith("http://") || randomCard.card.path.startsWith("https://"))) {
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", randomCard.card.path));
imageFileName = randomCard.card.path.split("/").pop()!;
const attachment = new AttachmentBuilder(image, { name: imageFileName });
files.push(attachment);
}
const attachment = new AttachmentBuilder(image, { name: imageFileName });
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
@ -112,7 +120,7 @@ export default class Multidrop extends ButtonEvent {
await interaction.editReply({
embeds: [ embed ],
files: [ attachment ],
files: files,
components: [ row ],
});
} catch (e) {

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

@ -5,6 +5,7 @@ import { CardRarityToString, GetSacrificeAmount } from "../constants/CardRarity"
import EmbedColours from "../constants/EmbedColours";
import User from "../database/entities/app/User";
import GetCardsHelper from "../helpers/DropHelpers/GetCardsHelper";
import CardConstants from "../constants/CardConstants";
export default class Sacrifice extends ButtonEvent {
public override async execute(interaction: ButtonInteraction) {
@ -17,6 +18,8 @@ export default class Sacrifice extends ButtonEvent {
case "cancel":
await this.cancel(interaction);
break;
case "give":
await this.give(interaction);
}
}
@ -168,4 +171,77 @@ export default class Sacrifice extends ButtonEvent {
components: [ row ],
});
}
private async give(interaction: ButtonInteraction) {
const userId = interaction.customId.split(" ")[2];
const cardNumber = interaction.customId.split(" ")[3];
const quantity = Number(interaction.customId.split(" ")[4]) || 1;
if (userId != interaction.user.id) {
await interaction.reply("Only the user who created this sacrifice can confirm it.");
return;
}
const cardData = GetCardsHelper.GetCardByCardNumber(cardNumber);
if (!cardData) {
await interaction.reply("Unable to find card in the database.");
return;
}
let user = await User.FetchOneById(User, userId);
if (!user) {
user = new User(userId, CardConstants.StartingCurrency);
}
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
let cardQuantity = 0;
if (cardInInventory) {
cardQuantity = cardInInventory.Quantity;
}
const cardValue = GetSacrificeAmount(cardData.card.type) * quantity;
const cardRarityString = CardRarityToString(cardData.card.type);
user.AddCurrency(cardValue);
await user.Save(User, user);
const description = [
`Card: ${cardData.card.name}`,
`Series: ${cardData.series.name}`,
`Rarity: ${cardRarityString}`,
`Quantity Owned: ${cardQuantity}`,
`Quantity To Sacrifice: ${quantity}`,
`Sacrifice Amount: ${cardValue}`,
];
const embed = new EmbedBuilder()
.setTitle("Card Sacrificed")
.setDescription(description.join("\n"))
.setColor(EmbedColours.Green)
.setFooter({ text: `${interaction.user.username}` });
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents([
new ButtonBuilder()
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardNumber}`)
.setLabel("Confirm")
.setStyle(ButtonStyle.Success)
.setDisabled(true),
new ButtonBuilder()
.setCustomId("sacrifice cancel")
.setLabel("Cancel")
.setStyle(ButtonStyle.Secondary)
.setDisabled(true),
]);
await interaction.update({
embeds: [ embed ],
components: [ row ],
attachments: [],
});
}
}

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

@ -62,10 +62,17 @@ export default class Multidrop extends Command {
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 files = [];
let imageFileName = "";
const attachment = new AttachmentBuilder(image, { name: imageFileName });
if (!(randomCard.card.path.startsWith("http://") || randomCard.card.path.startsWith("https://"))) {
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", randomCard.card.path));
imageFileName = randomCard.card.path.split("/").pop()!;
const attachment = new AttachmentBuilder(image, { name: imageFileName });
files.push(attachment);
}
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
@ -76,7 +83,7 @@ export default class Multidrop extends Command {
await interaction.editReply({
embeds: [ embed ],
files: [ attachment ],
files: files,
components: [ row ],
});
} catch (e) {

View file

@ -77,7 +77,7 @@ export default class DropEmbedHelper {
.setStyle(ButtonStyle.Success)
.setDisabled(disabled),
new ButtonBuilder()
.setCustomId(`sacrifice confirm ${userId} ${drop.card.id} 1`)
.setCustomId(`sacrifice give ${userId} ${drop.card.id} 1`)
.setLabel(`Sacrifice`)
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()

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);
}
@ -39,18 +39,28 @@ export default class GetUnclaimedCardsHelper {
const allCards = CoreClient.Cards
.flatMap(x => x.cards)
.filter(x => x.type == rarity)
.filter(x => !claimedCards.find(y => y.CardNumber == x.id));
.filter(x => !claimedCards.find(y => y.CardNumber == x.id && y.Quantity > 0));
if (!allCards) return undefined;
if (!allCards || allCards.length == 0) {
// There is no card left unclaimed, fallback to any card
return GetCardsHelper.GetRandomCardByRarity(rarity);
};
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;
}

View file

@ -151,7 +151,7 @@ export default class EffectHelper {
AppLogger.LogInfo("EffectHelper", `Created initial user entity for : ${userId}`);
}
if (user.Currency < totalCost) {
if (!disabled && user.Currency < totalCost) {
return `You don't have enough currency to buy this! You have \`${user.Currency} Currency\` and need \`${totalCost} Currency\`!`;
}

View file

@ -115,17 +115,48 @@ export default class InventoryHelper {
let pageNum = 0;
const maxLength = 25; // Discord API limit
const allPageOptions = pages.map(x =>
new StringSelectMenuOptionBuilder()
.setLabel(`${x.name} (${x.seriesSubpage + 1})`.substring(0, 100))
.setDescription(`Page ${pageNum + 1}`)
.setDefault(currentPage.id == x.id)
.setValue(`${userid} ${pageNum++}`));
const currentPageIndex = allPageOptions.indexOf(allPageOptions.find(x => x.data.default)!);
let pageOptions: StringSelectMenuOptionBuilder[] = [];
if (allPageOptions.length <= maxLength) {
pageOptions = allPageOptions;
} else {
let startIndex = currentPageIndex - Math.floor((maxLength - 1) / 2);
let endIndexOffset = 0;
if (startIndex < 0) {
endIndexOffset = 0 - startIndex;
startIndex = 0;
}
let endIndex = currentPageIndex + Math.floor((maxLength - 1) / 2) + endIndexOffset;
if (endIndex + 1 > allPageOptions.length) {
endIndex = allPageOptions.length;
}
for (let i = startIndex; i < endIndex; i++) {
pageOptions.push(allPageOptions[i]);
}
}
const row2 = new ActionRowBuilder<StringSelectMenuBuilder>()
.addComponents(
new StringSelectMenuBuilder()
.setCustomId("inventory")
.setPlaceholder(`${currentPage.name} (${currentPage.seriesSubpage + 1})`)
.addOptions(...pages.map(x =>
new StringSelectMenuOptionBuilder()
.setLabel(`${x.name} (${x.seriesSubpage + 1})`.substring(0, 100))
.setDescription(`Page ${pageNum + 1}`)
.setDefault(currentPage.id == x.id)
.setValue(`${userid} ${pageNum++}`))));
.addOptions(pageOptions));
const buffer = await ImageHelper.GenerateCardImageGrid(currentPage.cards.map(x => ({ id: x.id, path: x.path })));
const image = new AttachmentBuilder(buffer, { name: "page.png" });

View file

@ -0,0 +1,13 @@
describe("execute", () => {
describe("GIVEN randomCard image is hosted locally", () => {
test.todo("EXPECT image to be uploaded directly");
});
describe("GIVEN randomCard image is hosted via http", () => {
test.todo("EXPECT image link to be directly added to embed");
});
describe("GIVEN randomCard image is hosted via https", () => {
test.todo("EXPECT image link to be directly added to embed");
});
});

View file

@ -0,0 +1,13 @@
describe("execute", () => {
describe("GIVEN randomCard image is hosted locally", () => {
test.todo("EXPECT image to be uploaded directly");
});
describe("GIVEN randomCard image is hosted via http", () => {
test.todo("EXPECT image link to be directly added to embed");
});
describe("GIVEN randomCard image is hosted via https", () => {
test.todo("EXPECT image link to be directly added to embed");
});
});