poll.ts

1import {
2	ChatInputCommandInteraction,
3	SlashCommandBuilder,
4	EmbedBuilder,
5} from 'discord.js'
6import { Command } from '@/types/bot'
7
8export default {
9	data: new SlashCommandBuilder()
10		.setName('poll')
11		.setDescription('Create a poll')
12		.addStringOption((option) =>
13			option
14				.setName('question')
15				.setDescription('The poll question')
16				.setRequired(true),
17		)
18		.addStringOption((option) =>
19			option
20				.setName('options')
21				.setDescription('Poll options (comma-separated)')
22				.setRequired(true),
23		),
24
25	async execute(interaction: ChatInputCommandInteraction) {
26		const question = interaction.options.getString('question', true)
27		const options = interaction.options
28			.getString('options', true)
29			.split(',')
30			.map((opt) => opt.trim())
31
32		if (options.length < 2 || options.length > 10) {
33			return interaction.reply({
34				content: 'Please provide between 2 and 10 options!',
35				ephemeral: true,
36			})
37		}
38
39		const emojis = ['1️⃣', '2️⃣', '3️⃣', '4️⃣', '5️⃣', '6️⃣', '7️⃣', '8️⃣', '9️⃣', '🔟']
40
41		const embed = new EmbedBuilder()
42			.setTitle('📊 ' + question)
43			.setDescription(
44				options.map((opt, i) => `${emojis[i]} ${opt}`).join('\n\n'),
45			)
46			.setColor('#FF9300')
47			.setFooter({ text: `Poll created by ${interaction.user.tag}` })
48
49		const message = await interaction.reply({
50			embeds: [embed],
51			fetchReply: true,
52		})
53
54		for (let i = 0; i < options.length; i++) {
55			await message.react(emojis[i])
56		}
57	},
58} as Command
59