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:
BlackAngelTV committed 2026-09-15 14:50:54 +02:00
1 parent f29c09577a
commit ef1bedbc65
6 files changed
+514 -14

No files matched your search

+100 -5
View File
@@ -1,12 +1,107 @@
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 { parseChoices } from '../../services/polls/poll-service.js';
import { baseEmbed } from '../../ui/embeds.js';
export const access = ACCESS_LEVELS.USER;
export const data = new SlashCommandBuilder().setName('fun').setDescription('Commandes de divertissement');
const EIGHT_BALL_ANSWERS = Object.freeze([
'Oui, sans aucun doute.',
'Compte là-dessus et bois de l’eau.',
'Les signes disent que oui.',
'Impossible de le prédire pour le moment.',
'Concentre-toi et redemande.',
'Ma réponse est non.',
'Très probablement.',
'Mes sources disent non.',
'La réponse est floue, réessaie.',
'Absolument !',
'Pas dans un avenir proche.',
'Oui, mais fais attention à toi.',
]);
// TODO(M10-T07): add fun subcommands.
const pick = (list) => list[Math.floor(Math.random() * list.length)];
export const data = new SlashCommandBuilder()
.setName('fun')
.setDescription('Commandes de divertissement')
.addSubcommand((sub) => sub.setName('coinflip').setDescription('Pile ou face'))
.addSubcommand((sub) =>
sub
.setName('dice')
.setDescription('Lancer un dé')
.addIntegerOption((option) =>
option
.setName('sides')
.setDescription('Nombre de faces (6 par défaut)')
.setMinValue(2)
.setMaxValue(1000),
),
)
.addSubcommand((sub) =>
sub
.setName('8ball')
.setDescription('Poser une question à la boule magique')
.addStringOption((option) =>
option.setName('question').setDescription('Ta question').setRequired(true).setMaxLength(300),
),
)
.addSubcommand((sub) =>
sub
.setName('choose')
.setDescription('Choisir au hasard parmi plusieurs options')
.addStringOption((option) =>
option.setName('options').setDescription('Options séparées par ";" (2 à 10)').setRequired(true),
),
);
/** Lightweight, stateless fun commands (M10-T07). */
export async function execute(interaction) {
await interaction.reply({ embeds: [notImplementedEmbed('/fun')] });
const subcommand = interaction.options.getSubcommand();
if (subcommand === 'coinflip') {
const result = pick(['Pile', 'Face']);
await interaction.reply({
embeds: [baseEmbed(COLORS.info).setTitle('Pile ou face').setDescription(`🪙 ${result} !`)],
});
return;
}
if (subcommand === 'dice') {
const sides = interaction.options.getInteger('sides') ?? 6;
const roll = 1 + Math.floor(Math.random() * sides);
await interaction.reply({
embeds: [
baseEmbed(COLORS.info)
.setTitle(`Lancer de dé (d${sides})`)
.setDescription(`🎲 Tu obtiens **${roll}** !`),
],
});
return;
}
if (subcommand === '8ball') {
const question = interaction.options.getString('question', true);
await interaction.reply({
embeds: [
baseEmbed(COLORS.info)
.setTitle('🎱 Boule magique')
.addFields(
{ name: 'Question', value: question },
{ name: 'Réponse', value: pick(EIGHT_BALL_ANSWERS) },
),
],
});
return;
}
const options = parseChoices(interaction.options.getString('options', true));
await interaction.reply({
embeds: [
baseEmbed(COLORS.info)
.setTitle('Choix aléatoire')
.addFields({ name: 'Options', value: options.map((option) => `• ${option}`).join('\n') })
.setDescription(`Je choisis : **${pick(options)}**`),
],
});
}