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 93 94 95 96 97 98 99 |
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.`,
});
},
});
|