import { option } from "@purrkit/router"; import db, { bugs, eq, getActor } from "@stealth-developers/db"; import { errorMessage, getTextChannel } 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 container = await constructBugContainer(updatedBug, originalActor); if (!container) return; const channel = await getTextChannel("1512924775842840767"); if (!channel) return errorMessage(interaction, "Failed to find bug report channel."); if (updatedBug.messageId) { try { const message = await channel.messages.fetch(updatedBug.messageId); await message.edit({ components: [container], }); } catch (err) { console.error("error updating discord message:", err); } } const paddedNumber = updatedBug.bugNumber.toString().padStart(4, "0"); await interaction.editReply({ content: `Bug #${paddedNumber} status updated to **${status}** successfully.`, }); }, });