palico-bot/src/config.js
Shinuwa 8cf0a8eb5b
All checks were successful
Deploy Palico Bot / check (push) Successful in 10s
Deploy Palico Bot / deploy (push) Successful in 7s
Stop listening when an events client disconnects; warn on partial OAuth setup
- Detach the events stream from the queue on the response's close event,
  which fires when the client goes away whatever the Node version.
- Log a warning when only some of the Discord login variables are set,
  instead of silently leaving the login disabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:23:28 +01:00

98 lines
3.4 KiB
JavaScript

const requiredVariables = [
"NODE_ENV",
"DISCORD_GUILD_ID",
"SHINUWA_USER_ID",
"WEB_SERVER_PORT",
"WEB_API_KEY",
"DISCORD_APPLICATION_ID",
"DISCORD_TOKEN",
"VOICE_CHANNEL_ID",
"TEXT_CHANNEL_ID",
"GAME_SERVERS_MESSAGE_ID",
];
const missingVariables = requiredVariables.filter(
(name) => !process.env[name]?.trim()
);
if (missingVariables.length > 0) {
throw new Error(
`Variables d'environnement manquantes : ${missingVariables.join(", ")}`
);
}
const webServerPort = Number(process.env.WEB_SERVER_PORT);
if (
!Number.isInteger(webServerPort) ||
webServerPort <= 0 ||
webServerPort > 65_535
) {
throw new Error("WEB_SERVER_PORT doit être compris entre 1 et 65535");
}
// Variables facultatives.
// WEB_TRUSTED_PROXIES : proxys dont on accepte l'en-tête X-Forwarded-For (syntaxe
// « trust proxy » d'Express). Ne jamais mettre « true » : le port est joignable sans proxy.
const webTrustedProxies = process.env.WEB_TRUSTED_PROXIES?.trim() || "loopback";
// NOTIFICATIONS_API_KEY : clé des applications de confiance (Sokko TCG). Sans elle, la
// route des messages privés est désactivée.
const notificationsApiKey = process.env.NOTIFICATIONS_API_KEY?.trim() || null;
// INVITE_LINK : lien publié par /invitation quand la commande ne reçoit pas de lien.
const inviteLink = process.env.INVITE_LINK?.trim() || null;
// Connexion avec Discord sur la page web (facultative, active si les trois sont définies) :
// DISCORD_CLIENT_SECRET (portail développeur, OAuth2), WEB_PUBLIC_URL (adresse publique du
// site, sans / final) et SESSION_SECRET (32 caractères au moins, signe les sessions).
const discordClientSecret = process.env.DISCORD_CLIENT_SECRET?.trim() || null;
const webPublicUrl = process.env.WEB_PUBLIC_URL?.trim().replace(/\/+$/, "") || null;
const sessionSecret = process.env.SESSION_SECRET?.trim() || null;
const discordOAuthEnabled = Boolean(discordClientSecret && webPublicUrl && sessionSecret);
const oauthVariablesSet = [discordClientSecret, webPublicUrl, sessionSecret].filter(Boolean).length;
if (oauthVariablesSet > 0 && !discordOAuthEnabled) {
console.warn(
"Connexion avec Discord désactivée : DISCORD_CLIENT_SECRET, WEB_PUBLIC_URL et SESSION_SECRET doivent toutes être définies"
);
}
if (discordOAuthEnabled) {
let publicUrl;
try {
publicUrl = new URL(webPublicUrl);
} catch {
throw new Error("WEB_PUBLIC_URL doit être une adresse complète (https://…)");
}
if (publicUrl.protocol !== "https:" && publicUrl.hostname !== "localhost") {
throw new Error("WEB_PUBLIC_URL doit être en https");
}
if (sessionSecret.length < 32) {
throw new Error("SESSION_SECRET doit contenir au moins 32 caractères");
}
}
if (notificationsApiKey && notificationsApiKey === process.env.WEB_API_KEY) {
throw new Error("NOTIFICATIONS_API_KEY doit être différente de WEB_API_KEY");
}
const config = Object.freeze({
environnement: process.env.NODE_ENV,
discordGuildId: process.env.DISCORD_GUILD_ID,
shinuwaUserId: process.env.SHINUWA_USER_ID,
webServerPort,
webApiKey: process.env.WEB_API_KEY,
webTrustedProxies,
notificationsApiKey,
inviteLink,
discordOAuthEnabled,
discordClientSecret,
webPublicUrl,
sessionSecret,
applicationId: process.env.DISCORD_APPLICATION_ID,
token: process.env.DISCORD_TOKEN,
voiceChannelId: process.env.VOICE_CHANNEL_ID,
textChannelId: process.env.TEXT_CHANNEL_ID,
gameServersMessageId: process.env.GAME_SERVERS_MESSAGE_ID,
});
export { config };