Update inventory command so it errors gracefully if no page is found for the user #286

Merged
Vylpes merged 1 commit from feature/251-inventory-checks into develop 2024-07-06 19:12:22 +01:00
3 changed files with 16 additions and 5 deletions
Showing only changes of commit 51a5d20ce1 - Show all commits

View file

@ -26,6 +26,11 @@ export default class Inventory extends ButtonEvent {
const embed = await InventoryHelper.GenerateInventoryPage(member.user.username, member.user.id, Number(page)); const embed = await InventoryHelper.GenerateInventoryPage(member.user.username, member.user.id, Number(page));
if (!embed) {
await interaction.followUp("No page for user found.");
return;
}
await interaction.followUp({ await interaction.followUp({
files: [ embed.image ], files: [ embed.image ],
embeds: [ embed.embed ], embeds: [ embed.embed ],
@ -34,7 +39,7 @@ export default class Inventory extends ButtonEvent {
} catch (e) { } catch (e) {
AppLogger.LogError("Button/Inventory", `Error generating inventory page for ${member.user.username} with id ${member.user.id}: ${e}`); AppLogger.LogError("Button/Inventory", `Error generating inventory page for ${member.user.username} with id ${member.user.id}: ${e}`);
await interaction.reply("No page for user found."); await interaction.followUp("An error has occurred running this command.");
} }
} }
} }

View file

@ -39,6 +39,11 @@ export default class Inventory extends Command {
const embed = await InventoryHelper.GenerateInventoryPage(user.username, user.id, pageNumber); const embed = await InventoryHelper.GenerateInventoryPage(user.username, user.id, pageNumber);
if (!embed) {
await interaction.followUp("No page for user found.");
return;
}
await interaction.followUp({ await interaction.followUp({
files: [ embed.image ], files: [ embed.image ],
embeds: [ embed.embed ], embeds: [ embed.embed ],
@ -47,7 +52,7 @@ export default class Inventory extends Command {
} catch (e) { } catch (e) {
AppLogger.LogError("Commands/Inventory", e as string); AppLogger.LogError("Commands/Inventory", e as string);
await interaction.followUp("No page for user found."); await interaction.followUp("An error has occurred running this command.");
} }
} }
} }

View file

@ -31,13 +31,15 @@ interface ReturnedInventoryPage {
export default class InventoryHelper { export default class InventoryHelper {
public static async GenerateInventoryPage(username: string, userid: string, page: number): Promise<ReturnedInventoryPage> { public static async GenerateInventoryPage(username: string, userid: string, page: number): Promise<ReturnedInventoryPage | undefined> {
AppLogger.LogSilly("Helpers/InventoryHelper", `Parameters: username=${username}, userid=${userid}, page=${page}`); AppLogger.LogSilly("Helpers/InventoryHelper", `Parameters: username=${username}, userid=${userid}, page=${page}`);
const cardsPerPage = 9; const cardsPerPage = 9;
const inventory = await Inventory.FetchAllByUserId(userid); const inventory = await Inventory.FetchAllByUserId(userid);
if (!inventory || inventory.length == 0) return undefined;
const clientCards = cloneDeep(CoreClient.Cards); const clientCards = cloneDeep(CoreClient.Cards);
const allSeriesClaimed = clientCards const allSeriesClaimed = clientCards
@ -88,8 +90,7 @@ export default class InventoryHelper {
const currentPage = pages[page]; const currentPage = pages[page];
if (!currentPage) { if (!currentPage) {
AppLogger.LogError("Helpers/InventoryHelper", "Unable to find page"); return undefined;
return Promise.reject("Unable to find page");
} }
const embed = new EmbedBuilder() const embed = new EmbedBuilder()