Add utility to strip legacy description component from blockState and integrate into create and edit display commands

This commit is contained in:
2025-10-05 16:13:17 -05:00
parent 14c898fbec
commit 8adb748d3f
2 changed files with 48 additions and 5 deletions

View File

@@ -44,6 +44,24 @@ async function updateEditor(message: Message, data: EditorData): Promise<void> {
await message.edit(payload); await message.edit(payload);
} }
function stripLegacyDescriptionComponent(blockState: BlockState, match?: string | null): void {
if (!Array.isArray(blockState.components) || blockState.components.length === 0) return;
const normalize = (value: string | undefined | null) => value?.replace(/\s+/g, " ").trim() ?? "";
const target = normalize(match ?? blockState.description ?? undefined);
if (!target) return;
const index = blockState.components.findIndex((component: any) => {
if (!component || component.type !== 10) return false;
if (component.thumbnail || component.linkButton) return false;
return normalize(component.content) === target;
});
if (index >= 0) {
blockState.components.splice(index, 1);
}
}
export const command: CommandMessage = { export const command: CommandMessage = {
name: "crear-embed", name: "crear-embed",
type: "message", type: "message",
@@ -802,6 +820,7 @@ async function handleSaveBlock(
guildId: string guildId: string
): Promise<void> { ): Promise<void> {
try { try {
stripLegacyDescriptionComponent(blockState);
await client.prisma.blockV2Config.create({ await client.prisma.blockV2Config.create({
data: { data: {
guildId, guildId,

View File

@@ -145,6 +145,24 @@ const updateEditor = async (msg: any, data: any) => {
await msg.edit(payload); await msg.edit(payload);
}; };
const stripLegacyDescriptionComponent = (blockState: any, match?: string | null) => {
if (!Array.isArray(blockState?.components) || blockState.components.length === 0) return;
const normalize = (value: string | undefined | null) => value?.replace(/\s+/g, " ").trim() ?? "";
const target = normalize(match ?? blockState.description ?? undefined);
if (!target) return;
const index = blockState.components.findIndex((component: any) => {
if (!component || component.type !== 10) return false;
if (component.thumbnail || component.linkButton) return false;
return normalize(component.content) === target;
});
if (index >= 0) {
blockState.components.splice(index, 1);
}
};
export const command: CommandMessage = { export const command: CommandMessage = {
name: "editar-embed", name: "editar-embed",
type: "message", type: "message",
@@ -188,11 +206,13 @@ export const command: CommandMessage = {
}; };
if (!blockState.description || typeof blockState.description !== 'string' || blockState.description.trim().length === 0) { if (!blockState.description || typeof blockState.description !== 'string' || blockState.description.trim().length === 0) {
const firstText = Array.isArray(blockState.components) if (Array.isArray(blockState.components)) {
? blockState.components.find((c: any) => c?.type === 10 && typeof c.content === 'string') const firstTextIndex = blockState.components.findIndex((c: any) => c?.type === 10 && typeof c.content === 'string' && !c.thumbnail && !c.linkButton);
: null; if (firstTextIndex >= 0) {
if (firstText) { const firstText = blockState.components[firstTextIndex];
blockState.description = firstText.content; blockState.description = firstText.content;
blockState.components.splice(firstTextIndex, 1);
}
} }
} }
@@ -230,6 +250,7 @@ export const command: CommandMessage = {
case "save_block": { case "save_block": {
try { await i.deferUpdate(); } catch {} try { await i.deferUpdate(); } catch {}
try { try {
stripLegacyDescriptionComponent(blockState);
await client.prisma.blockV2Config.update({ await client.prisma.blockV2Config.update({
where: { guildId_name: { guildId: message.guildId!, name: blockName } }, where: { guildId_name: { guildId: message.guildId!, name: blockName } },
//@ts-ignore //@ts-ignore
@@ -850,8 +871,11 @@ export const command: CommandMessage = {
logger.info({ modalId: id, guildId: message.guildId, userId: interaction.user.id }, 'Título actualizado mediante modal.'); logger.info({ modalId: id, guildId: message.guildId, userId: interaction.user.id }, 'Título actualizado mediante modal.');
await sendResponse('✅ Título actualizado.'); await sendResponse('✅ Título actualizado.');
} else if (id === 'edit_description_modal') { } else if (id === 'edit_description_modal') {
const previousDescription = blockState.description ?? null;
const newDescription = interaction.components.getTextInputValue('description_input').trim(); const newDescription = interaction.components.getTextInputValue('description_input').trim();
blockState.description = newDescription.length > 0 ? newDescription : undefined; blockState.description = newDescription.length > 0 ? newDescription : undefined;
stripLegacyDescriptionComponent(blockState, previousDescription);
stripLegacyDescriptionComponent(blockState);
logger.info({ modalId: id, guildId: message.guildId, userId: interaction.user.id }, 'Descripción actualizada mediante modal.'); logger.info({ modalId: id, guildId: message.guildId, userId: interaction.user.id }, 'Descripción actualizada mediante modal.');
await sendResponse('✅ Descripción actualizada.'); await sendResponse('✅ Descripción actualizada.');
} else if (id === 'edit_color_modal') { } else if (id === 'edit_color_modal') {