import { MessageFlags, SlashCommandBuilder } from 'discord.js'; import { ACCESS_LEVELS } from '../../config/constants.js'; import { buildPollEmbed, closePollById, createPoll } from '../../services/polls/poll-service.js'; import { successEmbed } from '../../ui/embeds.js'; export const access = ACCESS_LEVELS.STAFF; export const data = new SlashCommandBuilder() .setName('poll') .setDescription('Sondages') .addSubcommand((sub) => sub .setName('create') .setDescription('Créer un sondage') .addStringOption((option) => option.setName('question').setDescription('Question posée').setRequired(true).setMaxLength(200), ) .addStringOption((option) => option .setName('choices') .setDescription('Propositions séparées par des « ; » (2 à 10)') .setRequired(true), ) .addIntegerOption((option) => option .setName('minutes') .setDescription('Durée avant fermeture automatique, en minutes') .setMinValue(1) .setMaxValue(10_080), ), ) .addSubcommand((sub) => sub .setName('close') .setDescription('Fermer un sondage') .addIntegerOption((option) => option.setName('id').setDescription('Numéro du sondage (affiché dans l’embed)').setRequired(true), ), ); /** Polls are published in the current channel and voted with buttons (M10). */ export async function execute(interaction) { if (interaction.options.getSubcommand() === 'close') { const poll = await closePollById(interaction.client, interaction.options.getInteger('id', true)); await interaction.reply({ embeds: [buildPollEmbed(poll)] }); return; } const question = interaction.options.getString('question', true); const choicesInput = interaction.options.getString('choices', true); const minutes = interaction.options.getInteger('minutes'); await interaction.deferReply({ flags: MessageFlags.Ephemeral }); const pollId = await createPoll(interaction, { question, choicesInput, minutes }); await interaction.editReply({ embeds: [ successEmbed( `Ton sondage **#${pollId}** est en ligne dans <#${interaction.channelId}> : tout le monde vote avec les boutons.`, ), ], }); }