refactor: centralize custom interaction IDs into constants

Replace all hardcoded custom ID strings with module-level constants.
Each module now has *_CUSTOM_IDS in its types file, using functions
for dynamic IDs and PREFIX for startsWith matching.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
syntaxbullet
2026-04-02 11:36:35 +02:00
parent 70d59a091a
commit 3c256ba0b2
27 changed files with 238 additions and 132 deletions

View File

@@ -3,6 +3,7 @@ import { inventoryService } from "@shared/modules/inventory/inventory.service";
import { getLootboxResultMessage } from "./inventory.view";
import type { ItemUsageData } from "@shared/lib/types";
import { getGuildConfig } from "@shared/lib/config";
import { INVENTORY_CUSTOM_IDS } from "./inventory.types";
export interface InventoryState {
ownerId: string;
@@ -25,7 +26,7 @@ export function parseInventoryCustomId(customId: string): { action: string; view
* Checks if a custom ID belongs to the inventory system.
*/
export function isInventoryInteraction(customId: string): boolean {
return customId.startsWith("inv_");
return customId.startsWith(INVENTORY_CUSTOM_IDS.PREFIX);
}
/**

View File

@@ -0,0 +1,13 @@
export const INVENTORY_CUSTOM_IDS = {
PREFIX: "inv_",
SELECT: (viewerId: string) => `inv_select_${viewerId}`,
PREV: (viewerId: string) => `inv_prev_${viewerId}`,
PAGE: (viewerId: string) => `inv_page_${viewerId}`,
NEXT: (viewerId: string) => `inv_next_${viewerId}`,
BACK: (viewerId: string) => `inv_back_${viewerId}`,
USE: (viewerId: string) => `inv_use_${viewerId}`,
DISCARD: (viewerId: string) => `inv_discard_${viewerId}`,
DISCARD_CONFIRM: (viewerId: string) => `inv_discard_confirm_${viewerId}`,
DISCARD_CANCEL: (viewerId: string) => `inv_discard_cancel_${viewerId}`,
USE_BACK: (viewerId: string) => `inv_use_back_${viewerId}`,
} as const;

View File

@@ -22,6 +22,7 @@ import { ItemType } from "@shared/lib/constants";
import type { ItemUsageData } from "@shared/lib/types";
import { join } from "path";
import { existsSync } from "fs";
import { INVENTORY_CUSTOM_IDS } from "./inventory.types";
export const ITEMS_PER_PAGE = 5;
@@ -101,7 +102,7 @@ export function getInventoryListMessage(
// Select menu with current page items
const selectMenu = new StringSelectMenuBuilder()
.setCustomId(`inv_select_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.SELECT(viewerId))
.setPlaceholder("Select an item for details");
for (const entry of pageItems) {
@@ -121,17 +122,17 @@ export function getInventoryListMessage(
// Pagination buttons
const navRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId(`inv_prev_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.PREV(viewerId))
.setLabel("◀ Previous")
.setStyle(ButtonStyle.Secondary)
.setDisabled(safePage <= 0),
new ButtonBuilder()
.setCustomId(`inv_page_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.PAGE(viewerId))
.setLabel(`Page ${safePage + 1}/${totalPages}`)
.setStyle(ButtonStyle.Secondary)
.setDisabled(true),
new ButtonBuilder()
.setCustomId(`inv_next_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.NEXT(viewerId))
.setLabel("Next ▶")
.setStyle(ButtonStyle.Secondary)
.setDisabled(safePage >= totalPages - 1),
@@ -225,7 +226,7 @@ export function getItemDetailMessage(
const actionRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId(`inv_back_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.BACK(viewerId))
.setLabel("◀ Back")
.setStyle(ButtonStyle.Primary)
);
@@ -233,7 +234,7 @@ export function getItemDetailMessage(
if (isUsable) {
actionRow.addComponents(
new ButtonBuilder()
.setCustomId(`inv_use_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.USE(viewerId))
.setLabel("🧪 Use")
.setStyle(ButtonStyle.Success)
);
@@ -242,7 +243,7 @@ export function getItemDetailMessage(
if (isOwner) {
actionRow.addComponents(
new ButtonBuilder()
.setCustomId(`inv_discard_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.DISCARD(viewerId))
.setLabel("🗑 Discard")
.setStyle(ButtonStyle.Danger)
);
@@ -271,11 +272,11 @@ export function getDiscardConfirmMessage(entry: InventoryEntry, viewerId: string
.addActionRowComponents(
new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId(`inv_discard_confirm_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.DISCARD_CONFIRM(viewerId))
.setLabel("Confirm")
.setStyle(ButtonStyle.Danger),
new ButtonBuilder()
.setCustomId(`inv_discard_cancel_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.DISCARD_CANCEL(viewerId))
.setLabel("Cancel")
.setStyle(ButtonStyle.Secondary)
)
@@ -296,7 +297,7 @@ export function getDiscardConfirmMessage(entry: InventoryEntry, viewerId: string
export function appendUseBackButton(message: any, viewerId: string): any {
const backRow = new ActionRowBuilder<ButtonBuilder>().addComponents(
new ButtonBuilder()
.setCustomId(`inv_use_back_${viewerId}`)
.setCustomId(INVENTORY_CUSTOM_IDS.USE_BACK(viewerId))
.setLabel("◀ Back to Inventory")
.setStyle(ButtonStyle.Primary)
);