At 8 PM Eastern on Wednesday, if no non-cancelled mission starts on that week's upcoming Friday, post once in #side-ops pinging the Community Zeus role (DISCORD_COMMUNITY_ZEUS_ROLE_ID, or the role resolved by name): the slot is open, hosting is welcome, and otherwise the slot stays empty or Jason hosts it himself. Dedup uses a stable per-Friday timestamp token scanned from recent channel messages, so no DB state. When a non-superuser later claims the slot the notice is edited to credit them; a cancelled or deleted claim reverts the notice, and a between-polls claim swap re-credits the new claimant.
133 lines
5 KiB
TypeScript
133 lines
5 KiB
TypeScript
import type { Payload } from "payload";
|
|
import type { User } from "@/payload-types";
|
|
import { superuserUserIds } from "@/utils/access-control/hasPermission";
|
|
|
|
/**
|
|
* Friday open-slot notice for the Discord bot.
|
|
*
|
|
* At 8 PM Eastern on Wednesday (the deploy container may run any server
|
|
* timezone, so the clock is evaluated in America/New_York) and no op is
|
|
* scheduled for that week's upcoming Friday yet, the bot posts once in
|
|
* #side-ops pinging the Community Zeus role: the slot is open, they are
|
|
* welcome to host an op, and if nobody picks it up the slot either stays
|
|
* empty (no op that Friday) or Jason hosts it himself. If the bot misses the
|
|
* whole Wednesday it stays quiet until the following week's notice.
|
|
*
|
|
* The shared logic lives here (not in the bot) to keep it testable and free
|
|
* of discord.js imports, mirroring the other reminder libs.
|
|
*/
|
|
|
|
/** Fallback role name resolved in the guild when no role ID is configured. */
|
|
export const SIDE_OPS_NOTICE_ROLE_NAME = "Community Zeus";
|
|
|
|
export const NOTICE_HOUR_ET = 20;
|
|
|
|
const ET_TIME_ZONE = "America/New_York";
|
|
|
|
const etParts = (now: Date): { weekday: string; hour: number } => {
|
|
const parts = new Intl.DateTimeFormat("en-US", {
|
|
timeZone: ET_TIME_ZONE,
|
|
weekday: "short",
|
|
hour: "numeric",
|
|
hour12: false,
|
|
}).formatToParts(now);
|
|
const get = (type: string): string => parts.find((part) => part.type === type)?.value ?? "";
|
|
const hour = Number.parseInt(get("hour"), 10);
|
|
return { weekday: get("weekday"), hour: hour === 24 ? 0 : hour };
|
|
};
|
|
|
|
/**
|
|
* The notice is due once Wednesday 8 PM Eastern has passed, and until the
|
|
* ET day rolls over to Thursday: a bot restart after 8 PM still catches up
|
|
* within the evening, but a fully missed Wednesday waits for next week.
|
|
*/
|
|
export const isNoticeDue = (now: Date): boolean => {
|
|
const { weekday, hour } = etParts(now);
|
|
return weekday === "Wed" && hour >= NOTICE_HOUR_ET;
|
|
};
|
|
|
|
/**
|
|
* Start/end/epoch of the upcoming Friday (server-local), strictly after
|
|
* `now`. Used both for the mission-existence query and for the stable
|
|
* per-Friday dedup token.
|
|
*/
|
|
export const upcomingFridayRange = (now: Date): { start: Date; end: Date; epoch: number } => {
|
|
const start = new Date(now);
|
|
const daysUntilFriday = (5 - start.getDay() + 7) % 7 || 7;
|
|
start.setDate(start.getDate() + daysUntilFriday);
|
|
start.setHours(0, 0, 0, 0);
|
|
const end = new Date(start);
|
|
end.setHours(23, 59, 59, 999);
|
|
return { start, end, epoch: Math.floor(start.getTime() / 1000) };
|
|
};
|
|
|
|
/**
|
|
* A Friday slot counts as taken when any non-cancelled mission (any status,
|
|
* any operation type) starts on that Friday: even a draft means someone is
|
|
* already working on the slot.
|
|
*/
|
|
export const hasMissionOnFriday = async (payload: Payload, now: Date): Promise<boolean> => {
|
|
const { start, end } = upcomingFridayRange(now);
|
|
const res = await payload.find({
|
|
collection: "missions",
|
|
overrideAccess: true,
|
|
depth: 0,
|
|
limit: 1,
|
|
where: {
|
|
and: [
|
|
{ "classification.startDateTime": { greater_than_equal: start.toISOString() } },
|
|
{ "classification.startDateTime": { less_than_equal: end.toISOString() } },
|
|
{ "ownershipAndStatus.status": { not_equals: "Cancelled" } },
|
|
],
|
|
},
|
|
});
|
|
return res.totalDocs > 0;
|
|
};
|
|
|
|
export interface FridayClaim {
|
|
userName: string;
|
|
}
|
|
|
|
/**
|
|
* The community claim on the upcoming Friday slot: the earliest-created
|
|
* non-cancelled mission whose creator is NOT a superuser, resolved to a
|
|
* display name. Jason (or any other superuser) hosting the op himself does
|
|
* not claim the slot as a community zeus claim, and staff-created missions
|
|
* without a creator do not either. Returns null when only superuser-created
|
|
* missions (or nothing) hold the Friday.
|
|
*/
|
|
export const getFridayClaimant = async (
|
|
payload: Payload,
|
|
now: Date,
|
|
): Promise<FridayClaim | null> => {
|
|
const { start, end } = upcomingFridayRange(now);
|
|
const res = await payload.find({
|
|
collection: "missions",
|
|
overrideAccess: true,
|
|
depth: 1,
|
|
limit: 25,
|
|
sort: "createdAt",
|
|
where: {
|
|
and: [
|
|
{ "classification.startDateTime": { greater_than_equal: start.toISOString() } },
|
|
{ "classification.startDateTime": { less_than_equal: end.toISOString() } },
|
|
{ "ownershipAndStatus.status": { not_equals: "Cancelled" } },
|
|
],
|
|
},
|
|
});
|
|
const creators = res.docs
|
|
.map((mission) => (typeof mission.createdBy === "object" ? (mission.createdBy as User) : null))
|
|
.filter((creator): creator is User => creator != null);
|
|
if (creators.length === 0) return null;
|
|
const superIds = await superuserUserIds(payload);
|
|
const claimant = creators.find((creator) => !superIds.includes(Number(creator.id)));
|
|
if (!claimant) return null;
|
|
return { userName: claimant.displayName ?? claimant.username };
|
|
};
|
|
|
|
/**
|
|
* Stable per-Friday token embedded in the notice message (renders as the
|
|
* Friday's date) so the bot can detect an existing notice by scanning recent
|
|
* channel messages, surviving restarts without any DB state.
|
|
*/
|
|
export const noticeToken = (now: Date): string => `<t:${upcomingFridayRange(now).epoch}:D>`;
|