feat(fws): implement the version, status and changelog subcommands

Status reports each dependency separately so a Gitea outage never hides a database or webhook problem.
This commit is contained in:
BlackAngelTV committed 2026-09-15 14:50:41 +02:00
1 parent 76d0bf9250
commit 26d7cce05f
1 file changed
+189 -28
+189 -28
View File
@@ -1,8 +1,16 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { ACCESS_LEVELS } from '../../config/constants.js'; import { ACCESS_LEVELS, COLORS } from '../../config/constants.js';
import { env } from '../../config/env.js'; import { env } from '../../config/env.js';
import { createLogger } from '../../core/logger.js';
import { getSetting } from '../../database/repositories/settings-repository.js';
import { countOpenTickets } from '../../database/repositories/ticket-repository.js';
import { asList, gitea, isGiteaConfigured, repositoryUrl } from '../../services/gitea/gitea-client.js';
import { releaseEmbed } from '../../services/gitea/gitea-embeds.js';
import { replyGiteaFailure } from '../../services/gitea/gitea-modals.js';
import { linkButtons } from '../../ui/buttons.js'; import { linkButtons } from '../../ui/buttons.js';
import { baseEmbed, notImplementedEmbed } from '../../ui/embeds.js'; import { baseEmbed, errorEmbed, infoEmbed } from '../../ui/embeds.js';
const log = createLogger('fws');
export const access = ACCESS_LEVELS.USER; export const access = ACCESS_LEVELS.USER;
@@ -28,30 +36,183 @@ const officialLinks = () => [
{ label: 'Discord', url: env.links.discordInvite }, { label: 'Discord', url: env.links.discordInvite },
]; ];
const handlers = { /** Guild settings are stored as JSON in SQLite; a failure falls back to the default (M4-T07). */
// TODO(M4-T01): content should come from guild_settings (M4-T07). function readSetting(guildId, key, fallback = null) {
info: (interaction) => try {
interaction.reply({ return getSetting(guildId, key, fallback);
embeds: [ } catch (error) {
baseEmbed() log.debug(`Could not read setting ${key}`, error);
.setTitle('FWS & FWS Arch') return fallback;
.setDescription( }
'FWS est une organisation open source multi-projets.\n' + }
"**FWS Arch** est un fork d'Arch Linux avec une surcouche conviviale.",
), const DEFAULT_PRESENTATION =
], 'FWS est une organisation open source multi-projets.\n' +
components: linkButtons(officialLinks()), "**FWS Arch** est un fork d'Arch Linux avec une surcouche conviviale.";
}),
links: (interaction) => async function latestRelease() {
interaction.reply({ try {
embeds: [baseEmbed().setTitle('Liens officiels FWS')], return await gitea.getLatestRelease();
components: linkButtons(officialLinks()), } catch {
}), return asList(await gitea.listReleases(1))[0] ?? null;
version: (interaction) => interaction.reply({ embeds: [notImplementedEmbed('/fws version')] }), }
status: (interaction) => interaction.reply({ embeds: [notImplementedEmbed('/fws status')] }), }
changelog: (interaction) => interaction.reply({ embeds: [notImplementedEmbed('/fws changelog')] }),
}; async function showInfo(interaction) {
const configured = readSetting(interaction.guildId, 'fws.info', DEFAULT_PRESENTATION);
export async function execute(interaction) { const description = typeof configured === 'string' && configured ? configured : DEFAULT_PRESENTATION;
await handlers[interaction.options.getSubcommand()](interaction); await interaction.reply({
embeds: [baseEmbed().setTitle('FWS & FWS Arch').setDescription(description)],
components: linkButtons(officialLinks()),
});
}
async function showVersion(interaction) {
const fallback = readSetting(interaction.guildId, 'fws.version', null);
if (!isGiteaConfigured()) {
await interaction.reply({
embeds: [
fallback
? infoEmbed('Version FWS Arch', `Version publiée : **${fallback}**`)
: errorEmbed('Gitea n’est pas configuré : impossible de lire la dernière version.'),
],
});
return;
}
await interaction.deferReply();
try {
const release = await latestRelease();
if (!release) {
await interaction.editReply({
embeds: [
fallback
? infoEmbed('Version FWS Arch', `Version publiée : **${fallback}**`)
: infoEmbed('Version FWS Arch', 'Aucune release publiée pour le moment.'),
],
});
return;
}
await interaction.editReply({
embeds: [releaseEmbed(release, { withNotes: false })],
components: release.html_url ? linkButtons([{ label: 'Notes de version', url: release.html_url }]) : [],
});
} catch (error) {
await replyGiteaFailure(interaction, error);
}
}
async function showStatus(interaction) {
await interaction.deferReply();
const giteaConfigured = isGiteaConfigured();
const giteaReachable = giteaConfigured
? await gitea
.listReleases(1)
.then(() => true)
.catch(() => false)
: false;
let openTickets = null;
try {
openTickets = countOpenTickets(interaction.guildId);
} catch (error) {
log.warn('Database unavailable while building /fws status', error);
}
const uptime = Math.floor(process.uptime());
const embed = baseEmbed(COLORS.info)
.setTitle('État des services')
.addFields(
{
name: 'API Gitea',
value: !giteaConfigured
? '⚙️ Non configurée'
: giteaReachable
? `✅ En ligne — ${repositoryUrl()}`
: '❌ Injoignable',
inline: false,
},
{
name: 'Webhooks Gitea',
value: env.webhook.enabled ? '✅ Actifs' : '⚙️ Désactivés (`WEBHOOK_ENABLED=false`)',
inline: false,
},
{
name: 'Base SQLite',
value:
openTickets === null ? '❌ Indisponible' : `✅ Accessible — ${openTickets} ticket(s) ouvert(s)`,
inline: false,
},
{
name: 'Discord',
value: `✅ Connecté — ${Math.round(interaction.client.ws.ping)} ms`,
inline: false,
},
{ name: 'Disponibilité du bot', value: `<t:${Math.floor((Date.now() - uptime * 1000) / 1000)}:R>` },
);
await interaction.editReply({ embeds: [embed] });
}
async function showChangelog(interaction) {
const version = interaction.options.getString('version');
if (!isGiteaConfigured()) {
await interaction.reply({
embeds: [errorEmbed('Gitea n’est pas configuré : le changelog est indisponible.')],
});
return;
}
await interaction.deferReply();
try {
const release = version ? await gitea.getReleaseByTag(version) : await latestRelease();
if (!release) {
await interaction.editReply({
embeds: [infoEmbed('Changelog', `Aucune release trouvée pour **${version}**.`)],
});
return;
}
await interaction.editReply({
embeds: [releaseEmbed(release)],
components: release.html_url ? linkButtons([{ label: 'Ouvrir sur Gitea', url: release.html_url }]) : [],
});
} catch (error) {
if (version) {
log.warn(`Release ${version} not found`, error);
await interaction.editReply({
embeds: [infoEmbed('Changelog', `La version **${version}** n’existe pas dans les releases Gitea.`)],
});
return;
}
await replyGiteaFailure(interaction, error);
}
}
/** Static presentation plus live data from Gitea (M4). */
export async function execute(interaction) {
switch (interaction.options.getSubcommand()) {
case 'info':
await showInfo(interaction);
return;
case 'links':
await interaction.reply({
embeds: [baseEmbed().setTitle('Liens officiels FWS')],
components: linkButtons(officialLinks()),
});
return;
case 'version':
await showVersion(interaction);
return;
case 'status':
await showStatus(interaction);
return;
case 'changelog':
await showChangelog(interaction);
return;
default:
return;
}
} }