Update gdrivesync to show an error message of what failed #144

Merged
Vylpes merged 1 commit from feature/134-json-failure-2 into develop 2024-01-20 14:31:07 +00:00
3 changed files with 60 additions and 21 deletions
Showing only changes of commit 02d2b51171 - Show all commits

View file

@ -5,37 +5,74 @@ import { glob } from "glob";
import { SeriesMetadata } from "../contracts/SeriesMetadata"; import { SeriesMetadata } from "../contracts/SeriesMetadata";
import { CoreClient } from "../client/client"; import { CoreClient } from "../client/client";
export interface CardMetadataResult {
IsSuccess: boolean;
ErrorMessage?: string;
}
export interface FindMetadataResult {
IsSuccess: boolean;
Result?: SeriesMetadata[];
Error?: {
File: string;
Message: string;
};
}
export default class CardMetadataFunction { export default class CardMetadataFunction {
public static async Execute(overrideSafeMode: boolean = false): Promise<boolean> { public static async Execute(overrideSafeMode: boolean = false): Promise<CardMetadataResult> {
if (!overrideSafeMode && await Config.GetValue("safemode") == "true") return false; if (!overrideSafeMode && await Config.GetValue("safemode") == "true") return {
IsSuccess: false,
ErrorMessage: "Safe mode is on and not overridden",
};
try { const cardResult = await this.FindMetadataJSONs();
CoreClient.Cards = await this.FindMetadataJSONs();
if (cardResult.IsSuccess) {
CoreClient.Cards = cardResult.Result!;
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 (e) {
console.error(e);
await Config.SetValue("safemode", "true"); return {
return false; IsSuccess: true,
};
} }
return true; await Config.SetValue("safemode", "true");
return {
IsSuccess: false,
ErrorMessage: `${cardResult.Error!.File}: ${cardResult.Error!.Message}`,
};
} }
private static async FindMetadataJSONs(): Promise<SeriesMetadata[]> { private static async FindMetadataJSONs(): Promise<FindMetadataResult> {
const res: SeriesMetadata[] = []; const res: SeriesMetadata[] = [];
const seriesJSONs = await glob(path.join(process.env.DATA_DIR!, "cards", "/**/*.json")); const seriesJSONs = await glob(path.join(process.env.DATA_DIR!, "cards", "/**/*.json"));
for (const jsonPath of seriesJSONs) { for (const jsonPath of seriesJSONs) {
console.log(`Reading file ${jsonPath}`); try {
const jsonFile = readFileSync(jsonPath); console.log(`Reading file ${jsonPath}`);
const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString()); const jsonFile = readFileSync(jsonPath);
const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString());
res.push(...parsedJson); res.push(...parsedJson);
} catch (e) {
console.error(e);
return {
IsSuccess: false,
Error: {
File: jsonPath,
Message: `${e}`,
}
};
}
} }
return res; return {
IsSuccess: true,
Result: res,
};
} }
} }

View file

@ -25,7 +25,10 @@ export default class Gdrivesync extends Command {
return; return;
} }
await interaction.reply("Syncing, this might take a while..."); await interaction.reply({
content: "Syncing, this might take a while...",
ephemeral: true,
});
CoreClient.AllowDrops = false; CoreClient.AllowDrops = false;
@ -34,17 +37,15 @@ 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 {
const result = await CardMetadataFunction.Execute(); const result = await CardMetadataFunction.Execute(true);
if (result) { if (result.IsSuccess) {
await interaction.editReply("Synced successfully."); await interaction.editReply("Synced successfully.");
CoreClient.AllowDrops = true; CoreClient.AllowDrops = true;
await Config.SetValue("safemode", "false"); await Config.SetValue("safemode", "false");
} else { } else {
const safemode = await Config.GetValue("safemode"); await interaction.editReply(`Sync failed \`\`\`${result.ErrorMessage}\`\`\``);
await interaction.editReply(`Sync failed. ${safemode == "true" ? "(Safe Mode is on)": "(Safe Mode is off)"}`);
} }
} }
}); });

View file

@ -1,5 +1,6 @@
export default class EmbedColours { export default class EmbedColours {
public static readonly Ok = 0x3050ba; public static readonly Ok = 0x3050ba;
public static readonly Error = 0xff0000;
public static readonly Grey = 0xd3d3d3; public static readonly Grey = 0xd3d3d3;
public static readonly BronzeCard = 0xcd7f32; public static readonly BronzeCard = 0xcd7f32;
public static readonly SilverCard = 0xc0c0c0; public static readonly SilverCard = 0xc0c0c0;