import { option } from "@purrkit/router"; import db, { bugs, eq, getActor } from "@stealth-developers/db"; import { errorMessage, getTextChannel, logger } from "@/lib"; import { useGetData } from "@/middleware"; import { buildReportModal, constructBugContainer } from "./shared"; export const editButton = useGetData.button("edit-bug", { options: { id: option.integer({ required: true }), }, run: async (interaction, { id: bugId }, { actor }) => { if (!actor) return errorMessage(interaction, "Failed to retrieve your user data"); const [bug] = await db.select().from(bugs).where(eq(bugs.id, bugId)); if (!bug) return errorMessage(interaction, "Bug not found."); if (!actor.moderator && bug.actorId !== actor.id) { return errorMessage(interaction, "You do not have permission to edit this bug."); } const modal = buildReportModal(bug); interaction.showModal(modal); }, }); export const bugStatusButton = useGetData.button("bug-status", { options: { id: option.integer({ required: true }), status: option.string({ required: true }), }, run: async (interaction, { id: bugId, status }, { actor }) => { if (!actor) return errorMessage(interaction, "Failed to retrieve your user data"); const [bug] = await db.select().from(bugs).where(eq(bugs.id, bugId)); if (!bug) return errorMessage(interaction, "Bug not found."); if (!actor.moderator && bug.actorId !== actor.id) return errorMessage(interaction, "You do not have permission to edit this bug."); await interaction.deferReply({ flags: ["Ephemeral"] }); const statusMap = { open: "closed", closed: "open", } as const; const newStatus = statusMap[status as keyof typeof statusMap]; if (!newStatus) return errorMessage(interaction, "Invalid status."); const [updatedBug] = await db .update(bugs) .set({ status: newStatus, closedAt: status === "closed" ? new Date() : null, updatedAt: new Date(), }) .where(eq(bugs.id, bugId)) .returning(); if (!updatedBug) { return errorMessage(interaction, "Failed to update bug report status."); } const originalActor = await getActor(updatedBug.actorId); if (!originalActor) { return errorMessage(interaction, "Failed to fetch original bug author."); } const channel = await getTextChannel("1520861689048993843"); if (!channel) return errorMessage(interaction, "Failed to find bug report channel."); let message; if (updatedBug.messageId) { try { message = await channel.messages.fetch(updatedBug.messageId); } catch (err) { logger.error(err, "error fetching discord message"); } } const container = await constructBugContainer(updatedBug, originalActor, undefined, message); if (!container) return; if (message) { try { await message.edit({ components: [container], }); } catch (err) { logger.error(err, "error updating discord message"); } } await interaction.editReply({ content: `Status updated to **${newStatus}** successfully.`, }); }, });