Skip to content

Commit

Permalink
Document polls. (discord#6746)
Browse files Browse the repository at this point in the history
  • Loading branch information
tpcstld authored Apr 18, 2024
1 parent cf50243 commit df7d365
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 20 deletions.
1 change: 1 addition & 0 deletions docs/interactions/Receiving_and_Responding.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ Not all message fields are currently supported.
| flags? | integer | [message flags](#DOCS_RESOURCES_CHANNEL/message-object-message-flags) combined as a [bitfield](https://en.wikipedia.org/wiki/Bit_field) (only `SUPPRESS_EMBEDS`, `EPHEMERAL`, and `SUPPRESS_NOTIFICATIONS` can be set) |
| components? | array of [components](#DOCS_INTERACTIONS_MESSAGE_COMPONENTS/) | message components |
| attachments? \* | array of partial [attachment](#DOCS_RESOURCES_CHANNEL/attachment-object) objects | attachment objects with filename and description |
| poll? | [poll](#DOCS_RESOURCES_POLL/poll-create-request-object) request object | A poll! |

\* See [Uploading Files](#DOCS_REFERENCE/uploading-files) for details.

Expand Down
8 changes: 5 additions & 3 deletions docs/resources/Channel.md
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ Represents a message sent in a channel within Discord.
> Fields specific to the `MESSAGE_CREATE` and `MESSAGE_UPDATE` events are listed in the [Gateway documentation](#DOCS_TOPICS_GATEWAY_EVENTS/message-create).
> warn
> `content`, `embeds`, `attachments`, and `components` require the [`MESSAGE_CONTENT` intent](#DOCS_TOPICS_GATEWAY/message-content-intent) to receive non-empty values.
> `content`, `embeds`, `attachments`, `components`, and `poll` require the [`MESSAGE_CONTENT` intent](#DOCS_TOPICS_GATEWAY/message-content-intent) to receive non-empty values.
| Field | Type | Description |
|-----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Expand Down Expand Up @@ -301,6 +301,7 @@ Represents a message sent in a channel within Discord.
| position? | integer | A generally increasing integer (there may be gaps or duplicates) that represents the approximate position of the message in a thread, it can be used to estimate the relative position of the message in a thread in company with `total_message_sent` on parent thread |
| role_subscription_data? | [role subscription data](#DOCS_RESOURCES_CHANNEL/role-subscription-data-object) object | data of the role subscription purchase or renewal that prompted this ROLE_SUBSCRIPTION_PURCHASE message |
| resolved? | [resolved](#DOCS_INTERACTIONS_RECEIVING_AND_RESPONDING/interaction-object-resolved-data-structure) data | data for users, members, channels, and roles in the message's [auto-populated select menus](#DOCS_INTERACTIONS_MESSAGE_COMPONENTS/select-menus) |
| poll? | [poll](#DOCS_RESOURCES_POLL/poll-create-request-object) request object | A poll! |


\* The author object follows the structure of the user object, but is only a valid user in the case where the message is generated by a user or bot user. If the message is generated by a webhook, the author object corresponds to the webhook's id, username, and avatar. You can tell if a message is generated by a webhook by checking for the `webhook_id` on the message object.
Expand Down Expand Up @@ -1068,7 +1069,7 @@ Files must be attached using a `multipart/form-data` body as described in [Uploa
###### JSON/Form Params

> info
> When creating a message, apps must provide a value for **at least one of** `content`, `embeds`, `sticker_ids`, `components`, or `files[n]`.
> When creating a message, apps must provide a value for **at least one of** `content`, `embeds`, `sticker_ids`, `components`, `files[n]`, or `poll`.
| Field | Type | Description |
|--------------------|---------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
Expand All @@ -1085,8 +1086,9 @@ Files must be attached using a `multipart/form-data` body as described in [Uploa
| attachments? | array of partial [attachment](#DOCS_RESOURCES_CHANNEL/attachment-object) objects | Attachment objects with filename and description. See [Uploading Files](#DOCS_REFERENCE/uploading-files) |
| flags? | integer | [Message flags](#DOCS_RESOURCES_CHANNEL/message-object-message-flags) combined as a [bitfield](https://en.wikipedia.org/wiki/Bit_field) (only `SUPPRESS_EMBEDS` and `SUPPRESS_NOTIFICATIONS` can be set) |
| enforce_nonce? | boolean | If true and nonce is present, it will be checked for uniqueness in the past few minutes. If another message was created by the same author with the same nonce, that message will be returned and no new message will be created. |
| poll? | [poll](#DOCS_RESOURCES_POLL/poll-create-request-object) request object | A poll! |

\* At least one of `content`, `embeds`, `sticker_ids`, `components`, or `files[n]` is required.
\* At least one of `content`, `embeds`, `sticker_ids`, `components`, `files[n]`, or `poll` is required.

###### Example Request Body (application/json)

Expand Down
143 changes: 143 additions & 0 deletions docs/resources/Poll.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Poll Resource

A poll is... well... a poll! It holds information about a poll!

![Example message containing a poll](example-poll.png)

### Poll Object

The poll object has a lot of levels and nested structures. It was also designed
to support future extensibility, so some fields may appear to be more complex than
necessary.

###### Poll Object Structure

| Field | Type | Description |
|-------------------|-----------------------------------------------------------------------------------------------------|-----------------------------------------------------------------|
| question | [Poll Media Object](#DOCS_RESOURCES_POLL/poll-media-object-poll-media-object-structure) | The question of the poll. Only `text` is supported. |
| answers | List of [Poll Answer Objects](#DOCS_RESOURCES_POLL/poll-answer-object-poll-answer-object-structure) | Each of the answers available in the poll. |
| expiry | ?IS08601 timestamp | The time when the poll ends. |
| allow_multiselect | boolean | Whether a user can select multiple answers |
| layout_type | integer | The [layout type](#DOCS_RESOURCES_POLL/layout-type) of the poll |
| results? | [Poll Results Object](#DOCS_RESOURCES_POLL/poll-results-object-poll-results-object-structure) | The results of the poll |

`expiry` is marked as nullable to support non-expiring polls in the future, but all polls have an expiry currently.

### Poll Create Request Object

This is the request object used when creating a poll across the different endpoints.
It is similar but not exactly identical to the main [poll object](#DOCS_RESOURCES_POLL/poll-object-poll-object-structure).
The main difference is that the request has `duration` which eventually becomes `expiry`.

###### Poll Create Request Object Structure

| Field | Type | Description |
|-------------------|-----------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------|
| question | [Poll Media Object](#DOCS_RESOURCES_POLL/poll-media-object-poll-media-object-structure) | The question of the poll. Only `text` is supported. |
| answers | List of [Poll Answer Objects](#DOCS_RESOURCES_POLL/poll-answer-object-poll-answer-object-structure) | Each of the answers available in the poll, up to 10 |
| duration | integer | Number of hours the poll should be open for, up to 7 days |
| allow_multiselect | boolean | Whether a user can select multiple answers |
| layout_type? | integer | The [layout type](#DOCS_RESOURCES_POLL/layout-type) of the poll. Defaults to... DEFAULT! |

### Layout Type

We might have different layouts for polls in the future.
For now though, this number will be 1.

| Type | ID | Description |
|---------|----|--------------------------------|
| DEFAULT | 1 | The, uhm, default layout type. |

### Poll Media Object

The poll media object is a common object that backs both the question and answers.
The intention is that it allows us to extensibly add new ways to display things in the future.
For now, `question` only supports `text`, while answers can have an optional `emoji`.

###### Poll Media Object Structure

| Field | Type | Description |
|--------|-----------------------------------------------------|------------------------|
| text? | string | The text of the field |
| emoji? | partial [emoji](#DOCS_RESOURCES_EMOJI/emoji-object) | The emoji of the field |

`text` should always be non-null for both questions and answers, but please do not depend on that in the future.
The maximum length of `text` is 300 for the question, and 55 for any answer.

When creating a poll answer with an emoji, one only needs to send either the `id` (custom emoji) or `name` (default emoji) as the only field.

### Poll Answer Object

The `answer_id` is a number that labels each answer.
As an implementation detail, it currently starts at 1 for the first answer and goes up sequentially.
We recommend against depending on this sequence.

Currently, there is a maximum of 10 answers per poll.

###### Poll Answer Object Structure

| Field | Type | Description |
|-------------|-----------------------------------------------------------------------------------------|------------------------|
| answer_id\* | integer | The ID of the answer |
| poll_media | [Poll Media Object](#DOCS_RESOURCES_POLL/poll-media-object-poll-media-object-structure) | The data of the answer |

\* Only sent as part of responses from Discord's API/Gateway.

### Poll Results Object

In a nutshell, this contains the number of votes for each answer.

The `results` field may be not present in certain responses where, as an implementation detail, we do not fetch the poll results in our backend.
This should be treated as "unknown results", as opposed to "no results". You can keep using the results if you have previously received them through other means.

Also due to the intricacies of counting at scale, while a poll is in progress the results may not be perfectly accurate.
They usually are accurate, and shouldn't deviate significantly -- it's just difficult to make guarantees.

To compensate for this, after a poll is finished there is a background job which performs a final, accurate tally of votes.
This tally concludes once `is_finalized` is `true`. Polls that have ended will also always contain results.

If `answer_counts` does not contain an entry for a particular answer, then there are no votes for that answer.

###### Poll Results Object Structure

| Field | Type | Description |
|---------------|-----------------------------------------------------------------------------------------------------------------|-----------------------------------------------|
| is_finalized | boolean | Whether the votes have been precisely counted |
| answer_counts | List of [Poll Answer Count Object](#DOCS_RESOURCES_POLL/poll-results-object-poll-answer-count-object-structure) | The counts for each answer |

###### Poll Answer Count Object Structure

| Field | Type | Description |
|----------|---------|------------------------------------------------|
| id | integer | The `answer_id` |
| count | integer | The number of votes for this answer |
| me_voted | boolean | Whether the current user voted for this answer |

# Poll Endpoints

For creating a poll, see [Create Message](#DOCS_RESOURCES_CHANNEL/create-message). After creation, the poll message cannot be edited.

Apps are not allowed to vote on polls. No rights! :)

## Get Answer Voters % GET /channels/{channel.id#DOCS_RESOURCES_CHANNEL/channel-object}/polls/{message.id#DOCS_RESOURCES_CHANNEL/message-object}/answers/{answer_id#DOCS_RESOURCES_POLL/poll-answer-object}

Get a list of users that voted for this specific answer.

###### Query String Params

| Field | Type | Description | Default |
|--------|-----------|---------------------------------------|---------|
| after? | snowflake | Get users after this user ID | absent |
| limit? | integer | Max number of users to return (1-100) | 25 |

###### Response Body

| Field | Type | Description |
|-------|---------------------------------------------------|---------------------------------|
| users | array of [user](#DOCS_RESOURCES_USER/user-object) | Users who voted for this answer |

## End Poll % POST /channels/{channel.id#DOCS_RESOURCES_CHANNEL/channel-object}/polls/{message.id#DOCS_RESOURCES_CHANNEL/message-object}/expire

Immediately ends the poll. You cannot end polls from other users.

Returns a [message](#DOCS_RESOURCES_CHANNEL/message-object) object. Fires a [Message Update](#DOCS_TOPICS_GATEWAY_EVENTS/message-update) Gateway event.
Loading

0 comments on commit df7d365

Please sign in to comment.