Add dropnumber test command (#69)
All checks were successful
continuous-integration/drone/push Build is passing

# 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 <tester@vylpes.com>
Co-authored-by: Ethan Lane <ethan@vylpes.com>
Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
Ethan Lane 2023-10-27 16:02:02 +01:00 committed by Vylpes
parent 28d745bf5c
commit ec2b523702
9 changed files with 121 additions and 70 deletions

View file

@ -17,7 +17,6 @@ ABOUT_FUNDING=
ABOUT_REPO=
DROP_RARITY=-1
DROP_CARD=-1
DB_HOST=127.0.0.1
DB_PORT=3301

View file

@ -17,7 +17,6 @@ ABOUT_FUNDING=
ABOUT_REPO=
DROP_RARITY=-1
DROP_CARD=-1
DB_HOST=127.0.0.1
DB_PORT=3321

View file

@ -17,7 +17,6 @@ ABOUT_FUNDING=
ABOUT_REPO=
DROP_RARITY=-1
DROP_CARD=-1
DB_HOST=127.0.0.1
DB_PORT=3311

View file

@ -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<ButtonBuilder>();
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 {

View file

@ -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<ButtonBuilder>();
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 {

View file

@ -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<CacheType>) {
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;
}
}

View file

@ -5,4 +5,5 @@ export enum Environment {
Local = 1 << 2,
All = Production | Stage | Local,
Test = Stage | Local,
}

View file

@ -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<ButtonBuilder> {
return new ActionRowBuilder<ButtonBuilder>()
.addComponents(
new ButtonBuilder()
.setCustomId(`claim ${card.CardNumber} ${claimId} ${userId}`)
.setLabel("Claim")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(`reroll`)
.setLabel("Reroll")
.setStyle(ButtonStyle.Secondary));
}
}

View file

@ -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() {