Update gdrivesync to show an error message of what failed
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2024-01-20 14:29:05 +00:00
parent 275a804e28
commit 02d2b51171
3 changed files with 60 additions and 21 deletions

View file

@ -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<boolean> {
if (!overrideSafeMode && await Config.GetValue("safemode") == "true") return false;
public static async Execute(overrideSafeMode: boolean = false): Promise<CardMetadataResult> {
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<SeriesMetadata[]> {
private static async FindMetadataJSONs(): Promise<FindMetadataResult> {
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,
};
}
}

View file

@ -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}\`\`\``);
}
}
});

View file

@ -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;