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:
@@ -12,6 +12,7 @@ import { inventoryService } from "@shared/modules/inventory/inventory.service";
|
||||
import { createErrorEmbed, createWarningEmbed, createSuccessEmbed, createInfoEmbed } from "@lib/embeds";
|
||||
import { UserError } from "@shared/lib/errors";
|
||||
import { getTradeDashboard, getTradeMoneyModal, getItemSelectMenu, getTradeCompletedEmbed } from "./trade.view";
|
||||
import { TRADE_CUSTOM_IDS } from "./trade.types";
|
||||
|
||||
|
||||
|
||||
@@ -23,25 +24,25 @@ export async function handleTradeInteraction(interaction: Interaction) {
|
||||
|
||||
if (!threadId) return;
|
||||
|
||||
if (customId === 'trade_cancel') {
|
||||
if (customId === TRADE_CUSTOM_IDS.CANCEL) {
|
||||
await handleCancel(interaction, threadId);
|
||||
} else if (customId === 'trade_lock') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.LOCK) {
|
||||
await handleLock(interaction, threadId);
|
||||
} else if (customId === 'trade_confirm') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.CONFIRM) {
|
||||
// Confirm logic is handled implicitly by both locking or explicitly if needed.
|
||||
// For now, locking both triggers execution, so no separate confirm handler is actively used
|
||||
// unless we re-introduce a specific button. keeping basic handler stub if needed.
|
||||
} else if (customId === 'trade_add_money') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.ADD_MONEY) {
|
||||
await handleAddMoneyClick(interaction);
|
||||
} else if (customId === 'trade_money_modal') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.MONEY_MODAL) {
|
||||
await handleMoneySubmit(interaction as ModalSubmitInteraction, threadId);
|
||||
} else if (customId === 'trade_add_item') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.ADD_ITEM) {
|
||||
await handleAddItemClick(interaction as ButtonInteraction, threadId);
|
||||
} else if (customId === 'trade_select_item') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.SELECT_ITEM) {
|
||||
await handleItemSelect(interaction as StringSelectMenuInteraction, threadId);
|
||||
} else if (customId === 'trade_remove_item') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.REMOVE_ITEM) {
|
||||
await handleRemoveItemClick(interaction as ButtonInteraction, threadId);
|
||||
} else if (customId === 'trade_remove_item_select') {
|
||||
} else if (customId === TRADE_CUSTOM_IDS.REMOVE_ITEM_SELECT) {
|
||||
await handleRemoveItemSelect(interaction as StringSelectMenuInteraction, threadId);
|
||||
}
|
||||
}
|
||||
@@ -82,7 +83,7 @@ async function handleAddMoneyClick(interaction: Interaction) {
|
||||
}
|
||||
|
||||
async function handleMoneySubmit(interaction: ModalSubmitInteraction, threadId: string) {
|
||||
const amountStr = interaction.fields.getTextInputValue('amount');
|
||||
const amountStr = interaction.fields.getTextInputValue(TRADE_CUSTOM_IDS.MONEY_AMOUNT_FIELD);
|
||||
const amount = BigInt(amountStr);
|
||||
|
||||
if (amount < 0n) throw new UserError("Amount must be positive");
|
||||
@@ -107,7 +108,7 @@ async function handleAddItemClick(interaction: ButtonInteraction, threadId: stri
|
||||
description: `Rarity: ${entry.item.rarity} `
|
||||
}));
|
||||
|
||||
const { components } = getItemSelectMenu(options, 'trade_select_item', 'Select an item to add');
|
||||
const { components } = getItemSelectMenu(options, TRADE_CUSTOM_IDS.SELECT_ITEM, 'Select an item to add');
|
||||
await interaction.reply({ content: "Select an item to add:", components, ephemeral: true });
|
||||
}
|
||||
|
||||
@@ -142,7 +143,7 @@ async function handleRemoveItemClick(interaction: ButtonInteraction, threadId: s
|
||||
value: i.id.toString(),
|
||||
}));
|
||||
|
||||
const { components } = getItemSelectMenu(options, 'trade_remove_item_select', 'Select an item to remove');
|
||||
const { components } = getItemSelectMenu(options, TRADE_CUSTOM_IDS.REMOVE_ITEM_SELECT, 'Select an item to remove');
|
||||
await interaction.reply({ content: "Select an item to remove:", components, ephemeral: true });
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,16 @@
|
||||
export const TRADE_CUSTOM_IDS = {
|
||||
PREFIX: "trade_",
|
||||
ADD_ITEM: "trade_add_item",
|
||||
ADD_MONEY: "trade_add_money",
|
||||
REMOVE_ITEM: "trade_remove_item",
|
||||
LOCK: "trade_lock",
|
||||
CANCEL: "trade_cancel",
|
||||
CONFIRM: "trade_confirm",
|
||||
MONEY_MODAL: "trade_money_modal",
|
||||
MONEY_AMOUNT_FIELD: "amount",
|
||||
SELECT_ITEM: "trade_select_item",
|
||||
REMOVE_ITEM_SELECT: "trade_remove_item_select",
|
||||
} as const;
|
||||
|
||||
export interface TradeItem {
|
||||
id: number;
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ModalBuilder, StringSelectMenuBuilder, TextInputBuilder, TextInputStyle } from "discord.js";
|
||||
import { createBaseEmbed } from "@lib/embeds";
|
||||
import type { TradeSession, TradeParticipant } from "./trade.types";
|
||||
import { TRADE_CUSTOM_IDS, type TradeSession, type TradeParticipant } from "./trade.types";
|
||||
|
||||
const EMBED_COLOR = 0xFFD700; // Gold
|
||||
|
||||
@@ -34,11 +34,11 @@ export function getTradeDashboard(session: TradeSession) {
|
||||
|
||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
||||
.addComponents(
|
||||
new ButtonBuilder().setCustomId('trade_add_item').setLabel('Add Item').setStyle(ButtonStyle.Secondary),
|
||||
new ButtonBuilder().setCustomId('trade_add_money').setLabel('Add Money').setStyle(ButtonStyle.Success),
|
||||
new ButtonBuilder().setCustomId('trade_remove_item').setLabel('Remove Item').setStyle(ButtonStyle.Secondary),
|
||||
new ButtonBuilder().setCustomId('trade_lock').setLabel('Lock / Unlock').setStyle(ButtonStyle.Primary),
|
||||
new ButtonBuilder().setCustomId('trade_cancel').setLabel('Cancel').setStyle(ButtonStyle.Danger),
|
||||
new ButtonBuilder().setCustomId(TRADE_CUSTOM_IDS.ADD_ITEM).setLabel('Add Item').setStyle(ButtonStyle.Secondary),
|
||||
new ButtonBuilder().setCustomId(TRADE_CUSTOM_IDS.ADD_MONEY).setLabel('Add Money').setStyle(ButtonStyle.Success),
|
||||
new ButtonBuilder().setCustomId(TRADE_CUSTOM_IDS.REMOVE_ITEM).setLabel('Remove Item').setStyle(ButtonStyle.Secondary),
|
||||
new ButtonBuilder().setCustomId(TRADE_CUSTOM_IDS.LOCK).setLabel('Lock / Unlock').setStyle(ButtonStyle.Primary),
|
||||
new ButtonBuilder().setCustomId(TRADE_CUSTOM_IDS.CANCEL).setLabel('Cancel').setStyle(ButtonStyle.Danger),
|
||||
);
|
||||
|
||||
return { embeds: [embed], components: [row] };
|
||||
@@ -57,11 +57,11 @@ export function getTradeCompletedEmbed(session: TradeSession) {
|
||||
|
||||
export function getTradeMoneyModal() {
|
||||
const modal = new ModalBuilder()
|
||||
.setCustomId('trade_money_modal')
|
||||
.setCustomId(TRADE_CUSTOM_IDS.MONEY_MODAL)
|
||||
.setTitle('Add Money');
|
||||
|
||||
const input = new TextInputBuilder()
|
||||
.setCustomId('amount')
|
||||
.setCustomId(TRADE_CUSTOM_IDS.MONEY_AMOUNT_FIELD)
|
||||
.setLabel("Amount to trade")
|
||||
.setStyle(TextInputStyle.Short)
|
||||
.setPlaceholder("100")
|
||||
|
||||
Reference in New Issue
Block a user