Create sacrifice button handler
This commit is contained in:
parent
ee696a5111
commit
6e7e4a15ac
1 changed files with 90 additions and 1 deletions
|
@ -8,6 +8,7 @@ import { readFileSync } from "fs";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import ErrorMessages from "../constants/ErrorMessages";
|
import ErrorMessages from "../constants/ErrorMessages";
|
||||||
import User from "../database/entities/app/User";
|
import User from "../database/entities/app/User";
|
||||||
|
import { GetSacrificeAmount } from "../constants/CardRarity";
|
||||||
|
|
||||||
export default class Multidrop extends ButtonEvent {
|
export default class Multidrop extends ButtonEvent {
|
||||||
public override async execute(interaction: ButtonInteraction) {
|
public override async execute(interaction: ButtonInteraction) {
|
||||||
|
@ -17,13 +18,16 @@ export default class Multidrop extends ButtonEvent {
|
||||||
case "keep":
|
case "keep":
|
||||||
await this.Keep(interaction);
|
await this.Keep(interaction);
|
||||||
break;
|
break;
|
||||||
|
case "sacrifice":
|
||||||
|
await this.Sacrifice(interaction);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
await interaction.reply("Invalid action");
|
await interaction.reply("Invalid action");
|
||||||
AppLogger.LogError("Button/Multidrop", `Invalid action, ${action}`);
|
AppLogger.LogError("Button/Multidrop", `Invalid action, ${action}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Keep(interaction: ButtonInteraction) {
|
private async Keep(interaction: ButtonInteraction) {
|
||||||
const cardNumber = interaction.customId.split(" ")[2];
|
const cardNumber = interaction.customId.split(" ")[2];
|
||||||
let cardsRemaining = Number(interaction.customId.split(" ")[3]) || 0;
|
let cardsRemaining = Number(interaction.customId.split(" ")[3]) || 0;
|
||||||
const userId = interaction.customId.split(" ")[4];
|
const userId = interaction.customId.split(" ")[4];
|
||||||
|
@ -111,4 +115,89 @@ export default class Multidrop extends ButtonEvent {
|
||||||
await interaction.reply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`);
|
await interaction.reply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Sacrifice(interaction: ButtonInteraction) {
|
||||||
|
const cardNumber = interaction.customId.split(" ")[2];
|
||||||
|
let cardsRemaining = Number(interaction.customId.split(" ")[3]) || 0;
|
||||||
|
const userId = interaction.customId.split(" ")[4];
|
||||||
|
|
||||||
|
if (interaction.user.id != userId) {
|
||||||
|
await interaction.reply("You're not the user this drop was made for!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const card = CardDropHelperMetadata.GetCardByCardNumber(cardNumber);
|
||||||
|
|
||||||
|
if (!card) {
|
||||||
|
await interaction.reply("Unable to find card.");
|
||||||
|
AppLogger.LogWarn("Button/Multidrop/Sacrifice", `Card not found, ${cardNumber}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cardsRemaining < 0) {
|
||||||
|
await interaction.reply("Your multidrop has ran out! Please buy a new one!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const user = await User.FetchOneById(User, interaction.user.id);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
AppLogger.LogWarn("Button/Multidrop/Sacrifice", ErrorMessages.UnableToFetchUser);
|
||||||
|
await interaction.reply(ErrorMessages.UnableToFetchUser);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sacrifice
|
||||||
|
const sacrificeAmount = GetSacrificeAmount(card.card.type);
|
||||||
|
|
||||||
|
user.AddCurrency(sacrificeAmount);
|
||||||
|
|
||||||
|
await user.Save(User, user);
|
||||||
|
|
||||||
|
// Pack has ran out
|
||||||
|
if (cardsRemaining == 0) {
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
|
.setDescription("Your multidrop has ran out! Please buy a new one!")
|
||||||
|
.setColor(EmbedColours.Ok);
|
||||||
|
|
||||||
|
await interaction.update({ embeds: [ embed ]});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Drop next card
|
||||||
|
const randomCard = CardDropHelperMetadata.GetRandomCard();
|
||||||
|
cardsRemaining -= 1;
|
||||||
|
|
||||||
|
if (!randomCard) {
|
||||||
|
AppLogger.LogWarn("Button/Multidrop/Keep", ErrorMessages.UnableToFetchCard);
|
||||||
|
await interaction.reply(ErrorMessages.UnableToFetchCard);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await interaction.deferUpdate();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", ));
|
||||||
|
const imageFileName = randomCard.card.path.split("/").pop()!;
|
||||||
|
|
||||||
|
const attachment = new AttachmentBuilder(image, { name: imageFileName });
|
||||||
|
|
||||||
|
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
|
||||||
|
const quantityClaimed = inventory ? inventory.Quantity : 0;
|
||||||
|
|
||||||
|
const embed = CardDropHelperMetadata.GenerateMultidropEmbed(randomCard, quantityClaimed, imageFileName, cardsRemaining, undefined, user.Currency);
|
||||||
|
|
||||||
|
const row = CardDropHelperMetadata.GenerateMultidropButtons(randomCard, cardsRemaining, interaction.user.id, cardsRemaining < 0);
|
||||||
|
|
||||||
|
await interaction.update({
|
||||||
|
embeds: [ embed ],
|
||||||
|
files: [ attachment ],
|
||||||
|
components: [ row ],
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
AppLogger.LogError("Button/Multidrop/Keep", `Error sending next drop for card ${randomCard.card.id}: ${e}`);
|
||||||
|
|
||||||
|
await interaction.reply(`Unable to send next drop. Please try again, and report this if it keeps happening. (${randomCard.card.id})`);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue