From de08fa26d77de28d4eb797acafaee4719daed6c7 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 20 Jan 2024 14:31:07 +0000 Subject: [PATCH] Update gdrivesync to show an error message of what failed (#144) # 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. Fixes # (issue) ## Type of change Please delete options that are not relevant. - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] This change requires a documentation update # 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 Reviewed-on: https://gitea.vylpes.xyz/External/card-drop/pulls/144 Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/Functions/CardMetadataFunction.ts | 67 +++++++++++++++++++++------ src/commands/gdrivesync.ts | 13 +++--- src/constants/EmbedColours.ts | 1 + 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/Functions/CardMetadataFunction.ts b/src/Functions/CardMetadataFunction.ts index 99a162e..3a8b97a 100644 --- a/src/Functions/CardMetadataFunction.ts +++ b/src/Functions/CardMetadataFunction.ts @@ -5,37 +5,74 @@ import { glob } from "glob"; import { SeriesMetadata } from "../contracts/SeriesMetadata"; 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 { - public static async Execute(overrideSafeMode: boolean = false): Promise { - if (!overrideSafeMode && await Config.GetValue("safemode") == "true") return false; + public static async Execute(overrideSafeMode: boolean = false): Promise { + if (!overrideSafeMode && await Config.GetValue("safemode") == "true") return { + IsSuccess: false, + ErrorMessage: "Safe mode is on and not overridden", + }; - try { - CoreClient.Cards = await this.FindMetadataJSONs(); + const cardResult = await this.FindMetadataJSONs(); + if (cardResult.IsSuccess) { + CoreClient.Cards = cardResult.Result!; console.log(`Loaded ${CoreClient.Cards.flatMap(x => x.cards).length} cards to database`); - } catch (e) { - console.error(e); - await Config.SetValue("safemode", "true"); - return false; + return { + IsSuccess: true, + }; } - return true; + await Config.SetValue("safemode", "true"); + + return { + IsSuccess: false, + ErrorMessage: `${cardResult.Error!.File}: ${cardResult.Error!.Message}`, + }; } - private static async FindMetadataJSONs(): Promise { + private static async FindMetadataJSONs(): Promise { const res: SeriesMetadata[] = []; const seriesJSONs = await glob(path.join(process.env.DATA_DIR!, "cards", "/**/*.json")); for (const jsonPath of seriesJSONs) { - console.log(`Reading file ${jsonPath}`); - const jsonFile = readFileSync(jsonPath); - const parsedJson: SeriesMetadata[] = JSON.parse(jsonFile.toString()); + try { + console.log(`Reading file ${jsonPath}`); + 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, + }; } } \ No newline at end of file diff --git a/src/commands/gdrivesync.ts b/src/commands/gdrivesync.ts index 35cf9cd..c8a393c 100644 --- a/src/commands/gdrivesync.ts +++ b/src/commands/gdrivesync.ts @@ -25,7 +25,10 @@ export default class Gdrivesync extends Command { 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; @@ -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 Config.SetValue("safemode", "true"); } else { - const result = await CardMetadataFunction.Execute(); + const result = await CardMetadataFunction.Execute(true); - if (result) { + if (result.IsSuccess) { await interaction.editReply("Synced successfully."); CoreClient.AllowDrops = true; await Config.SetValue("safemode", "false"); } else { - const safemode = await Config.GetValue("safemode"); - - await interaction.editReply(`Sync failed. ${safemode == "true" ? "(Safe Mode is on)": "(Safe Mode is off)"}`); + await interaction.editReply(`Sync failed \`\`\`${result.ErrorMessage}\`\`\``); } } }); diff --git a/src/constants/EmbedColours.ts b/src/constants/EmbedColours.ts index 6094dbf..a54d56f 100644 --- a/src/constants/EmbedColours.ts +++ b/src/constants/EmbedColours.ts @@ -1,5 +1,6 @@ export default class EmbedColours { public static readonly Ok = 0x3050ba; + public static readonly Error = 0xff0000; public static readonly Grey = 0xd3d3d3; public static readonly BronzeCard = 0xcd7f32; public static readonly SilverCard = 0xc0c0c0;