const polls = new Map<string, { yes: number; no: number; voters: Set<string> }>()
bot.onSlashCommand('poll', async (handler, event) => {
const pollId = `poll-${Date.now()}`
polls.set(pollId, { yes: 0, no: 0, voters: new Set() })
await handler.sendInteractionRequest(event.channelId, {
type: 'form',
id: pollId,
components: [
{ id: 'yes', type: 'button', label: 'Yes' },
{ id: 'no', type: 'button', label: 'No' }
]
// No recipient = public poll
})
})
bot.onInteractionResponse(async (handler, event) => {
if (event.response.payload.content?.case !== 'form') return
const form = event.response.payload.content.value
const poll = polls.get(form.id)
if (!poll) return
// Prevent duplicate votes
if (poll.voters.has(event.userId)) return
poll.voters.add(event.userId)
for (const c of form.components) {
if (c.component.case === 'button') {
if (c.id === 'yes') poll.yes++
if (c.id === 'no') poll.no++
}
}
await handler.sendMessage(event.channelId, `Yes: ${poll.yes}, No: ${poll.no}`)
})