From ec2b5237029397ae4444c56884d49f66d1c1f13a Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 27 Oct 2023 16:02:02 +0100 Subject: [PATCH] Add dropnumber test command (#69) # Description Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. - Add `dropnumber` test command, for test servers only - Remove `DROP_CARD` environment variable #39 ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) # How Has This Been Tested? Please describe the tests that you ran to verify the changes. Provide instructions so we can reproduce. Please also list any relevant details to your test configuration. - This has been tested locally # Checklist - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that provde my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged and published in downstream modules Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/69 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- .dev.env | 1 - .prod.env | 1 - .stage.env | 1 - src/buttonEvents/Reroll.ts | 36 ++------------- src/commands/drop.ts | 36 ++------------- src/commands/stage/dropnumber.ts | 76 ++++++++++++++++++++++++++++++++ src/constants/Environment.ts | 1 + src/helpers/CardDropHelper.ts | 29 +++++++++++- src/registry.ts | 10 +++++ 9 files changed, 121 insertions(+), 70 deletions(-) create mode 100644 src/commands/stage/dropnumber.ts diff --git a/.dev.env b/.dev.env index b275229..d7841d4 100644 --- a/.dev.env +++ b/.dev.env @@ -17,7 +17,6 @@ ABOUT_FUNDING= ABOUT_REPO= DROP_RARITY=-1 -DROP_CARD=-1 DB_HOST=127.0.0.1 DB_PORT=3301 diff --git a/.prod.env b/.prod.env index afbba60..1a29d54 100644 --- a/.prod.env +++ b/.prod.env @@ -17,7 +17,6 @@ ABOUT_FUNDING= ABOUT_REPO= DROP_RARITY=-1 -DROP_CARD=-1 DB_HOST=127.0.0.1 DB_PORT=3321 diff --git a/.stage.env b/.stage.env index c1df020..330e255 100644 --- a/.stage.env +++ b/.stage.env @@ -17,7 +17,6 @@ ABOUT_FUNDING= ABOUT_REPO= DROP_RARITY=-1 -DROP_CARD=-1 DB_HOST=127.0.0.1 DB_PORT=3311 diff --git a/src/buttonEvents/Reroll.ts b/src/buttonEvents/Reroll.ts index 7e37a81..0f99267 100644 --- a/src/buttonEvents/Reroll.ts +++ b/src/buttonEvents/Reroll.ts @@ -1,8 +1,7 @@ -import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CacheType, DiscordAPIError, EmbedBuilder } from "discord.js"; +import { AttachmentBuilder, ButtonInteraction, DiscordAPIError } from "discord.js"; import { ButtonEvent } from "../type/buttonEvent"; import CardDropHelper from "../helpers/CardDropHelper"; import { readFileSync } from "fs"; -import { CardRarityToColour, CardRarityToString } from "../constants/CardRarity"; import { v4 } from "uuid"; import { CoreClient } from "../client/client"; import Card from "../database/entities/card/Card"; @@ -16,15 +15,6 @@ export default class Reroll extends ButtonEvent { if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) { randomCard = await CardDropHelper.GetRandomCardByRarity(Number(process.env.DROP_RARITY)); - } else if (process.env.DROP_CARD && process.env.DROP_CARD != '-1') { - let card = await Card.FetchOneByCardNumber(process.env.DROP_CARD, [ "Series" ]); - - if (!card) { - await interaction.reply("Card not found"); - return; - } - - randomCard = card; } const image = readFileSync(randomCard.Path); @@ -36,30 +26,11 @@ export default class Reroll extends ButtonEvent { const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.CardNumber); const quantityClaimed = inventory ? inventory.Quantity : 0; - let embedDescription = ""; - embedDescription += `Series: ${randomCard.Series.Name}\n`; - embedDescription += `Claimed: ${quantityClaimed || 0}\n`; - - const embed = new EmbedBuilder() - .setTitle(randomCard.Name) - .setDescription(embedDescription) - .setFooter({ text: CardRarityToString(randomCard.Rarity) }) - .setColor(CardRarityToColour(randomCard.Rarity)) - .setImage(`attachment://${randomCard.FileName}`); - - const row = new ActionRowBuilder(); + const embed = CardDropHelper.GenerateDropEmbed(randomCard, quantityClaimed || 0); const claimId = v4(); - row.addComponents( - new ButtonBuilder() - .setCustomId(`claim ${randomCard.CardNumber} ${claimId} ${interaction.user.id}`) - .setLabel("Claim") - .setStyle(ButtonStyle.Primary), - new ButtonBuilder() - .setCustomId(`reroll`) - .setLabel("Reroll") - .setStyle(ButtonStyle.Secondary)); + const row = CardDropHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id); try { await interaction.editReply({ @@ -70,7 +41,6 @@ export default class Reroll extends ButtonEvent { } catch (e) { console.error(e); - if (e instanceof DiscordAPIError) { await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`); } else { diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 260f58e..031bbe7 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -1,7 +1,6 @@ -import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, DiscordAPIError, EmbedBuilder, SlashCommandBuilder } from "discord.js"; +import { AttachmentBuilder, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js"; import { Command } from "../type/command"; import CardDropHelper from "../helpers/CardDropHelper"; -import { CardRarityToColour, CardRarityToString } from "../constants/CardRarity"; import { readFileSync } from "fs"; import { CoreClient } from "../client/client"; import { v4 } from "uuid"; @@ -22,15 +21,6 @@ export default class Drop extends Command { if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) { randomCard = await CardDropHelper.GetRandomCardByRarity(Number(process.env.DROP_RARITY)); - } else if (process.env.DROP_CARD && process.env.DROP_CARD != '-1') { - let card = await Card.FetchOneByCardNumber(process.env.DROP_CARD, [ "Series" ]); - - if (!card) { - await interaction.reply("Card not found"); - return; - } - - randomCard = card; } const image = readFileSync(randomCard.Path); @@ -42,30 +32,11 @@ export default class Drop extends Command { const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.CardNumber); const quantityClaimed = inventory ? inventory.Quantity : 0; - let embedDescription = ""; - embedDescription += `Series: ${randomCard.Series.Name}\n`; - embedDescription += `Claimed: ${quantityClaimed || 0}\n`; - - const embed = new EmbedBuilder() - .setTitle(randomCard.Name) - .setDescription(embedDescription) - .setFooter({ text: CardRarityToString(randomCard.Rarity) }) - .setColor(CardRarityToColour(randomCard.Rarity)) - .setImage(`attachment://${randomCard.FileName}`); - - const row = new ActionRowBuilder(); + const embed = CardDropHelper.GenerateDropEmbed(randomCard, quantityClaimed || 0); const claimId = v4(); - row.addComponents( - new ButtonBuilder() - .setCustomId(`claim ${randomCard.CardNumber} ${claimId} ${interaction.user.id}`) - .setLabel("Claim") - .setStyle(ButtonStyle.Primary), - new ButtonBuilder() - .setCustomId(`reroll`) - .setLabel("Reroll") - .setStyle(ButtonStyle.Secondary)); + const row = CardDropHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id); try { await interaction.editReply({ @@ -76,7 +47,6 @@ export default class Drop extends Command { } catch (e) { console.error(e); - if (e instanceof DiscordAPIError) { await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`); } else { diff --git a/src/commands/stage/dropnumber.ts b/src/commands/stage/dropnumber.ts new file mode 100644 index 0000000..dd42a43 --- /dev/null +++ b/src/commands/stage/dropnumber.ts @@ -0,0 +1,76 @@ +import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js"; +import { Command } from "../../type/command"; +import Card from "../../database/entities/card/Card"; +import { readFileSync } from "fs"; +import Inventory from "../../database/entities/app/Inventory"; +import CardDropHelper from "../../helpers/CardDropHelper"; +import { v4 } from "uuid"; +import { CoreClient } from "../../client/client"; + +export default class Dropnumber extends Command { + constructor() { + super(); + + super.CommandBuilder = new SlashCommandBuilder() + .setName('dropnumber') + .setDescription('(TEST) Summon a specific card') + .addStringOption(x => + x + .setName('cardnumber') + .setDescription('The card number to summon') + .setRequired(true)); + } + + public override async execute(interaction: CommandInteraction) { + if (!interaction.isChatInputCommand()) return; + + const cardNumber = interaction.options.get('cardnumber'); + + if (!cardNumber || !cardNumber.value) { + await interaction.reply('Card Number is required'); + return; + } + + const card = await Card.FetchOneByCardNumber(cardNumber.value.toString(), [ + "Series" + ]); + + if (!card) { + await interaction.reply('Card not found'); + return; + } + + const image = readFileSync(card.Path); + + await interaction.deferReply(); + + const attachment = new AttachmentBuilder(image, { name: card.FileName }); + + const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.CardNumber); + const quantityClaimed = inventory ? inventory.Quantity : 0; + + const embed = CardDropHelper.GenerateDropEmbed(card, quantityClaimed || 0); + + const claimId = v4(); + + const row = CardDropHelper.GenerateDropButtons(card, claimId, interaction.user.id); + + try { + await interaction.editReply({ + embeds: [ embed ], + files: [ attachment ], + components: [ row ], + }); + } catch (e) { + console.error(e); + + if (e instanceof DiscordAPIError) { + await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`); + } else { + await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening.Code: UNKNOWN`); + } + } + + CoreClient.ClaimId = claimId; + } +} \ No newline at end of file diff --git a/src/constants/Environment.ts b/src/constants/Environment.ts index 13f5ba3..410797f 100644 --- a/src/constants/Environment.ts +++ b/src/constants/Environment.ts @@ -5,4 +5,5 @@ export enum Environment { Local = 1 << 2, All = Production | Stage | Local, + Test = Stage | Local, } \ No newline at end of file diff --git a/src/helpers/CardDropHelper.ts b/src/helpers/CardDropHelper.ts index 6eec120..184eab1 100644 --- a/src/helpers/CardDropHelper.ts +++ b/src/helpers/CardDropHelper.ts @@ -1,4 +1,5 @@ -import { CardRarity } from "../constants/CardRarity"; +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js"; +import { CardRarity, CardRarityToColour, CardRarityToString } from "../constants/CardRarity"; import Card from "../database/entities/card/Card"; export default class CardDropHelper { @@ -30,4 +31,30 @@ export default class CardDropHelper { return card; } + + public static GenerateDropEmbed(card: Card, quantityClaimed: Number): EmbedBuilder { + let description = ""; + description += `Series: ${card.Series.Name}\n`; + description += `Claimed: ${quantityClaimed}\n`; + + return new EmbedBuilder() + .setTitle(card.Name) + .setDescription(description) + .setFooter({ text: CardRarityToString(card.Rarity) }) + .setColor(CardRarityToColour(card.Rarity)) + .setImage(`attachment://${card.FileName}`); + } + + public static GenerateDropButtons(card: Card, claimId: string, userId: string): ActionRowBuilder { + return new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`claim ${card.CardNumber} ${claimId} ${userId}`) + .setLabel("Claim") + .setStyle(ButtonStyle.Primary), + new ButtonBuilder() + .setCustomId(`reroll`) + .setLabel("Reroll") + .setStyle(ButtonStyle.Secondary)); + } } \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index c517af3..9685c5d 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -1,15 +1,25 @@ import { CoreClient } from "./client/client"; +// Global Command Imports import About from "./commands/about"; import Drop from "./commands/drop"; +// Test Command Imports +import Dropnumber from "./commands/stage/dropnumber"; + +// Button Event Imports import Claim from "./buttonEvents/Claim"; import Reroll from "./buttonEvents/Reroll"; +import { Environment } from "./constants/Environment"; export default class Registry { public static RegisterCommands() { + // Global Commands CoreClient.RegisterCommand('about', new About()); CoreClient.RegisterCommand('drop', new Drop()); + + // Test Commands + CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test); } public static RegisterEvents() {