Ethan Lane
5f513d740e
# 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. - Update the sacrifice command so that the embed starts red, goes grey if cancelled, and goes green if successful - Fix event not checking if the user who ran the command is the same user who clicked the button - Remove duplicated card name in the embed, didn't need it in the footer as well as the main body #203, #230, #238 ## 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. # Checklist - [ ] My code follows the style guidelines of this project - [ ] 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 - [ ] 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 Co-authored-by: VylpesTester <tester@vylpes.com> Reviewed-on: #237 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
157 lines
No EOL
5.5 KiB
TypeScript
157 lines
No EOL
5.5 KiB
TypeScript
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder } from "discord.js";
|
|
import { ButtonEvent } from "../type/buttonEvent";
|
|
import Inventory from "../database/entities/app/Inventory";
|
|
import CardDropHelperMetadata from "../helpers/CardDropHelperMetadata";
|
|
import { CardRarityToString, GetSacrificeAmount } from "../constants/CardRarity";
|
|
import EmbedColours from "../constants/EmbedColours";
|
|
import User from "../database/entities/app/User";
|
|
|
|
export default class Sacrifice extends ButtonEvent {
|
|
public override async execute(interaction: ButtonInteraction) {
|
|
const subcommand = interaction.customId.split(" ")[1];
|
|
|
|
switch(subcommand) {
|
|
case "confirm":
|
|
await this.confirm(interaction);
|
|
break;
|
|
case "cancel":
|
|
await this.cancel(interaction);
|
|
break;
|
|
}
|
|
}
|
|
|
|
private async confirm(interaction: ButtonInteraction) {
|
|
const userId = interaction.customId.split(" ")[2];
|
|
const cardNumber = interaction.customId.split(" ")[3];
|
|
|
|
if (userId != interaction.user.id) {
|
|
await interaction.reply("Only the user who created this sacrifice can confirm it.");
|
|
return;
|
|
}
|
|
|
|
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
|
|
|
|
if (!cardInInventory) {
|
|
await interaction.reply("Unable to find card in inventory.");
|
|
return;
|
|
}
|
|
|
|
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
|
|
|
|
if (!cardData) {
|
|
await interaction.reply("Unable to find card in the database.");
|
|
return;
|
|
}
|
|
|
|
const user = await User.FetchOneById(User, userId);
|
|
|
|
if (!user) {
|
|
await interaction.reply("Unable to find user in database.");
|
|
return;
|
|
}
|
|
|
|
cardInInventory.RemoveQuantity(1);
|
|
|
|
await cardInInventory.Save(Inventory, cardInInventory);
|
|
|
|
const cardValue = GetSacrificeAmount(cardData.card.type);
|
|
const cardRarityString = CardRarityToString(cardData.card.type);
|
|
|
|
user.AddCurrency(cardValue);
|
|
|
|
await user.Save(User, user);
|
|
|
|
const description = [
|
|
`Card: ${cardData.card.name}`,
|
|
`Series: ${cardData.series.name}`,
|
|
`Rarity: ${cardRarityString}`,
|
|
`Quantity Owned: ${cardInInventory.Quantity}`,
|
|
`Sacrifice Amount: ${cardValue}`,
|
|
];
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setTitle("Card Sacrificed")
|
|
.setDescription(description.join("\n"))
|
|
.setColor(EmbedColours.Green)
|
|
.setFooter({ text: `${interaction.user.username}` });
|
|
|
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
|
.addComponents([
|
|
new ButtonBuilder()
|
|
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardNumber}`)
|
|
.setLabel("Confirm")
|
|
.setStyle(ButtonStyle.Success)
|
|
.setDisabled(true),
|
|
new ButtonBuilder()
|
|
.setCustomId("sacrifice cancel")
|
|
.setLabel("Cancel")
|
|
.setStyle(ButtonStyle.Secondary)
|
|
.setDisabled(true),
|
|
]);
|
|
|
|
await interaction.update({
|
|
embeds: [ embed ],
|
|
components: [ row ],
|
|
});
|
|
}
|
|
|
|
private async cancel(interaction: ButtonInteraction) {
|
|
const userId = interaction.customId.split(" ")[2];
|
|
const cardNumber = interaction.customId.split(" ")[3];
|
|
|
|
if (userId != interaction.user.id) {
|
|
await interaction.reply("Only the user who created this sacrifice can cancel it.");
|
|
return;
|
|
}
|
|
|
|
const cardInInventory = await Inventory.FetchOneByCardNumberAndUserId(userId, cardNumber);
|
|
|
|
if (!cardInInventory) {
|
|
await interaction.reply("Unable to find card in inventory.");
|
|
return;
|
|
}
|
|
|
|
const cardData = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
|
|
|
|
if (!cardData) {
|
|
await interaction.reply("Unable to find card in the database.");
|
|
return;
|
|
}
|
|
|
|
const cardValue = GetSacrificeAmount(cardData.card.type);
|
|
const cardRarityString = CardRarityToString(cardData.card.type);
|
|
|
|
const description = [
|
|
`Card: ${cardData.card.name}`,
|
|
`Series: ${cardData.series.name}`,
|
|
`Rarity: ${cardRarityString}`,
|
|
`Quantity Owned: ${cardInInventory.Quantity}`,
|
|
`Sacrifice Amount: ${cardValue}`,
|
|
];
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setTitle("Sacrifice Cancelled")
|
|
.setDescription(description.join("\n"))
|
|
.setColor(EmbedColours.Grey)
|
|
.setFooter({ text: `${interaction.user.username}` });
|
|
|
|
const row = new ActionRowBuilder<ButtonBuilder>()
|
|
.addComponents([
|
|
new ButtonBuilder()
|
|
.setCustomId(`sacrifice confirm ${interaction.user.id} ${cardNumber}`)
|
|
.setLabel("Confirm")
|
|
.setStyle(ButtonStyle.Success)
|
|
.setDisabled(true),
|
|
new ButtonBuilder()
|
|
.setCustomId("sacrifice cancel")
|
|
.setLabel("Cancel")
|
|
.setStyle(ButtonStyle.Secondary)
|
|
.setDisabled(true),
|
|
]);
|
|
|
|
await interaction.update({
|
|
embeds: [ embed ],
|
|
components: [ row ],
|
|
});
|
|
}
|
|
} |