Add dropnumber test command #69

Merged
Vylpes merged 2 commits from feature/39-dropnumber-command into develop 2023-10-27 16:02:02 +01:00
9 changed files with 121 additions and 70 deletions

View file

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

View file

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

View file

@ -17,7 +17,6 @@ ABOUT_FUNDING=
ABOUT_REPO= ABOUT_REPO=
DROP_RARITY=-1 DROP_RARITY=-1
DROP_CARD=-1
DB_HOST=127.0.0.1 DB_HOST=127.0.0.1
DB_PORT=3311 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 { ButtonEvent } from "../type/buttonEvent";
import CardDropHelper from "../helpers/CardDropHelper"; import CardDropHelper from "../helpers/CardDropHelper";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { CardRarityToColour, CardRarityToString } from "../constants/CardRarity";
import { v4 } from "uuid"; import { v4 } from "uuid";
import { CoreClient } from "../client/client"; import { CoreClient } from "../client/client";
import Card from "../database/entities/card/Card"; 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) { if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) {
randomCard = await CardDropHelper.GetRandomCardByRarity(Number(process.env.DROP_RARITY)); 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); 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 inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.CardNumber);
const quantityClaimed = inventory ? inventory.Quantity : 0; const quantityClaimed = inventory ? inventory.Quantity : 0;
let embedDescription = ""; const embed = CardDropHelper.GenerateDropEmbed(randomCard, quantityClaimed || 0);
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 claimId = v4(); const claimId = v4();
row.addComponents( const row = CardDropHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id);
new ButtonBuilder()
.setCustomId(`claim ${randomCard.CardNumber} ${claimId} ${interaction.user.id}`)
.setLabel("Claim")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(`reroll`)
.setLabel("Reroll")
.setStyle(ButtonStyle.Secondary));
try { try {
await interaction.editReply({ await interaction.editReply({
@ -70,7 +41,6 @@ export default class Reroll extends ButtonEvent {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
if (e instanceof DiscordAPIError) { 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}`); await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`);
} else { } 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 { Command } from "../type/command";
import CardDropHelper from "../helpers/CardDropHelper"; import CardDropHelper from "../helpers/CardDropHelper";
import { CardRarityToColour, CardRarityToString } from "../constants/CardRarity";
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { CoreClient } from "../client/client"; import { CoreClient } from "../client/client";
import { v4 } from "uuid"; 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) { if (process.env.DROP_RARITY && Number(process.env.DROP_RARITY) > 0) {
randomCard = await CardDropHelper.GetRandomCardByRarity(Number(process.env.DROP_RARITY)); 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); 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 inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.CardNumber);
const quantityClaimed = inventory ? inventory.Quantity : 0; const quantityClaimed = inventory ? inventory.Quantity : 0;
let embedDescription = ""; const embed = CardDropHelper.GenerateDropEmbed(randomCard, quantityClaimed || 0);
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 claimId = v4(); const claimId = v4();
row.addComponents( const row = CardDropHelper.GenerateDropButtons(randomCard, claimId, interaction.user.id);
new ButtonBuilder()
.setCustomId(`claim ${randomCard.CardNumber} ${claimId} ${interaction.user.id}`)
.setLabel("Claim")
.setStyle(ButtonStyle.Primary),
new ButtonBuilder()
.setCustomId(`reroll`)
.setLabel("Reroll")
.setStyle(ButtonStyle.Secondary));
try { try {
await interaction.editReply({ await interaction.editReply({
@ -76,7 +47,6 @@ export default class Drop extends Command {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
if (e instanceof DiscordAPIError) { 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}`); await interaction.editReply(`Unable to send next drop. Please try again, and report this if it keeps happening. Code: ${e.code}`);
} else { } 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, Local = 1 << 2,
All = Production | Stage | Local, 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"; import Card from "../database/entities/card/Card";
export default class CardDropHelper { export default class CardDropHelper {
@ -30,4 +31,30 @@ export default class CardDropHelper {
return card; 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"; import { CoreClient } from "./client/client";
// Global Command Imports
import About from "./commands/about"; import About from "./commands/about";
import Drop from "./commands/drop"; import Drop from "./commands/drop";
// Test Command Imports
import Dropnumber from "./commands/stage/dropnumber";
// Button Event Imports
import Claim from "./buttonEvents/Claim"; import Claim from "./buttonEvents/Claim";
import Reroll from "./buttonEvents/Reroll"; import Reroll from "./buttonEvents/Reroll";
import { Environment } from "./constants/Environment";
export default class Registry { export default class Registry {
public static RegisterCommands() { public static RegisterCommands() {
// Global Commands
CoreClient.RegisterCommand('about', new About()); CoreClient.RegisterCommand('about', new About());
CoreClient.RegisterCommand('drop', new Drop()); CoreClient.RegisterCommand('drop', new Drop());
// Test Commands
CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test);
} }
public static RegisterEvents() { public static RegisterEvents() {