feat: mejorar la estructura y legibilidad del código en el comando 'area-nivel', añadiendo soporte para metadata y optimizando la gestión de interacciones

This commit is contained in:
2025-10-07 22:24:53 -05:00
parent 67643595f3
commit a5d4879123

View File

@@ -1,9 +1,18 @@
import type { CommandMessage } from '../../../core/types/commands'; import type { CommandMessage } from "../../../core/types/commands";
import type Amayo from '../../../core/client'; import type Amayo from "../../../core/client";
import { hasManageGuildOrStaff } from '../../../core/lib/permissions'; import { hasManageGuildOrStaff } from "../../../core/lib/permissions";
import { prisma } from '../../../core/database/prisma'; import { prisma } from "../../../core/database/prisma";
import { Message, MessageComponentInteraction, MessageFlags, ButtonInteraction } from 'discord.js'; import {
import { ComponentType, TextInputStyle, ButtonStyle } from 'discord-api-types/v10'; Message,
MessageComponentInteraction,
MessageFlags,
ButtonInteraction,
} from "discord.js";
import {
ComponentType,
TextInputStyle,
ButtonStyle,
} from "discord-api-types/v10";
interface LevelState { interface LevelState {
areaKey: string; areaKey: string;
@@ -11,30 +20,52 @@ interface LevelState {
requirements?: any; requirements?: any;
rewards?: any; rewards?: any;
mobs?: any; mobs?: any;
metadata?: any;
availableFrom?: string; availableFrom?: string;
availableTo?: string; availableTo?: string;
} }
export const command: CommandMessage = { export const command: CommandMessage = {
name: 'area-nivel', name: "area-nivel",
type: 'message', type: "message",
aliases: ['nivel-area','arealevel'], aliases: ["nivel-area", "arealevel"],
cooldown: 10, cooldown: 10,
description: 'Crea o edita un nivel de una GameArea (requisitos, recompensas, mobs, ventana).', description:
usage: 'area-nivel <areaKey> <level>', "Crea o edita un nivel de una GameArea (requisitos, recompensas, mobs, ventana).",
usage: "area-nivel <areaKey> <level>",
run: async (message, args, _client: Amayo) => { run: async (message, args, _client: Amayo) => {
const allowed = await hasManageGuildOrStaff(message.member, message.guild!.id, prisma); const allowed = await hasManageGuildOrStaff(
if (!allowed) { await message.reply('❌ No tienes permisos de ManageGuild ni rol de staff.'); return; } message.member,
message.guild!.id,
prisma
);
if (!allowed) {
await message.reply(
"❌ No tienes permisos de ManageGuild ni rol de staff."
);
return;
}
const areaKey = args[0]?.trim(); const areaKey = args[0]?.trim();
const levelNum = parseInt(args[1] || '', 10); const levelNum = parseInt(args[1] || "", 10);
if (!areaKey || !Number.isFinite(levelNum) || levelNum <= 0) { await message.reply('Uso: `!area-nivel <areaKey> <level>`'); return; } if (!areaKey || !Number.isFinite(levelNum) || levelNum <= 0) {
await message.reply("Uso: `!area-nivel <areaKey> <level>`");
return;
}
const guildId = message.guild!.id; const guildId = message.guild!.id;
const area = await prisma.gameArea.findFirst({ where: { key: areaKey, OR: [{ guildId }, { guildId: null }] }, orderBy: [{ guildId: 'desc' }] }); const area = await prisma.gameArea.findFirst({
if (!area) { await message.reply('❌ Área no encontrada.'); return; } where: { key: areaKey, OR: [{ guildId }, { guildId: null }] },
orderBy: [{ guildId: "desc" }],
});
if (!area) {
await message.reply("❌ Área no encontrada.");
return;
}
const existing = await prisma.gameAreaLevel.findFirst({ where: { areaId: area.id, level: levelNum } }); const existing = await prisma.gameAreaLevel.findFirst({
where: { areaId: area.id, level: levelNum },
});
const state: LevelState = { const state: LevelState = {
areaKey, areaKey,
@@ -42,91 +73,264 @@ export const command: CommandMessage = {
requirements: existing?.requirements ?? {}, requirements: existing?.requirements ?? {},
rewards: existing?.rewards ?? {}, rewards: existing?.rewards ?? {},
mobs: existing?.mobs ?? {}, mobs: existing?.mobs ?? {},
availableFrom: existing?.availableFrom ? new Date(existing.availableFrom).toISOString() : '', metadata: existing?.metadata ?? {},
availableTo: existing?.availableTo ? new Date(existing.availableTo).toISOString() : '', availableFrom: existing?.availableFrom
? new Date(existing.availableFrom).toISOString()
: "",
availableTo: existing?.availableTo
? new Date(existing.availableTo).toISOString()
: "",
}; };
const editorMsg = await (message.channel as any).send({ const editorMsg = await (message.channel as any).send({
content: `📊 Editor Nivel Área: \`${areaKey}\` nivel ${levelNum} ${existing ? '(editar)' : '(nuevo)'}`, content: `📊 Editor Nivel Área: \`${areaKey}\` nivel ${levelNum} ${
existing ? "(editar)" : "(nuevo)"
}`,
components: [ components: [
{ type: 1, components: [ {
{ type: 2, style: ButtonStyle.Primary, label: 'Requisitos', custom_id: 'gl_req' }, type: 1,
{ type: 2, style: ButtonStyle.Secondary, label: 'Recompensas', custom_id: 'gl_rewards' }, components: [
{ type: 2, style: ButtonStyle.Secondary, label: 'Mobs', custom_id: 'gl_mobs' }, {
{ type: 2, style: ButtonStyle.Secondary, label: 'Ventana', custom_id: 'gl_window' }, type: 2,
] }, style: ButtonStyle.Primary,
{ type: 1, components: [ label: "Requisitos",
{ type: 2, style: ButtonStyle.Success, label: 'Guardar', custom_id: 'gl_save' }, custom_id: "gl_req",
{ type: 2, style: ButtonStyle.Danger, label: 'Cancelar', custom_id: 'gl_cancel' }, },
] }, {
type: 2,
style: ButtonStyle.Secondary,
label: "Recompensas",
custom_id: "gl_rewards",
},
{
type: 2,
style: ButtonStyle.Secondary,
label: "Mobs",
custom_id: "gl_mobs",
},
{
type: 2,
style: ButtonStyle.Secondary,
label: "Ventana",
custom_id: "gl_window",
},
{
type: 2,
style: ButtonStyle.Secondary,
label: "Meta/Imagen",
custom_id: "gl_meta",
},
],
},
{
type: 1,
components: [
{
type: 2,
style: ButtonStyle.Success,
label: "Guardar",
custom_id: "gl_save",
},
{
type: 2,
style: ButtonStyle.Danger,
label: "Cancelar",
custom_id: "gl_cancel",
},
],
},
], ],
}); });
const collector = editorMsg.createMessageComponentCollector({ time: 30*60_000, filter: (i: MessageComponentInteraction)=> i.user.id === message.author.id }); const collector = editorMsg.createMessageComponentCollector({
collector.on('collect', async (i: MessageComponentInteraction) => { time: 30 * 60_000,
filter: (i: MessageComponentInteraction) =>
i.user.id === message.author.id,
});
collector.on("collect", async (i: MessageComponentInteraction) => {
try { try {
if (!i.isButton()) return; if (!i.isButton()) return;
switch (i.customId) { switch (i.customId) {
case 'gl_cancel': case "gl_cancel":
await i.deferUpdate(); await editorMsg.edit({ content: '❌ Editor de Nivel cancelado.', components: [] }); collector.stop('cancel'); return; await i.deferUpdate();
case 'gl_req': await showJsonModal(i as ButtonInteraction, state, 'requirements', 'Requisitos'); return; await editorMsg.edit({
case 'gl_rewards': await showJsonModal(i as ButtonInteraction, state, 'rewards', 'Recompensas'); return; content: "❌ Editor de Nivel cancelado.",
case 'gl_mobs': await showJsonModal(i as ButtonInteraction, state, 'mobs', 'Mobs'); return; components: [],
case 'gl_window': await showWindowModal(i as ButtonInteraction, state); return; });
case 'gl_save': collector.stop("cancel");
return;
case "gl_req":
await showJsonModal(
i as ButtonInteraction,
state,
"requirements",
"Requisitos"
);
return;
case "gl_rewards":
await showJsonModal(
i as ButtonInteraction,
state,
"rewards",
"Recompensas"
);
return;
case "gl_mobs":
await showJsonModal(i as ButtonInteraction, state, "mobs", "Mobs");
return;
case "gl_window":
await showWindowModal(i as ButtonInteraction, state);
return;
case "gl_meta":
await showJsonModal(
i as ButtonInteraction,
state,
"metadata",
"Metadata (incl. imagen)"
);
return;
case "gl_save":
const data = { const data = {
areaId: area.id, areaId: area.id,
level: state.level, level: state.level,
requirements: state.requirements ?? {}, requirements: state.requirements ?? {},
rewards: state.rewards ?? {}, rewards: state.rewards ?? {},
mobs: state.mobs ?? {}, mobs: state.mobs ?? {},
availableFrom: state.availableFrom ? new Date(state.availableFrom) : null, metadata: state.metadata ?? {},
availableTo: state.availableTo ? new Date(state.availableTo) : null, availableFrom: state.availableFrom
? new Date(state.availableFrom)
: null,
availableTo: state.availableTo
? new Date(state.availableTo)
: null,
} as const; } as const;
if (existing) { if (existing) {
await prisma.gameAreaLevel.update({ where: { id: existing.id }, data }); await prisma.gameAreaLevel.update({
where: { id: existing.id },
data,
});
} else { } else {
await prisma.gameAreaLevel.create({ data }); await prisma.gameAreaLevel.create({ data });
} }
await i.reply({ content: '✅ Nivel guardado.', flags: MessageFlags.Ephemeral }); await i.reply({
await editorMsg.edit({ content: `✅ Nivel guardado para \`${areaKey}\` (${state.level}).`, components: [] }); content: "✅ Nivel guardado.",
collector.stop('saved'); flags: MessageFlags.Ephemeral,
});
await editorMsg.edit({
content: `✅ Nivel guardado para \`${areaKey}\` (${state.level}).`,
components: [],
});
collector.stop("saved");
return; return;
} }
} catch (e) { } catch (e) {
if (!i.deferred && !i.replied) await i.reply({ content: '❌ Error procesando la acción.', flags: MessageFlags.Ephemeral }); if (!i.deferred && !i.replied)
await i.reply({
content: "❌ Error procesando la acción.",
flags: MessageFlags.Ephemeral,
});
} }
}); });
collector.on('end', async (_c: any,r: string)=> { if (r==='time') { try { await editorMsg.edit({ content:'⏰ Editor expirado.', components: [] }); } catch {} } }); collector.on("end", async (_c: any, r: string) => {
if (r === "time") {
try {
await editorMsg.edit({
content: "⏰ Editor expirado.",
components: [],
});
} catch {}
} }
});
},
}; };
async function showJsonModal(i: ButtonInteraction, state: LevelState, field: 'requirements'|'rewards'|'mobs', title: string) { async function showJsonModal(
i: ButtonInteraction,
state: LevelState,
field: "requirements" | "rewards" | "mobs" | "metadata",
title: string
) {
const current = JSON.stringify(state[field] ?? {}); const current = JSON.stringify(state[field] ?? {});
const modal = { title, customId: `gl_json_${field}`, components: [ const modal = {
{ type: ComponentType.Label, label: 'JSON', component: { type: ComponentType.TextInput, customId: 'json', style: TextInputStyle.Paragraph, required: false, value: current.slice(0,4000) } }, title,
] } as const; customId: `gl_json_${field}`,
components: [
{
type: ComponentType.Label,
label: "JSON",
component: {
type: ComponentType.TextInput,
customId: "json",
style: TextInputStyle.Paragraph,
required: false,
value: current.slice(0, 4000),
},
},
],
} as const;
await i.showModal(modal); await i.showModal(modal);
try { try {
const sub = await i.awaitModalSubmit({ time: 300_000 }); const sub = await i.awaitModalSubmit({ time: 300_000 });
const raw = sub.components.getTextInputValue('json'); const raw = sub.components.getTextInputValue("json");
if (raw) { try { state[field] = JSON.parse(raw); await sub.reply({ content: '✅ Guardado.', flags: MessageFlags.Ephemeral }); } catch { await sub.reply({ content: '❌ JSON inválido.', flags: MessageFlags.Ephemeral }); } } if (raw) {
else { state[field] = {}; await sub.reply({ content: ' Limpio.', flags: MessageFlags.Ephemeral }); } try {
state[field] = JSON.parse(raw);
await sub.reply({
content: "✅ Guardado.",
flags: MessageFlags.Ephemeral,
});
} catch {
await sub.reply({
content: "❌ JSON inválido.",
flags: MessageFlags.Ephemeral,
});
}
} else {
state[field] = {};
await sub.reply({ content: " Limpio.", flags: MessageFlags.Ephemeral });
}
} catch {} } catch {}
} }
async function showWindowModal(i: ButtonInteraction, state: LevelState) { async function showWindowModal(i: ButtonInteraction, state: LevelState) {
const modal = { title: 'Ventana del Nivel', customId: 'gl_window_modal', components: [ const modal = {
{ type: ComponentType.Label, label: 'Desde (ISO, opcional)', component: { type: ComponentType.TextInput, customId: 'from', style: TextInputStyle.Short, required: false, value: state.availableFrom ?? '' } }, title: "Ventana del Nivel",
{ type: ComponentType.Label, label: 'Hasta (ISO, opcional)', component: { type: ComponentType.TextInput, customId: 'to', style: TextInputStyle.Short, required: false, value: state.availableTo ?? '' } }, customId: "gl_window_modal",
] } as const; components: [
{
type: ComponentType.Label,
label: "Desde (ISO, opcional)",
component: {
type: ComponentType.TextInput,
customId: "from",
style: TextInputStyle.Short,
required: false,
value: state.availableFrom ?? "",
},
},
{
type: ComponentType.Label,
label: "Hasta (ISO, opcional)",
component: {
type: ComponentType.TextInput,
customId: "to",
style: TextInputStyle.Short,
required: false,
value: state.availableTo ?? "",
},
},
],
} as const;
await i.showModal(modal); await i.showModal(modal);
try { try {
const sub = await i.awaitModalSubmit({ time: 300_000 }); const sub = await i.awaitModalSubmit({ time: 300_000 });
const from = sub.components.getTextInputValue('from').trim(); const from = sub.components.getTextInputValue("from").trim();
const to = sub.components.getTextInputValue('to').trim(); const to = sub.components.getTextInputValue("to").trim();
state.availableFrom = from || ''; state.availableFrom = from || "";
state.availableTo = to || ''; state.availableTo = to || "";
await sub.reply({ content: '✅ Ventana actualizada.', flags: MessageFlags.Ephemeral }); await sub.reply({
content: "✅ Ventana actualizada.",
flags: MessageFlags.Ephemeral,
});
} catch {} } catch {}
} }