Guides
Poll
Make a /poll command that posts a real Discord poll, or a quick vote with buttons.
You will make /poll. Members run it with a question and some answers. The bot posts a poll that everyone can vote on.
There are two ways to do it:
| Way | Best for |
|---|---|
| Discord poll | Most polls. Discord counts the votes and shows the result bar. Lasts from 1 hour to 32 days. |
| Button vote | A quick yes/no vote that ends after a few minutes. |
Discord poll#
1. Create the command#
Open Create → Slash Command and switch to Advanced at the top of the form.
| Field | Value |
|---|---|
| Command Name | poll |
| Description | Start a poll |
Under Behavior, turn on Only the user sees the reply. The poll itself is public. Only the "Poll posted" note is private.
2. Add the arguments#
Under Arguments, add three:
| Type | Name | Required | Description |
|---|---|---|---|
| Text | question | Yes | What to ask |
| Text | answers | Yes | Answers, split by commas |
| Integer | hours | No | How long it stays open |
3. Add the code#
Open Pre-hook and paste:
choices = [a.strip() for a in answers.split(",") if a.strip()]
if len(choices) < 2:
set_error("Give at least two answers, split by commas.")
await create_poll(question, choices, duration=hours or 24)
set_content("Poll posted!")Leave the Content / Reply Message box empty. set_content writes the reply.
4. Try it#
Save, then run:
/poll question: Pizza or burgers? answers: Pizza, Burgers, BothDiscord's poll limits
- Up to 10 answers. Extra answers are dropped.
- Each answer can be up to 55 characters. The question can be up to 300.
durationis in hours, from 1 to 768 (32 days). The default is 24.
Let members pick more than one#
Add multiselect=True:
await create_poll(question, choices, duration=hours or 24, multiselect=True)Post it in another channel#
Add channel=. It takes a channel, its ID or its name:
await create_poll(question, choices, channel="announcements")Button vote#
This version puts Yes and No buttons on the reply. Each button shows its count. A member can change their vote. Each member counts once.
Make a new slash command named vote with one Text argument named question. Leave Only the user sees the reply off, so everyone can see the buttons.
Pre-hook#
class Vote(View):
def __init__(self):
self.votes = {}
async def cast(self, interaction, choice):
await defer(interaction, action="update")
self.votes[str(interaction.user.id)] = choice
yes = len([v for v in self.votes.values() if v == "yes"])
no = len(self.votes) - yes
self.update_component("yes", label=f"Yes ({yes})")
self.update_component("no", label=f"No ({no})")
# Edit through the newest click, so the edit never expires.
self.interaction = interaction
await self.update_view()
@button(label="Yes (0)", style="success", custom_id="yes")
async def yes(self, interaction, component):
await self.cast(interaction, "yes")
@button(label="No (0)", style="danger", custom_id="no")
async def no(self, interaction, component):
await self.cast(interaction, "no")
view = Vote()
view.interaction = interaction
set_view(view)
set_content(f"**{question}**\nClick to vote.")Post-hook#
The post-hook runs when the vote ends. Voting ends after 3 minutes with no new votes. Each new vote starts the 3 minutes again.
yes = len([v for v in view.votes.values() if v == "yes"])
no = len(view.votes) - yes
await respond_interaction(
view.interaction,
content=f"**{question}**\nVoting closed. Yes: {yes} · No: {no}",
)respond_interaction changes the text and removes the buttons.
Tip
Need a vote that stays open for hours or days? Use the Discord poll above. Buttons on a command reply are for short votes.
Learn more#
- Buttons and menus explains how a View works.
- Server events and polls lists every argument of
create_poll. - Views lists
update_component,update_viewand the other View tools.