diff --git a/.env.example b/.env.example index 489569a..add48a5 100644 --- a/.env.example +++ b/.env.example @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.8.3 +BOT_VER=0.8.2 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=682942374040961060 diff --git a/package.json b/package.json index 32242a8..0dc2630 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "card-drop", - "version": "0.8.3", + "version": "0.8.2", "main": "./dist/bot.js", "typings": "./dist", "scripts": { diff --git a/src/buttonEvents/Reroll.ts b/src/buttonEvents/Reroll.ts index dc9622a..12578db 100644 --- a/src/buttonEvents/Reroll.ts +++ b/src/buttonEvents/Reroll.ts @@ -51,17 +51,10 @@ export default class Reroll extends ButtonEvent { try { AppLogger.LogVerbose("Button/Reroll", `Sending next drop: ${randomCard.card.id} (${randomCard.card.name})`); - const files = []; - let imageFileName = ""; + const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", randomCard.card.path)); + const imageFileName = randomCard.card.path.split("/").pop()!; - 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; @@ -74,7 +67,7 @@ export default class Reroll extends ButtonEvent { await interaction.editReply({ embeds: [ embed ], - files: files, + files: [ attachment ], components: [ row ], }); diff --git a/src/buttonEvents/View.ts b/src/buttonEvents/View.ts index 007a911..f79a1af 100644 --- a/src/buttonEvents/View.ts +++ b/src/buttonEvents/View.ts @@ -19,7 +19,7 @@ export default class View extends ButtonEvent { await interaction.editReply({ embeds: [ searchResult.embed ], components: [ searchResult.row ], - files: searchResult.attachments, + files: [ searchResult.attachment ], }); } } diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 63ecdd9..6799a2c 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -58,17 +58,10 @@ export default class Drop extends Command { await interaction.deferReply(); try { - const files = []; - let imageFileName = ""; + const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", randomCard.card.path)); + const imageFileName = randomCard.card.path.split("/").pop()!; - 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; @@ -81,7 +74,7 @@ export default class Drop extends Command { await interaction.editReply({ embeds: [ embed ], - files: files, + files: [ attachment ], components: [ row ], }); diff --git a/src/commands/id.ts b/src/commands/id.ts index ea37965..ae924a6 100644 --- a/src/commands/id.ts +++ b/src/commands/id.ts @@ -43,20 +43,22 @@ export default class Id extends Command { const series = CoreClient.Cards .find(x => x.cards.includes(card))!; - const files = []; - let imageFileName = ""; + let image: Buffer; + const imageFileName = card.path.split("/").pop()!; - if (!(card.path.startsWith("http://") || card.path.startsWith("https://"))) { - const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path)); - imageFileName = card.path.split("/").pop()!; + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path)); + } catch { + AppLogger.LogError("Commands/View", `Unable to fetch image for card ${card.id}.`); - const attachment = new AttachmentBuilder(image, { name: imageFileName }); - - files.push(attachment); + await interaction.reply(`Unable to fetch image for card ${card.id}.`); + return; } await interaction.deferReply(); + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id); const quantityClaimed = inventory ? inventory.Quantity : 0; @@ -65,7 +67,7 @@ export default class Id extends Command { try { await interaction.editReply({ embeds: [ embed ], - files: files, + files: [ attachment ], }); } catch (e) { AppLogger.LogError("Commands/View", `Error sending view for card ${card.id}: ${e}`); diff --git a/src/commands/stage/dropnumber.ts b/src/commands/stage/dropnumber.ts index 750210d..0642327 100644 --- a/src/commands/stage/dropnumber.ts +++ b/src/commands/stage/dropnumber.ts @@ -43,20 +43,20 @@ export default class Dropnumber extends Command { const series = CoreClient.Cards .find(x => x.cards.includes(card))!; - const files = []; - let imageFileName = ""; + let image: Buffer; + const imageFileName = card.path.split("/").pop()!; - if (!(card.path.startsWith("http://") || card.path.startsWith("https://"))) { - const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path)); - imageFileName = card.path.split("/").pop()!; - - const attachment = new AttachmentBuilder(image, { name: imageFileName }); - - files.push(attachment); + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.path)); + } catch { + await interaction.reply(`Unable to fetch image for card ${card.id}`); + return; } await interaction.deferReply(); + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id); const quantityClaimed = inventory ? inventory.Quantity : 0; @@ -69,7 +69,7 @@ export default class Dropnumber extends Command { try { await interaction.editReply({ embeds: [ embed ], - files: files, + files: [ attachment ], components: [ row ], }); } catch (e) { diff --git a/src/commands/stage/droprarity.ts b/src/commands/stage/droprarity.ts index 0e95db0..be0a62d 100644 --- a/src/commands/stage/droprarity.ts +++ b/src/commands/stage/droprarity.ts @@ -46,18 +46,20 @@ export default class Droprarity extends Command { return; } - const files = []; - let imageFileName = ""; + let image: Buffer; + const imageFileName = card.card.path.split("/").pop()!; - if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) { - const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); - imageFileName = card.card.path.split("/").pop()!; - - const attachment = new AttachmentBuilder(image, { name: imageFileName }); - - files.push(attachment); + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); + } catch { + await interaction.reply(`Unable to fetch image for card ${card.card.id}`); + return; } + await interaction.deferReply(); + + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.card.id); const quantityClaimed = inventory ? inventory.Quantity : 0; @@ -70,7 +72,7 @@ export default class Droprarity extends Command { try { await interaction.editReply({ embeds: [ embed ], - files: files, + files: [ attachment ], components: [ row ], }); } catch (e) { diff --git a/src/commands/view.ts b/src/commands/view.ts index 9a1d447..3ba5621 100644 --- a/src/commands/view.ts +++ b/src/commands/view.ts @@ -34,7 +34,7 @@ export default class View extends Command { await interaction.editReply({ embeds: [ searchResult.embed ], components: [ searchResult.row ], - files: searchResult.attachments, + files: [ searchResult.attachment ], }); } } \ No newline at end of file diff --git a/src/helpers/CardDropHelperMetadata.ts b/src/helpers/CardDropHelperMetadata.ts index f9f1ea8..8b9b273 100644 --- a/src/helpers/CardDropHelperMetadata.ts +++ b/src/helpers/CardDropHelperMetadata.ts @@ -97,18 +97,12 @@ export default class CardDropHelperMetadata { AppLogger.LogWarn("CardDropHelperMetadata/GenerateDropEmbed", `Card's colour override is invalid: ${drop.card.id}, ${drop.card.colour}`); } - let imageUrl = `attachment://${imageFileName}`; - - if (drop.card.path.startsWith("http://") || drop.card.path.startsWith("https://")) { - imageUrl = drop.card.path; - } - const embed = new EmbedBuilder() .setTitle(drop.card.name) .setDescription(description) .setFooter({ text: `${CardRarityToString(drop.card.type)} ยท ${drop.card.id}` }) .setColor(colour) - .setImage(imageUrl) + .setImage(`attachment://${imageFileName}`) .addFields([ { name: "Claimed", diff --git a/src/helpers/CardSearchHelper.ts b/src/helpers/CardSearchHelper.ts index d548245..29014f7 100644 --- a/src/helpers/CardSearchHelper.ts +++ b/src/helpers/CardSearchHelper.ts @@ -10,7 +10,7 @@ import AppLogger from "../client/appLogger.js"; interface ReturnedPage { embed: EmbedBuilder, row: ActionRowBuilder, - attachments: AttachmentBuilder[], + attachment: AttachmentBuilder, results: string[], } @@ -36,18 +36,19 @@ export default class CardSearchHelper { if (!card) return undefined; - const attachments = []; - let imageFileName = ""; + let image: Buffer; + const imageFileName = card.card.path.split("/").pop()!; - if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) { - const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); - imageFileName = card.card.path.split("/").pop()!; - - const attachment = new AttachmentBuilder(image, { name: imageFileName }); - - attachments.push(attachment); + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); + } catch { + AppLogger.LogError("CardSearchHelper/GenerateSearchQuery", `Unable to fetch image for card ${card.card.id}.`); + + return undefined; } + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + const inventory = await Inventory.FetchOneByCardNumberAndUserId(userid, card.card.id); const quantityClaimed = inventory?.Quantity ?? 0; @@ -66,7 +67,7 @@ export default class CardSearchHelper { .setStyle(ButtonStyle.Primary) .setDisabled(pages == 1)); - return { embed, row, attachments, results }; + return { embed, row, attachment, results }; } public static async GenerateSearchPageFromQuery(results: string[], userid: string, page: number): Promise { @@ -80,18 +81,19 @@ export default class CardSearchHelper { return undefined; } - const attachments = []; - let imageFileName = ""; + let image: Buffer; + const imageFileName = card.card.path.split("/").pop()!; - if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) { - const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); - imageFileName = card.card.path.split("/").pop()!; + try { + image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path)); + } catch { + AppLogger.LogError("CardSearchHelper/GenerateSearchPageFromQuery", `Unable to fetch image for card ${card.card.id}.`); - const attachment = new AttachmentBuilder(image, { name: imageFileName }); - - attachments.push(attachment); + return undefined; } + const attachment = new AttachmentBuilder(image, { name: imageFileName }); + const inventory = await Inventory.FetchOneByCardNumberAndUserId(userid, card.card.id); const quantityClaimed = inventory?.Quantity ?? 0; @@ -110,6 +112,6 @@ export default class CardSearchHelper { .setStyle(ButtonStyle.Primary) .setDisabled(page == results.length)); - return { embed, row, attachments, results }; + return { embed, row, attachment, results }; } }