Skip to content

Commit

Permalink
Add daily announcement for birthdays
Browse files Browse the repository at this point in the history
  • Loading branch information
gausie committed Nov 15, 2023
1 parent b93cc44 commit c99c6dc
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 1 deletion.
68 changes: 68 additions & 0 deletions src/commands/misc/daily.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Player } from "@prisma/client";
import { heading, userMention } from "discord.js";

import { prisma } from "../../clients/database.js";
import { createEmbed, discordClient } from "../../clients/discord.js";
import { kolClient, resolveKoLImage } from "../../clients/kol.js";
import { config } from "../../config.js";
import { englishJoin, notNull } from "../../utils.js";

async function createBirthdayEmbed() {
const birthdays = await prisma.$queryRaw<
Player[]
>`SELECT * FROM "Player" WHERE "discordId" NOT NULL AND EXTRACT(MONTH FROM "accountCreationDate") = EXTRACT(MONTH FROM CURRENT_DATE) AND EXTRACT(DAY FROM "accountCreationDate") = EXTRACT(DAY FROM CURRENT_DATE)`;

if (birthdays.length === 0) return null;

const currentYear = new Date().getFullYear();

const byAge = birthdays.reduce(
(acc, p) => {
if (!p.accountCreationDate) return acc;
const age = currentYear - p.accountCreationDate.getFullYear();
return { ...acc, [age]: [...(acc[age] || []), p] };
},
{} as Record<number, Player[]>,
);

const content = (Object.entries(byAge) as [string, Player[]][])
.toSorted((a, b) => Number(a[0]) - Number(b[0]))
.map(([age, players]) => {
const playerTags = players.map((p) => userMention(p.discordId!));
return `${age} year${age === "1" ? "" : "s"} old: ${englishJoin(
playerTags,
)}${age === "11" ? " (ridiculous; not even funny)" : ""}`;
})
.join("\n");

return createEmbed()
.setTitle("In-Game Birthdays")
.setImage(resolveKoLImage("/itemimages/cake3.gif"))
.setDescription(content);
}

async function onRollover() {
const guild = await discordClient.guilds.fetch(config.GUILD_ID);
const announcementChannel = guild?.channels.cache.get(
config.ANNOUNCEMENTS_CHANNEL_ID,
);

if (!announcementChannel?.isTextBased()) {
await discordClient.alert("No valid announcement channel");
return;
}

const embeds = [await createBirthdayEmbed()].filter(notNull);

if (embeds.length === 0) return;

await announcementChannel.send({
content: heading("Daily Announcements"),
embeds,
allowedMentions: { users: [] },
});
}

export async function init() {
kolClient.on("rollover", onRollover);
}
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,9 @@ export function formatPlayer(player: Player | undefined, backupId?: number) {
export function ensureArray<T>(v: T | T[]) {
return Array.isArray(v) ? v : [v];
}

export function englishJoin<T>(v: T[]) {
return `${v.length > 1 ? `${v.slice(0, -1).join(", ")} and ` : ""}${v.slice(
-1,
)}`;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"declaration": false,
"experimentalDecorators": true,
"forceConsistentCasingInFileNames": true,
"lib": ["ES2022"],
"lib": ["ES2023"],
"module": "NodeNext",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
Expand Down

0 comments on commit c99c6dc

Please sign in to comment.