|
| 1 | +--- |
| 2 | +title: Poll |
| 3 | +--- |
| 4 | + |
| 5 | +The `Poll` component allows you to create interactive polls in Discord messages. Users can vote on poll questions with multiple answer options, and the results are displayed in real-time. Polls can be open for up to 32 days (768 hours). |
| 6 | + |
| 7 | +## Basic usage |
| 8 | + |
| 9 | +```tsx title="src/app/commands/poll-example.tsx" |
| 10 | +import { |
| 11 | + CommandData, |
| 12 | + Poll, |
| 13 | + PollQuestion, |
| 14 | + PollAnswer, |
| 15 | + ChatInputCommand, |
| 16 | +} from 'commandkit'; |
| 17 | +import { PollData } from 'discord.js'; |
| 18 | + |
| 19 | +export const command: CommandData = { |
| 20 | + name: 'poll', |
| 21 | + description: 'Create a poll', |
| 22 | +}; |
| 23 | + |
| 24 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 25 | + const poll: PollData = ( |
| 26 | + <Poll duration={24} allowMultiselect={false}> |
| 27 | + <PollQuestion>What's your favorite programming language?</PollQuestion> |
| 28 | + <PollAnswer emoji="🟥">JavaScript</PollAnswer> |
| 29 | + <PollAnswer emoji="🟦">TypeScript</PollAnswer> |
| 30 | + <PollAnswer emoji="🟩">Python</PollAnswer> |
| 31 | + <PollAnswer emoji="🟨">Rust</PollAnswer> |
| 32 | + </Poll> |
| 33 | + ); |
| 34 | + |
| 35 | + await ctx.interaction.reply({ poll }); |
| 36 | +}; |
| 37 | +``` |
| 38 | + |
| 39 | +## Poll duration |
| 40 | + |
| 41 | +Set how long the poll should be active (in hours). The duration defaults to 24 hours and can be up to 32 days (768 hours): |
| 42 | + |
| 43 | +```tsx title="src/app/commands/poll-duration.tsx" |
| 44 | +import { |
| 45 | + CommandData, |
| 46 | + Poll, |
| 47 | + PollQuestion, |
| 48 | + PollAnswer, |
| 49 | + ChatInputCommand, |
| 50 | +} from 'commandkit'; |
| 51 | +import { PollData } from 'discord.js'; |
| 52 | + |
| 53 | +export const command: CommandData = { |
| 54 | + name: 'quickpoll', |
| 55 | + description: 'Create a quick poll', |
| 56 | +}; |
| 57 | + |
| 58 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 59 | + const poll: PollData = ( |
| 60 | + <Poll duration={1}> |
| 61 | + <PollQuestion>Quick question: Coffee or tea?</PollQuestion> |
| 62 | + <PollAnswer emoji="☕">Coffee</PollAnswer> |
| 63 | + <PollAnswer emoji="🍵">Tea</PollAnswer> |
| 64 | + </Poll> |
| 65 | + ); |
| 66 | + |
| 67 | + await ctx.interaction.reply({ poll }); |
| 68 | +}; |
| 69 | +``` |
| 70 | + |
| 71 | +For longer polls, you can specify durations up to 32 days: |
| 72 | + |
| 73 | +```tsx title="src/app/commands/long-poll.tsx" |
| 74 | +import { |
| 75 | + CommandData, |
| 76 | + Poll, |
| 77 | + PollQuestion, |
| 78 | + PollAnswer, |
| 79 | + ChatInputCommand, |
| 80 | +} from 'commandkit'; |
| 81 | +import { PollData } from 'discord.js'; |
| 82 | + |
| 83 | +export const command: CommandData = { |
| 84 | + name: 'weeklypoll', |
| 85 | + description: 'Create a weekly poll', |
| 86 | +}; |
| 87 | + |
| 88 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 89 | + const poll: PollData = ( |
| 90 | + <Poll duration={168}> |
| 91 | + <PollQuestion>Weekly poll: What should we work on next?</PollQuestion> |
| 92 | + <PollAnswer emoji="🚀">New features</PollAnswer> |
| 93 | + <PollAnswer emoji="🐛">Bug fixes</PollAnswer> |
| 94 | + <PollAnswer emoji="📚">Documentation</PollAnswer> |
| 95 | + <PollAnswer emoji="🎨">UI improvements</PollAnswer> |
| 96 | + </Poll> |
| 97 | + ); |
| 98 | + |
| 99 | + await ctx.interaction.reply({ poll }); |
| 100 | +}; |
| 101 | +``` |
| 102 | + |
| 103 | +## Multiple choice polls |
| 104 | + |
| 105 | +Allow users to select multiple answers: |
| 106 | + |
| 107 | +```tsx title="src/app/commands/multiselect-poll.tsx" |
| 108 | +import { |
| 109 | + CommandData, |
| 110 | + Poll, |
| 111 | + PollQuestion, |
| 112 | + PollAnswer, |
| 113 | + ChatInputCommand, |
| 114 | +} from 'commandkit'; |
| 115 | +import { PollData } from 'discord.js'; |
| 116 | + |
| 117 | +export const command: CommandData = { |
| 118 | + name: 'multiselect', |
| 119 | + description: 'Create a multiple choice poll', |
| 120 | +}; |
| 121 | + |
| 122 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 123 | + const poll: PollData = ( |
| 124 | + <Poll duration={48} allowMultiselect={true}> |
| 125 | + <PollQuestion>Which social media platforms do you use? (Select all that apply)</PollQuestion> |
| 126 | + <PollAnswer emoji="📘">Facebook</PollAnswer> |
| 127 | + <PollAnswer emoji="🐦">Twitter</PollAnswer> |
| 128 | + <PollAnswer emoji="📷">Instagram</PollAnswer> |
| 129 | + <PollAnswer emoji="💼">LinkedIn</PollAnswer> |
| 130 | + <PollAnswer emoji="🎵">TikTok</PollAnswer> |
| 131 | + <PollAnswer emoji="📺">YouTube</PollAnswer> |
| 132 | + </Poll> |
| 133 | + ); |
| 134 | + |
| 135 | + await ctx.interaction.reply({ poll }); |
| 136 | +}; |
| 137 | +``` |
| 138 | + |
| 139 | +## Poll layout types |
| 140 | + |
| 141 | +Customize how your poll appears using different layout types: |
| 142 | + |
| 143 | +```tsx title="src/app/commands/poll-layouts.tsx" |
| 144 | +import { |
| 145 | + CommandData, |
| 146 | + Poll, |
| 147 | + PollQuestion, |
| 148 | + PollAnswer, |
| 149 | + ChatInputCommand, |
| 150 | +} from 'commandkit'; |
| 151 | +import { PollData, PollLayoutType } from 'discord.js'; |
| 152 | + |
| 153 | +export const command: CommandData = { |
| 154 | + name: 'layoutpoll', |
| 155 | + description: 'Create a poll with custom layout', |
| 156 | +}; |
| 157 | + |
| 158 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 159 | + const poll: PollData = ( |
| 160 | + <Poll duration={24} layoutType={PollLayoutType.List}> |
| 161 | + <PollQuestion>List layout poll</PollQuestion> |
| 162 | + <PollAnswer emoji="📝">First item</PollAnswer> |
| 163 | + <PollAnswer emoji="📋">Second item</PollAnswer> |
| 164 | + <PollAnswer emoji="📄">Third item</PollAnswer> |
| 165 | + </Poll> |
| 166 | + ); |
| 167 | + |
| 168 | + await ctx.interaction.reply({ poll }); |
| 169 | +}; |
| 170 | +``` |
| 171 | + |
| 172 | +## Poll with media |
| 173 | + |
| 174 | +Add images or other media to your poll questions: |
| 175 | + |
| 176 | +```tsx title="src/app/commands/media-poll.tsx" |
| 177 | +import { |
| 178 | + CommandData, |
| 179 | + Poll, |
| 180 | + PollQuestion, |
| 181 | + PollAnswer, |
| 182 | + ChatInputCommand, |
| 183 | +} from 'commandkit'; |
| 184 | +import { PollData } from 'discord.js'; |
| 185 | + |
| 186 | +export const command: CommandData = { |
| 187 | + name: 'mediapoll', |
| 188 | + description: 'Create a poll with media', |
| 189 | +}; |
| 190 | + |
| 191 | +export const chatInput: ChatInputCommand = async (ctx) => { |
| 192 | + const poll: PollData = ( |
| 193 | + <Poll duration={24}> |
| 194 | + <PollQuestion |
| 195 | + text="Which logo design do you prefer?" |
| 196 | + media={{ |
| 197 | + type: 1, |
| 198 | + url: "https://example.com/logos.png" |
| 199 | + }} |
| 200 | + > |
| 201 | + Choose your favorite logo design |
| 202 | + </PollQuestion> |
| 203 | + <PollAnswer emoji="🎨">Design A</PollAnswer> |
| 204 | + <PollAnswer emoji="🖼️">Design B</PollAnswer> |
| 205 | + <PollAnswer emoji="✨">Design C</PollAnswer> |
| 206 | + </Poll> |
| 207 | + ); |
| 208 | + |
| 209 | + await ctx.interaction.reply({ poll }); |
| 210 | +}; |
| 211 | +``` |
0 commit comments