feat(polls): implement polls, fun and statistics commands
A poll must end even when nobody is there to close it, so a scheduler reads the stored end dates and finalises the results at startup and while running.
This commit is contained in:
6 files changed
+514
-14
No files matched your search
@@ -1,6 +1,13 @@
|
||||
import { SlashCommandBuilder } from 'discord.js';
|
||||
import { ACCESS_LEVELS } from '../../config/constants.js';
|
||||
import { notImplementedEmbed } from '../../ui/embeds.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;
|
||||
|
||||
@@ -11,7 +18,97 @@ export const data = new SlashCommandBuilder()
|
||||
.addSubcommand((sub) => sub.setName('gitea').setDescription('Activité et indicateurs Gitea'))
|
||||
.addSubcommand((sub) => sub.setName('fws').setDescription('Indicateurs FWS / FWS Arch'));
|
||||
|
||||
// TODO(M11-T02..T04): implement per data source.
|
||||
export async function execute(interaction) {
|
||||
await interaction.reply({ embeds: [notImplementedEmbed(`/stats ${interaction.options.getSubcommand()}`)] });
|
||||
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: `<t:${Math.floor(guild.createdTimestamp / 1000)}:R>`, 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.')
|
||||
.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 sont indisponibles. Vérifie `GITEA_URL` et `GITEA_TOKEN`.'),
|
||||
],
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user