Compare commits

...

2 commits

Author SHA1 Message Date
9bf145c4e3 Fix image issues
Some checks failed
continuous-integration/drone/push Build is failing
2023-12-08 18:19:43 +00:00
8aa2794635 Add feature flag to sync commands 2023-12-08 17:19:28 +00:00
8 changed files with 51 additions and 17 deletions

View file

@ -6,14 +6,16 @@ import SeriesMetadata from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client"; import { CoreClient } from "../client/client";
export default class CardMetadataFunction { export default class CardMetadataFunction {
public static async Execute(): Promise<boolean> { public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> {
if (await Config.GetValue('safemode') == "true") return false; if (!overrideSafeMode && await Config.GetValue('safemode') == "true") return false;
try { try {
CoreClient.Cards = await this.FindMetadataJSONs(); CoreClient.Cards = await this.FindMetadataJSONs();
console.log(`Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`); console.log(`Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`);
} catch { } catch (e) {
console.error(e);
await Config.SetValue('safemode', 'true'); await Config.SetValue('safemode', 'true');
return false; return false;
} }
@ -27,6 +29,7 @@ export default class CardMetadataFunction {
const seriesJSONs = await glob(path.join(process.cwd(), 'cards', '/**/*.json')); const seriesJSONs = await glob(path.join(process.cwd(), 'cards', '/**/*.json'));
for (let jsonPath of seriesJSONs) { for (let jsonPath of seriesJSONs) {
console.log(`Reading file ${jsonPath}`);
const jsonFile = readFileSync(jsonPath); const jsonFile = readFileSync(jsonPath);
const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString()); const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString());

View file

@ -7,8 +7,8 @@ import { CardRarity, CardRarityToString } from "../constants/CardRarity";
import Config from "../database/entities/app/Config"; import Config from "../database/entities/app/Config";
export default class CardSetupFunction { export default class CardSetupFunction {
public static async Execute(): Promise<boolean> { public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> {
if (await Config.GetValue('safemode') == "true") return false; if (!overrideSafeMode && await Config.GetValue('safemode') == "true") return false;
try { try {
await this.ClearDatabase(); await this.ClearDatabase();

View file

@ -33,16 +33,24 @@ export default class Reroll extends ButtonEvent {
return; return;
} }
const image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path)); let image: Buffer;
const imageFileName = randomCard.card.path.split("/").pop()!;
try {
image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
} catch {
await interaction.reply(`Unable to fetch image for card ${randomCard.card.id}`);
return;
}
await interaction.deferReply(); await interaction.deferReply();
const attachment = new AttachmentBuilder(image, { name: randomCard.card.id }); const attachment = new AttachmentBuilder(image, { name: imageFileName });
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id); const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0; const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed); const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
const claimId = v4(); const claimId = v4();

View file

@ -79,9 +79,9 @@ export class CoreClient extends Client {
super.on("ready", this._events.onReady); super.on("ready", this._events.onReady);
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') { if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
await CardMetadataFunction.Execute(); await CardMetadataFunction.Execute(true);
} else { } else {
await CardSetupFunction.Execute(); await CardSetupFunction.Execute(true);
} }
this._util.loadEvents(this, CoreClient._eventItems); this._util.loadEvents(this, CoreClient._eventItems);

View file

@ -41,16 +41,24 @@ export default class Drop extends Command {
return; return;
} }
const image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path)); let image: Buffer;
const imageFileName = randomCard.card.path.split("/").pop()!;
try {
image = readFileSync(path.join(process.cwd(), 'cards', randomCard.card.path));
} catch {
await interaction.reply(`Unable to fetch image for card ${randomCard.card.id}`);
return;
}
await interaction.deferReply(); await interaction.deferReply();
const attachment = new AttachmentBuilder(image, { name: randomCard.card.id }); const attachment = new AttachmentBuilder(image, { name: imageFileName });
const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id); const inventory = await Inventory.FetchOneByCardNumberAndUserId(interaction.user.id, randomCard.card.id);
const quantityClaimed = inventory ? inventory.Quantity : 0; const quantityClaimed = inventory ? inventory.Quantity : 0;
const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed); const embed = CardDropHelperMetadata.GenerateDropEmbed(randomCard, quantityClaimed, imageFileName);
const claimId = v4(); const claimId = v4();

View file

@ -4,6 +4,7 @@ import { ExecException, exec } from "child_process";
import CardSetupFunction from "../Functions/CardSetupFunction"; import CardSetupFunction from "../Functions/CardSetupFunction";
import { CoreClient } from "../client/client"; import { CoreClient } from "../client/client";
import Config from "../database/entities/app/Config"; import Config from "../database/entities/app/Config";
import CardMetadataFunction from "../Functions/CardMetadataFunction";
export default class Gdrivesync extends Command { export default class Gdrivesync extends Command {
constructor() { constructor() {
@ -34,7 +35,12 @@ export default class Gdrivesync extends Command {
await interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`); await interaction.editReply(`Error while running sync command. Safe Mode has been activated. Code: ${error.code}`);
await Config.SetValue('safemode', 'true'); await Config.SetValue('safemode', 'true');
} else { } else {
await CardSetupFunction.Execute(); if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
await CardMetadataFunction.Execute();
} else {
await CardSetupFunction.Execute();
}
await interaction.editReply('Synced successfully.'); await interaction.editReply('Synced successfully.');
CoreClient.AllowDrops = true; CoreClient.AllowDrops = true;

View file

@ -2,6 +2,7 @@ import { CacheType, CommandInteraction, PermissionsBitField, SlashCommandBuilder
import { Command } from "../type/command"; import { Command } from "../type/command";
import CardSetupFunction from "../Functions/CardSetupFunction"; import CardSetupFunction from "../Functions/CardSetupFunction";
import Config from "../database/entities/app/Config"; import Config from "../database/entities/app/Config";
import CardMetadataFunction from "../Functions/CardMetadataFunction";
export default class Resync extends Command { export default class Resync extends Command {
constructor() { constructor() {
@ -23,7 +24,15 @@ export default class Resync extends Command {
return; return;
} }
if (await CardSetupFunction.Execute()) { let result: boolean;
if (process.env.FEATURE_METADATA && process.env.FEATURE_METADATA == 'true') {
result = await CardMetadataFunction.Execute(true);
} else {
result = await CardSetupFunction.Execute(true);
}
if (result) {
if (await Config.GetValue('safemode') == "true") { if (await Config.GetValue('safemode') == "true") {
await Config.SetValue('safemode', 'false'); await Config.SetValue('safemode', 'false');
await interaction.reply("Resynced database and disabled safe mode."); await interaction.reply("Resynced database and disabled safe mode.");

View file

@ -48,7 +48,7 @@ export default class CardDropHelperMetadata {
}; };
} }
public static GenerateDropEmbed(drop: DropResult, quantityClaimed: Number): EmbedBuilder { public static GenerateDropEmbed(drop: DropResult, quantityClaimed: Number, imageFileName: string): EmbedBuilder {
let description = ""; let description = "";
description += `Series: ${drop.series.name}\n`; description += `Series: ${drop.series.name}\n`;
description += `Claimed: ${quantityClaimed}\n`; description += `Claimed: ${quantityClaimed}\n`;
@ -58,7 +58,7 @@ export default class CardDropHelperMetadata {
.setDescription(description) .setDescription(description)
.setFooter({ text: CardRarityToString(drop.card.type) }) .setFooter({ text: CardRarityToString(drop.card.type) })
.setColor(CardRarityToColour(drop.card.type)) .setColor(CardRarityToColour(drop.card.type))
.setImage(`attachment://${drop.card.id}`); .setImage(`attachment://${imageFileName}`);
} }
public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string): ActionRowBuilder<ButtonBuilder> { public static GenerateDropButtons(drop: DropResult, claimId: string, userId: string): ActionRowBuilder<ButtonBuilder> {