initial
This commit is contained in:
@@ -3,27 +3,10 @@ import {
|
||||
ModalSubmitInteraction,
|
||||
MessageFlags,
|
||||
EmbedBuilder,
|
||||
User,
|
||||
Collection,
|
||||
Snowflake,
|
||||
} from "discord.js";
|
||||
import { prisma } from "../../core/database/prisma";
|
||||
import { hasManageGuildOrStaff } from "../../core/lib/permissions";
|
||||
|
||||
interface UserSelectComponent {
|
||||
custom_id: string;
|
||||
type: number;
|
||||
values: string[];
|
||||
}
|
||||
|
||||
interface ComponentData {
|
||||
components?: ComponentData[];
|
||||
component?: ComponentData;
|
||||
custom_id?: string;
|
||||
type?: number;
|
||||
values?: string[];
|
||||
}
|
||||
|
||||
export default {
|
||||
customId: "ld_points_modal",
|
||||
run: async (interaction: ModalSubmitInteraction) => {
|
||||
@@ -53,88 +36,26 @@ export default {
|
||||
}
|
||||
|
||||
try {
|
||||
// Obtener valores del modal con manejo seguro de errores
|
||||
// Read values from modal text inputs
|
||||
let userId: string = "";
|
||||
let userName: string = "";
|
||||
let totalInput: string = "";
|
||||
let selectedUsers: ReturnType<
|
||||
typeof interaction.components.getSelectedUsers
|
||||
> = null;
|
||||
|
||||
try {
|
||||
selectedUsers = interaction.components.getSelectedUsers("user_select");
|
||||
// Get user ID from text input
|
||||
userId = interaction.fields.getTextInputValue("user_id_input");
|
||||
// Get points modification from text input
|
||||
totalInput = interaction.fields.getTextInputValue("points_input");
|
||||
|
||||
if (!selectedUsers || selectedUsers.size === 0) {
|
||||
// Fallback: intentar obtener los IDs directamente de los datos raw
|
||||
const rawData = (interaction as any).data?.components as
|
||||
| ComponentData[]
|
||||
| undefined;
|
||||
if (rawData) {
|
||||
const userSelectComponent = findUserSelectComponent(
|
||||
rawData,
|
||||
"user_select"
|
||||
);
|
||||
if (
|
||||
userSelectComponent?.values?.length &&
|
||||
userSelectComponent.values.length > 0
|
||||
) {
|
||||
userId = userSelectComponent.values[0];
|
||||
logger.info(
|
||||
`🔄 Fallback: UserId extraído de datos raw: ${userId}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (!userId) {
|
||||
return interaction.reply({
|
||||
content: "❌ Debes seleccionar un usuario del leaderboard.",
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
const selectedUser = Array.from(selectedUsers.values())[0] as User;
|
||||
if (selectedUser) {
|
||||
userId = selectedUser.id;
|
||||
userName = selectedUser.tag ?? selectedUser.username ?? userId;
|
||||
}
|
||||
}
|
||||
logger.info(`🔍 Input recibido - UserId: ${userId}, Puntos: ${totalInput}`);
|
||||
} catch (error) {
|
||||
// @ts-ignore
|
||||
logger.error(
|
||||
"Error procesando UserSelect, intentando fallback:",
|
||||
String(error)
|
||||
);
|
||||
|
||||
// Fallback más agresivo: obtener directamente de los datos raw
|
||||
try {
|
||||
const rawData = (interaction as any).data?.components as
|
||||
| ComponentData[]
|
||||
| undefined;
|
||||
const userSelectComponent = findUserSelectComponent(
|
||||
rawData,
|
||||
"user_select"
|
||||
);
|
||||
|
||||
if (
|
||||
userSelectComponent?.values?.length &&
|
||||
userSelectComponent.values.length > 0
|
||||
) {
|
||||
userId = userSelectComponent.values[0];
|
||||
logger.info(`🔄 Fallback agresivo: UserId extraído: ${userId}`);
|
||||
} else {
|
||||
throw new Error("No se pudo extraer userId de los datos raw");
|
||||
}
|
||||
} catch (fallbackError) {
|
||||
// @ts-ignore
|
||||
logger.error("Falló el fallback:", String(fallbackError));
|
||||
return interaction.reply({
|
||||
content:
|
||||
"❌ Error procesando la selección de usuario. Inténtalo de nuevo.",
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
logger.error({ error }, "Error al leer campos del modal");
|
||||
return interaction.reply({
|
||||
content: "❌ Error al leer los campos del formulario.",
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
|
||||
logger.info(`🔍 Input recibido: ${totalInput}`);
|
||||
logger.info(`🔍 UserId extraído: ${userId}`);
|
||||
|
||||
if (!totalInput) {
|
||||
return interaction.reply({
|
||||
content: "❌ Debes ingresar un valor para modificar.",
|
||||
@@ -144,24 +65,29 @@ export default {
|
||||
|
||||
if (!userId) {
|
||||
return interaction.reply({
|
||||
content: "❌ Error al identificar el usuario seleccionado.",
|
||||
content: "❌ Debes ingresar el ID del usuario.",
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
|
||||
// Si no tenemos userName, intentar obtenerlo del servidor
|
||||
if (!userName) {
|
||||
try {
|
||||
const targetMember = await interaction.guild.members.fetch(userId);
|
||||
userName = targetMember.displayName || targetMember.user.username;
|
||||
} catch (error) {
|
||||
// @ts-ignore
|
||||
logger.warn(
|
||||
`No se pudo obtener info del usuario ${userId}:`,
|
||||
String(error)
|
||||
);
|
||||
userName = `Usuario ${userId}`;
|
||||
}
|
||||
// Validar que el userId sea un número válido
|
||||
if (!/^\d{17,20}$/.test(userId)) {
|
||||
return interaction.reply({
|
||||
content: "❌ El ID del usuario debe ser un número de 17-20 dígitos.",
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
}
|
||||
|
||||
// Intentar obtener el nombre del usuario
|
||||
try {
|
||||
const targetMember = await interaction.guild.members.fetch(userId);
|
||||
userName = targetMember.displayName || targetMember.user.username;
|
||||
} catch (error) {
|
||||
logger.warn(
|
||||
{ error },
|
||||
`No se pudo obtener info del usuario ${userId}`
|
||||
);
|
||||
userName = `Usuario ${userId}`;
|
||||
}
|
||||
|
||||
// ✅ ARREGLO: Asegurar que el User exista en la base de datos antes de crear PartnershipStats
|
||||
@@ -346,9 +272,7 @@ export default {
|
||||
flags: MessageFlags.Ephemeral,
|
||||
});
|
||||
} catch (error) {
|
||||
// @ts-ignore
|
||||
// @ts-ignore
|
||||
logger.error("❌ Error en ldPointsModal:", String(error));
|
||||
logger.error({ error }, "❌ Error en ldPointsModal");
|
||||
|
||||
if (!interaction.replied && !interaction.deferred) {
|
||||
await interaction.reply({
|
||||
@@ -360,32 +284,3 @@ export default {
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
// Función auxiliar para buscar componentes UserSelect en datos raw
|
||||
function findUserSelectComponent(
|
||||
components: ComponentData[] | undefined,
|
||||
customId: string
|
||||
): UserSelectComponent | null {
|
||||
if (!components) return null;
|
||||
|
||||
for (const comp of components) {
|
||||
if (comp.components) {
|
||||
const found = findUserSelectComponent(comp.components, customId);
|
||||
if (found) return found;
|
||||
}
|
||||
|
||||
if (comp.component) {
|
||||
if (comp.component.custom_id === customId) {
|
||||
return comp.component as UserSelectComponent;
|
||||
}
|
||||
const found = findUserSelectComponent([comp.component], customId);
|
||||
if (found) return found;
|
||||
}
|
||||
|
||||
if (comp.custom_id === customId) {
|
||||
return comp as UserSelectComponent;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user