From 2809cc1014a0ac9031022f3b2672be1b1a4a66eb Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sun, 3 Sep 2023 20:04:17 +0100 Subject: [PATCH] Add reroll button --- src/buttonEvents/Reroll.ts | 48 ++++++++++++++++++++++++++++++++++++++ src/commands/drop.ts | 8 +++++-- src/registry.ts | 2 ++ 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/buttonEvents/Reroll.ts diff --git a/src/buttonEvents/Reroll.ts b/src/buttonEvents/Reroll.ts new file mode 100644 index 0000000..f1c4bcc --- /dev/null +++ b/src/buttonEvents/Reroll.ts @@ -0,0 +1,48 @@ +import { ActionRowBuilder, AttachmentBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CacheType, EmbedBuilder } 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"; + +export default class Reroll extends ButtonEvent { + public override async execute(interaction: ButtonInteraction) { + if (!interaction.guild || !interaction.guildId) return; + + const randomCard = await CardDropHelper.GetRandomCard(); + + const image = readFileSync(randomCard.Path); + + const attachment = new AttachmentBuilder(image, { name: `${randomCard.Id}.png` }); + + const embed = new EmbedBuilder() + .setTitle(randomCard.Name) + .setDescription(randomCard.Series.Name) + .setFooter({ text: CardRarityToString(randomCard.Rarity) }) + .setColor(CardRarityToColour(randomCard.Rarity)) + .setImage(`attachment://${randomCard.Id}.png`); + + const row = new ActionRowBuilder(); + + const claimId = v4(); + + row.addComponents( + new ButtonBuilder() + .setCustomId(`claim ${randomCard.CardNumber} ${claimId}`) + .setLabel("Claim") + .setStyle(ButtonStyle.Primary), + new ButtonBuilder() + .setCustomId(`reroll`) + .setLabel("Reroll") + .setStyle(ButtonStyle.Secondary)); + + await interaction.reply({ + embeds: [ embed ], + files: [ attachment ], + components: [ row ], + }); + + CoreClient.ClaimId = claimId; + } +} \ No newline at end of file diff --git a/src/commands/drop.ts b/src/commands/drop.ts index 9d24d46..861fb18 100644 --- a/src/commands/drop.ts +++ b/src/commands/drop.ts @@ -37,9 +37,13 @@ export default class Drop extends Command { new ButtonBuilder() .setCustomId(`claim ${randomCard.CardNumber} ${claimId}`) .setLabel("Claim") - .setStyle(ButtonStyle.Primary)); + .setStyle(ButtonStyle.Primary), + new ButtonBuilder() + .setCustomId(`reroll`) + .setLabel("Reroll") + .setStyle(ButtonStyle.Secondary)); - const message = await interaction.reply({ + await interaction.reply({ embeds: [ embed ], files: [ attachment ], components: [ row ], diff --git a/src/registry.ts b/src/registry.ts index 2cc953d..c517af3 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -4,6 +4,7 @@ import About from "./commands/about"; import Drop from "./commands/drop"; import Claim from "./buttonEvents/Claim"; +import Reroll from "./buttonEvents/Reroll"; export default class Registry { public static RegisterCommands() { @@ -17,5 +18,6 @@ export default class Registry { public static RegisterButtonEvents() { CoreClient.RegisterButtonEvent('claim', new Claim()); + CoreClient.RegisterButtonEvent('reroll', new Reroll()); } } \ No newline at end of file