Polls
guideRead a poll attached to a message, list its voters, end it early, and react when someone votes.
This page covers a poll that is already on Discord: read its question and answers, list who voted, end it before its deadline, and react to votes as they come in. Sending one is covered in creating polls. You're assumed to have a bot named "my-bot" logged in and to know how to read a message.
How polls work
has a poll tells you whether a message carries one; when it does, poll of the message gives back a messagepoll with a poll question and a list of poll answers, along with multi answer, poll layout and poll expiration date.
on message received seen by "my-bot": if event-message has a poll: set {_poll} to poll of event-message reply with "%poll question of {_poll}%" loop poll answers of {_poll}: reply with "%answer text of loop-value%: %answer votes of loop-value% votes"
Answers
Each element of poll answers is a pollanswer.
Property | Returns |
|---|---|
the option's text | |
the number of votes it has, as a plain number | |
the emoji shown next to it, if the poll has one; none otherwise | |
its number, from 1 in the order the answers were declared | |
whether the bot itself voted for it |
answer votes never needs await: the count travels with the message itself, not a separate fetch. answer emote prints as the emoji itself, so it can go straight into a message.
on message received seen by "my-bot": if discord content of event-message is "!poll-winner": set {_poll} to poll of event-message set {_best} to first element of (poll answers of {_poll}) loop poll answers of {_poll}: if answer votes of loop-value > answer votes of {_best}: set {_best} to loop-value reply with "leading: %answer text of {_best}% (%answer votes of {_best}%)"
Listing voters
voters of poll answer %number/pollanswer% of %message% lists the users who picked one answer. The answer is a pollanswer or its answer id; the message is the one carrying the poll, not the poll itself. The list is fetched from Discord, every page of it, so the line requires await.
on message received seen by "my-bot": if discord content of event-message is "!voters": set {_m} to {poll::release} loop poll answers of (poll of {_m}): await set {_voters::*} to the voters of poll answer loop-value of {_m} reply with "%answer text of loop-value%: %{_voters::*}%" delete {_voters::*}
{poll::release} holds a message sent from a message builder, the only way to keep the poll's message. An answer number the poll does not have is a readable error (this poll has no answer numbered <n>).
[the] [poll] voters of [poll] answer %number/pollanswer% (of|in|from) [the] [message] %message%The users who voted for one answer of a poll. The answer is a poll answer object or its number (answers are numbered from 1 in the order they were declared); the message is the one carrying the poll, not the poll itself. Every page of voters is fetched.Ending a poll
end the poll of closes a poll before its deadline; nobody can vote on it after this runs.
on message received seen by "my-bot": if discord content of event-message is "!end-poll": end the poll of event-message
is expired is true once the poll's deadline has passed and no more votes are accepted; is finalized is true once Discord has settled the vote counts as definitive, which end the poll of also does immediately. end the poll of refuses a message with no poll, and a poll that is already expired or finalized.
end [the] poll of [the] [message] %message% [now]Ends the poll attached to a message immediately, preventing any further vote.Vote events
on message poll vote added and on message poll vote removed fire per vote. event-message, event-member and event-user are fetched from Discord before the trigger starts, so none of them need await (see cache and await); event-member is none for a vote cast in a DM.
Event | Values |
|---|---|
|
|
|
|
on message poll vote added seen by "my-bot": send "%event-user% voted on a poll in %event-channel%" to console
Neither event says which answer was voted on: only who voted, and on which message. Read the voters of each answer if you need to know which option changed.
event Poll Vote AddEvents[message] poll vote add[ed]Fired when a user adds their vote to a poll in a message.Errors you may see
Message (console) | Cause | Fix |
|---|---|---|
IllegalStateException: this message carries no poll |
| Check |
IllegalStateException: this poll is already over |
| Check |