import { SlashCommandBuilder } from 'discord.js'; import { ACCESS_LEVELS, COLORS } from '../../config/constants.js'; import { createLogger } from '../../core/logger.js'; import { countByAction } from '../../database/repositories/moderation-repository.js'; import { countPolls } from '../../database/repositories/poll-repository.js'; import { countOpenTickets } from '../../database/repositories/ticket-repository.js'; import { asList, gitea, repositoryUrl } from '../../services/gitea/gitea-client.js'; import { baseEmbed, errorEmbed } from '../../ui/embeds.js'; const log = createLogger('stats'); export const access = ACCESS_LEVELS.USER; export const data = new SlashCommandBuilder() .setName('stats') .setDescription('Statistiques publiques') .addSubcommand((sub) => sub.setName('discord').setDescription('Statistiques du serveur Discord')) .addSubcommand((sub) => sub.setName('gitea').setDescription('Activité et indicateurs Gitea')) .addSubcommand((sub) => sub.setName('fws').setDescription('Indicateurs FWS / FWS Arch')); const discordStatsEmbed = (interaction) => { const guild = interaction.guild; return baseEmbed() .setTitle(`📊 Statistiques — ${guild.name}`) .setThumbnail(guild.iconURL({ size: 128 })) .addFields( { name: 'Membres', value: String(guild.memberCount), inline: true }, { name: 'Salons', value: String(guild.channels.cache.size), inline: true }, { name: 'Rôles', value: String(guild.roles.cache.size), inline: true }, { name: 'Boosts', value: String(guild.premiumSubscriptionCount ?? 0), inline: true }, { name: 'Créé', value: ``, inline: true }, { name: 'Latence', value: `${Math.round(interaction.client.ws.ping)} ms`, inline: true }, ); }; async function giteaStatsEmbed() { const [issues, pulls, commits, releases] = await Promise.all([ gitea.listIssues('state=open&type=issues&limit=50'), gitea.listPullRequests('state=open&limit=50'), gitea.listCommits(5), gitea.listReleases(1), ]); const latestCommits = asList(commits) .slice(0, 5) .map((commit) => `\`${commit.sha?.slice(0, 7)}\` ${commit.commit?.message?.split('\n')[0] ?? ''}`) .join('\n'); const release = asList(releases)[0]; const embed = baseEmbed(COLORS.gitea) .setTitle('📈 Activité Gitea') .setURL(repositoryUrl()) .setDescription(latestCommits || 'Aucun commit récent pour le moment.') .addFields( { name: 'Issues ouvertes', value: String(asList(issues).length), inline: true }, { name: 'PR ouvertes', value: String(asList(pulls).length), inline: true }, ); if (release) embed.addFields({ name: 'Dernière release', value: release.tag_name ?? 'inconnue', inline: true }); return embed; } /** Community activity stored locally, independent from Gitea availability (M11). */ function fwsStatsEmbed(interaction) { const actions = countByAction(interaction.guildId); const total = actions.reduce((sum, row) => sum + row.count, 0); return baseEmbed(COLORS.gitea) .setTitle('📈 Indicateurs FWS') .addFields( { name: 'Tickets ouverts', value: String(countOpenTickets(interaction.guildId)), inline: true }, { name: 'Sondages créés', value: String(countPolls(interaction.guildId)), inline: true }, { name: 'Actions de modération', value: String(total), inline: true }, { name: 'Dépôt', value: repositoryUrl() || 'Non configuré' }, ) .setFooter({ text: 'Statistiques locales · FWS-Bot' }); } /** `/stats` never fails because of Gitea: the API errors are caught (M11-T03). */ export async function execute(interaction) { const subcommand = interaction.options.getSubcommand(); await interaction.deferReply(); if (subcommand === 'discord') { await interaction.editReply({ embeds: [discordStatsEmbed(interaction)] }); return; } if (subcommand === 'fws') { try { const releases = asList(await gitea.listReleases(1)); const embed = fwsStatsEmbed(interaction); if (releases[0]) embed.addFields({ name: 'Version FWS Arch', value: releases[0].tag_name ?? 'inconnue' }); await interaction.editReply({ embeds: [embed] }); } catch (error) { log.warn('Gitea unavailable while building /stats fws', error); await interaction.editReply({ embeds: [fwsStatsEmbed(interaction)] }); } return; } try { await interaction.editReply({ embeds: [await giteaStatsEmbed()] }); } catch (error) { log.error('Could not read Gitea stats', error); await interaction.editReply({ embeds: [ errorEmbed( 'Les statistiques Gitea ne répondent pas. Réessaie dans un instant — si ça continue, l’équipe peut vérifier `GITEA_URL` et `GITEA_TOKEN`. Le reste du bot fonctionne normalement.', ), ], }); } }