Merge branch 'develop' into feature/CD-415

This commit is contained in:
Ethan Lane 2025-04-24 19:15:08 +01:00
commit 01dec14b6c
9 changed files with 264 additions and 93 deletions

View file

@ -86,4 +86,12 @@ export default class AppLogger {
public static LogSilly(label: string, message: string) {
AppLogger.Logger.silly({ label, message });
}
public static CatchError(label: string, error: unknown) {
if (error instanceof Error) {
AppLogger.Logger.error({ label, message: error.message });
} else {
AppLogger.Logger.error({ label, message: error });
}
}
}

View file

@ -60,13 +60,18 @@ export default class Series extends Command {
return;
}
const embed = await SeriesHelper.GenerateSeriesViewPage(series.id, 0, interaction.user.id);
try {
const embed = await SeriesHelper.GenerateSeriesViewPage(series.id, 0, interaction.user.id);
await interaction.followUp({
embeds: [ embed!.embed ],
components: [ embed!.row ],
files: [ embed!.image ],
});
await interaction.followUp({
embeds: [ embed!.embed ],
components: [ embed!.row ],
files: [ embed!.image ],
});
} catch (e) {
await interaction.followUp("An error has occured generating the series grid.");
AppLogger.CatchError("Series", e);
}
}
private async ListSeries(interaction: CommandInteraction) {

View file

@ -1,4 +1,4 @@
import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
import { AttachmentBuilder, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js";
import { Command } from "../../type/command";
import { readFileSync } from "fs";
import Inventory from "../../database/entities/app/Inventory";
@ -6,6 +6,7 @@ import { v4 } from "uuid";
import { CoreClient } from "../../client/client";
import path from "path";
import DropEmbedHelper from "../../helpers/DropHelpers/DropEmbedHelper";
import AppLogger from "../../client/appLogger";
export default class Dropnumber extends Command {
constructor() {
@ -40,9 +41,10 @@ export default class Dropnumber extends Command {
return;
}
const series = CoreClient.Cards
.find(x => x.cards.includes(card))!;
const claimId = v4();
await interaction.deferReply();
try {
const files = [];
let imageFileName = "";
@ -55,31 +57,24 @@ export default class Dropnumber extends Command {
files.push(attachment);
}
await interaction.deferReply();
const series = CoreClient.Cards
.find(x => x.cards.includes(card))!;
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = DropEmbedHelper.GenerateDropEmbed({ card, series }, quantityClaimed, imageFileName);
const claimId = v4();
const row = DropEmbedHelper.GenerateDropButtons({ card, series }, claimId, interaction.user.id);
try {
await interaction.editReply({
embeds: [ embed ],
files: files,
components: [ row ],
});
await interaction.editReply({
embeds: [ embed ],
files: files,
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");
}
AppLogger.CatchError("Dropnumber", e);
await interaction.editReply("Unable to send next drop. Please try again, and report this if it keeps happening");
}
}
}

View file

@ -1,6 +1,6 @@
import { AttachmentBuilder, CacheType, CommandInteraction, DiscordAPIError, SlashCommandBuilder } from "discord.js";
import { AttachmentBuilder, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js";
import { Command } from "../../type/command";
import { CardRarity, CardRarityParse } from "../../constants/CardRarity";
import { CardRarity, CardRarityChoices, CardRarityParse } from "../../constants/CardRarity";
import { readFileSync } from "fs";
import Inventory from "../../database/entities/app/Inventory";
import { v4 } from "uuid";
@ -8,6 +8,7 @@ import { CoreClient } from "../../client/client";
import path from "path";
import GetCardsHelper from "../../helpers/DropHelpers/GetCardsHelper";
import DropEmbedHelper from "../../helpers/DropHelpers/DropEmbedHelper";
import AppLogger from "../../client/appLogger";
export default class Droprarity extends Command {
constructor() {
@ -20,7 +21,8 @@ export default class Droprarity extends Command {
x
.setName("rarity")
.setDescription("The rarity you want to summon")
.setRequired(true));
.setRequired(true)
.setChoices(CardRarityChoices));
}
public override async execute(interaction: CommandInteraction<CacheType>) {
@ -47,41 +49,37 @@ export default class Droprarity extends Command {
return;
}
const files = [];
let imageFileName = "";
if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) {
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path));
imageFileName = card.card.path.split("/").pop()!;
const attachment = new AttachmentBuilder(image, { name: imageFileName });
files.push(attachment);
}
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = DropEmbedHelper.GenerateDropEmbed(card, quantityClaimed, imageFileName);
const claimId = v4();
const row = DropEmbedHelper.GenerateDropButtons(card, claimId, interaction.user.id);
await interaction.deferReply();
try {
const files = [];
let imageFileName = "";
if (!(card.card.path.startsWith("http://") || card.card.path.startsWith("https://"))) {
const image = readFileSync(path.join(process.env.DATA_DIR!, "cards", card.card.path));
imageFileName = card.card.path.split("/").pop()!;
const attachment = new AttachmentBuilder(image, { name: imageFileName });
files.push(attachment);
}
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, card.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = DropEmbedHelper.GenerateDropEmbed(card, quantityClaimed, imageFileName);
const row = DropEmbedHelper.GenerateDropButtons(card, claimId, interaction.user.id);
await interaction.editReply({
embeds: [ embed ],
files: files,
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");
}
AppLogger.CatchError("Droprarity", e);
await interaction.editReply("Unable to send next drop. Please try again, and report this if it keeps happening");
}
}
}

View file

@ -9,6 +9,29 @@ export enum CardRarity {
Legendary,
}
export const CardRarityChoices = [
{
name: "Bronze",
value: "bronze",
},
{
name: "Silver",
value: "silver",
},
{
name: "Gold",
value: "gold",
},
{
name: "Manga",
value: "manga",
},
{
name: "Legendary",
value: "legendary",
},
];
export function CardRarityToString(rarity: CardRarity): string {
switch (rarity) {
case CardRarity.Unknown:

View file

@ -3,7 +3,8 @@ import path from "path";
import AppLogger from "../client/appLogger";
import {existsSync} from "fs";
import Inventory from "../database/entities/app/Inventory";
import {Jimp} from "jimp";
import { Bitmap, Jimp } from "jimp";
import axios from "axios";
interface CardInput {
id: string;
@ -25,36 +26,51 @@ export default class ImageHelper {
const ctx = canvas.getContext("2d");
for (let i = 0; i < cards.length; i++) {
const card = cards[i];
try {
const card = cards[i];
const filePath = path.join(process.env.DATA_DIR!, "cards", card.path);
const filePath = path.join(process.env.DATA_DIR!, "cards", card.path);
const exists = existsSync(filePath);
let bitmap: Bitmap;
if (!exists) {
AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`);
continue;
}
if (existsSync(filePath)) {
const data = await Jimp.read(filePath);
const imageData = await Jimp.read(filePath);
bitmap = data.bitmap;
} else if (card.path.startsWith("http://") || card.path.startsWith("https://")) {
const response = await axios.get(card.path, { responseType: "arraybuffer" });
const buffer = Buffer.from(response.data);
const data = await Jimp.fromBuffer(buffer);
if (userId != null) {
const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id);
if (!claimed || claimed.Quantity == 0) {
imageData.greyscale();
bitmap = data.bitmap;
} else {
AppLogger.LogError("ImageHelper/GenerateCardImageGrid", `Failed to load image from path ${card.path}`);
continue;
}
const imageData = Jimp.fromBitmap(bitmap);
if (userId != null) {
const claimed = await Inventory.FetchOneByCardNumberAndUserId(userId, card.id);
if (!claimed || claimed.Quantity == 0) {
imageData.greyscale();
}
}
const image = await loadImage(await imageData.getBuffer("image/png"));
const x = i % gridWidth;
const y = Math.floor(i / gridWidth);
const imageX = imageWidth * x;
const imageY = imageHeight * y;
ctx.drawImage(image, imageX, imageY);
}
catch (e) {
AppLogger.CatchError("ImageHelper", e);
}
const image = await loadImage(await imageData.getBuffer("image/png"));
const x = i % gridWidth;
const y = Math.floor(i / gridWidth);
const imageX = imageWidth * x;
const imageY = imageHeight * y;
ctx.drawImage(image, imageX, imageY);
}
return canvas.toBuffer();