all repos — stealth-developers @ 80180ee40ecc4d92d5572e4c8c32e3517a42ca16

apps/bot/src/feats/bugs/buttons.ts (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
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.`,
		});
	},
});