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:
1 file changed
+185
-24
+185
-24
@@ -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(
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const DEFAULT_PRESENTATION =
|
||||||
'FWS est une organisation open source multi-projets.\n' +
|
'FWS est une organisation open source multi-projets.\n' +
|
||||||
"**FWS Arch** est un fork d'Arch Linux avec une surcouche conviviale.",
|
"**FWS Arch** est un fork d'Arch Linux avec une surcouche conviviale.";
|
||||||
),
|
|
||||||
],
|
async function latestRelease() {
|
||||||
|
try {
|
||||||
|
return await gitea.getLatestRelease();
|
||||||
|
} catch {
|
||||||
|
return asList(await gitea.listReleases(1))[0] ?? null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function showInfo(interaction) {
|
||||||
|
const configured = readSetting(interaction.guildId, 'fws.info', DEFAULT_PRESENTATION);
|
||||||
|
const description = typeof configured === 'string' && configured ? configured : DEFAULT_PRESENTATION;
|
||||||
|
await interaction.reply({
|
||||||
|
embeds: [baseEmbed().setTitle('FWS & FWS Arch').setDescription(description)],
|
||||||
components: linkButtons(officialLinks()),
|
components: linkButtons(officialLinks()),
|
||||||
}),
|
});
|
||||||
links: (interaction) =>
|
}
|
||||||
interaction.reply({
|
|
||||||
|
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')],
|
embeds: [baseEmbed().setTitle('Liens officiels FWS')],
|
||||||
components: linkButtons(officialLinks()),
|
components: linkButtons(officialLinks()),
|
||||||
}),
|
});
|
||||||
version: (interaction) => interaction.reply({ embeds: [notImplementedEmbed('/fws version')] }),
|
return;
|
||||||
status: (interaction) => interaction.reply({ embeds: [notImplementedEmbed('/fws status')] }),
|
case 'version':
|
||||||
changelog: (interaction) => interaction.reply({ embeds: [notImplementedEmbed('/fws changelog')] }),
|
await showVersion(interaction);
|
||||||
};
|
return;
|
||||||
|
case 'status':
|
||||||
export async function execute(interaction) {
|
await showStatus(interaction);
|
||||||
await handlers[interaction.options.getSubcommand()](interaction);
|
return;
|
||||||
|
case 'changelog':
|
||||||
|
await showChangelog(interaction);
|
||||||
|
return;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user