- MissionRating.tsx star form + overview renderer, wired into missions/[id] - Discord rating embed + ptf-rate action row, posted on mission Completed - interactionCreate ptf-rate handler replies ephemeral with mission URL
121 lines
3.5 KiB
TypeScript
121 lines
3.5 KiB
TypeScript
import { type ButtonInteraction, MessageFlags } from "discord.js";
|
|
import type { Payload } from "payload";
|
|
import { findUserByDiscordId } from "@/bot/lib/resolve";
|
|
import {
|
|
attendanceHash,
|
|
buildAttendanceRow,
|
|
fetchAttendanceDocs,
|
|
renderAttendanceEmbed,
|
|
} from "@/bot/services/missionEmbeds";
|
|
import { botConfig } from "@/bot/config";
|
|
import {
|
|
attendanceClosedMessage,
|
|
type AttendanceResponse,
|
|
getMissionAttendance,
|
|
isMissionPast,
|
|
setMissionAttendance,
|
|
} from "@/lib/attendance";
|
|
|
|
const ATTENDANCE_LABELS: Record<AttendanceResponse, string> = {
|
|
yes: "Yes",
|
|
no: "No",
|
|
tentative: "Tentative",
|
|
};
|
|
|
|
const isAttendanceResponse = (value: string): value is AttendanceResponse =>
|
|
value === "yes" || value === "no" || value === "tentative";
|
|
|
|
const errorMessage = (error: unknown): string =>
|
|
error instanceof Error ? error.message : "Unknown error";
|
|
|
|
export const handleButtonInteraction = async (
|
|
interaction: ButtonInteraction,
|
|
payload: Payload,
|
|
): Promise<void> => {
|
|
const parts = interaction.customId.split(":");
|
|
|
|
// Post-op survey rating prompt: link back to the mission page to rate.
|
|
if (parts.length === 3 && parts[0] === "ptf-rate") {
|
|
const missionId = Number(parts[1]);
|
|
if (!Number.isInteger(missionId)) return;
|
|
try {
|
|
const url = `${botConfig.appUrl}/intelligence/missions/${missionId}`;
|
|
await interaction.reply({
|
|
content: `Rate this op here: ${url}`,
|
|
flags: MessageFlags.Ephemeral,
|
|
});
|
|
} catch (error) {
|
|
payload.logger.error(
|
|
`[Bot] Rating interaction failed for mission #${missionId}: ${errorMessage(error)}`,
|
|
);
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Attendance roll-call buttons.
|
|
if (parts.length !== 3 || parts[0] !== "ptf-att") {
|
|
return;
|
|
}
|
|
const missionId = Number(parts[1]);
|
|
const response = parts[2];
|
|
if (!Number.isInteger(missionId) || !isAttendanceResponse(response)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const user = await findUserByDiscordId(payload, interaction.user.id);
|
|
if (!user) {
|
|
await interaction.reply({
|
|
content: "Run /signup or /link first.",
|
|
flags: MessageFlags.Ephemeral,
|
|
});
|
|
return;
|
|
}
|
|
|
|
const mission = await payload.findByID({
|
|
collection: "missions",
|
|
id: missionId,
|
|
depth: 1,
|
|
overrideAccess: true,
|
|
});
|
|
|
|
if (isMissionPast(mission.classification.startDateTime)) {
|
|
await interaction.reply({ content: attendanceClosedMessage, flags: MessageFlags.Ephemeral });
|
|
return;
|
|
}
|
|
|
|
await setMissionAttendance(payload, { missionId, userId: user.id, response });
|
|
|
|
const summary = await getMissionAttendance(payload, missionId);
|
|
const hash = attendanceHash(
|
|
await fetchAttendanceDocs(payload, missionId),
|
|
mission.classification.startDateTime,
|
|
);
|
|
|
|
await interaction.message.edit({
|
|
embeds: [renderAttendanceEmbed(mission, summary)],
|
|
components: [buildAttendanceRow(missionId)],
|
|
});
|
|
await payload.update({
|
|
collection: "missions",
|
|
id: missionId,
|
|
data: { discordAttendanceHash: hash },
|
|
overrideAccess: true,
|
|
});
|
|
|
|
await interaction.reply({
|
|
content: `You are marked as ${ATTENDANCE_LABELS[response]} for ${mission.name}.`,
|
|
flags: MessageFlags.Ephemeral,
|
|
});
|
|
} catch (error) {
|
|
payload.logger.error(
|
|
`[Bot] RSVP interaction failed for mission #${missionId}: ${errorMessage(error)}`,
|
|
);
|
|
if (!interaction.replied && !interaction.deferred) {
|
|
await interaction.reply({
|
|
content: "Something went wrong while updating your attendance.",
|
|
flags: MessageFlags.Ephemeral,
|
|
});
|
|
}
|
|
}
|
|
};
|