Files
elysium/apps/api/src/services/webhook.ts
T
hikari 1a99e83b86 refactor: hardcode non-secret Discord IDs as constants
Move guild ID, apotheosis role ID, Discord client ID, and redirect URI
from env vars to hardcoded constants — none of these are secrets, and
hardcoding removes unnecessary runtime configuration surface. Update
tests to use the real IDs and drop now-irrelevant env var scenarios.
2026-03-24 18:26:38 -07:00

153 lines
4.7 KiB
TypeScript

/**
* @file Discord webhook and role-grant utilities for milestone events.
* @copyright nhcarrigan
* @license Naomi's Public License
* @author Naomi Carrigan
*/
/* eslint-disable @typescript-eslint/naming-convention -- Discord API requires snake_case and Pascal-case header names */
import { logger } from "./logger.js";
const discordApi = "https://discord.com/api/v10";
/**
* Discord MessageFlags.SUPPRESS_NOTIFICATIONS — messages are delivered without
* triggering desktop or mobile push notifications.
*/
const suppressNotifications = 4096;
/**
* The Discord role ID for the Elysian role granted to all Elysium players.
*/
const discordGuildId = "1354624415861833870";
const elysianRoleId = "1486144823684628490";
const apotheosisRoleId = "1479966598210129991";
/**
* Grants the Elysian Discord role to the given player and returns whether they are in the guild.
* Fails silently so role grant errors do not affect the auth flow.
* @param discordId - The Discord user ID to grant the role to.
* @returns True if the player is in the guild and the role was granted, false otherwise.
*/
const grantElysianRole = async(discordId: string): Promise<boolean> => {
const botToken = process.env.DISCORD_BOT_TOKEN;
if (botToken === undefined || botToken === "") {
return false;
}
try {
const response = await fetch(
`${discordApi}/guilds/${discordGuildId}/members/${discordId}/roles/${elysianRoleId}`,
{
headers: {
"Authorization": `Bot ${botToken}`,
"User-Agent": "DiscordBot (https://elysium.nhcarrigan.com, 1.0.0)",
},
method: "PUT",
},
);
return response.ok || response.status === 204;
} catch (error) {
void logger.error(
"webhook_elysian_role",
error instanceof Error
? error
: new Error(String(error)),
);
return false;
}
};
/**
* Grants the apotheosis Discord role to the given player if configured.
* Fails silently so role grant errors do not affect the game action.
* @param discordId - The Discord user ID to grant the role to.
* @returns A promise that resolves when the role grant attempt completes.
*/
const grantApotheosisRole = async(discordId: string): Promise<void> => {
const botToken = process.env.DISCORD_BOT_TOKEN;
if (botToken === undefined || botToken === "") {
return;
}
try {
await fetch(
`${discordApi}/guilds/${discordGuildId}/members/${discordId}/roles/${apotheosisRoleId}`,
{
headers: {
"Authorization": `Bot ${botToken}`,
"User-Agent": "DiscordBot (https://elysium.nhcarrigan.com, 1.0.0)",
},
method: "PUT",
},
);
} catch (error) {
void logger.error(
"webhook_apotheosis_role",
error instanceof Error
? error
: new Error(String(error)),
);
// Graceful degradation — role grant failure must not affect the apotheosis
}
};
type MilestoneType = "prestige" | "transcendence" | "apotheosis";
interface MilestoneCounts {
prestige: number;
transcendence: number;
apotheosis: number;
}
const milestoneVerbs: Record<MilestoneType, string> = {
apotheosis: "reached apotheosis",
prestige: "prestiged",
transcendence: "transcended",
};
/**
* Posts a milestone announcement to the configured Discord webhook.
* Fails silently so webhook errors do not affect the game action.
* @param discordId - The Discord user ID of the player.
* @param milestone - The type of milestone reached.
* @param counts - The current prestige, transcendence, and apotheosis counts.
* @returns A promise that resolves when the webhook post attempt completes.
*/
const postMilestoneWebhook = async(
discordId: string,
milestone: MilestoneType,
counts: MilestoneCounts,
): Promise<void> => {
const webhookUrl = process.env.DISCORD_MILESTONE_WEBHOOK;
if (webhookUrl === undefined || webhookUrl === "") {
return;
}
const verb = milestoneVerbs[milestone];
/* eslint-disable-next-line @typescript-eslint/restrict-template-expressions -- counts fields are numbers, intentionally stringified */
const content = `<@${discordId}> has ${verb}~! They are now on Prestige ${counts.prestige}, Transcendence ${counts.transcendence}, Apotheosis ${counts.apotheosis}!`;
try {
await fetch(webhookUrl, {
body: JSON.stringify({
content: content,
flags: suppressNotifications,
}),
headers: { "Content-Type": "application/json" },
method: "POST",
});
} catch (error) {
void logger.error(
"webhook_milestone",
error instanceof Error
? error
: new Error(String(error)),
);
// Graceful degradation — webhook failure must not affect the game action
}
};
export { grantApotheosisRole, grantElysianRole, postMilestoneWebhook };