Polls
Working with polls in Carbon.
Carbon provides robust support for Discord Polls, allowing you to both send new polls and interact with existing ones. This guide will walk you through how to work with polls in your Carbon bot.
The Poll
Class
The Poll
class represents a poll that has been received from Discord, typically accessed through a Message
object. This class is designed for reading and interacting with existing polls, not for creating new ones.
The Poll
class integrates seamlessly with Discord's message system, allowing you to access polls through the poll
property of any Message
object. Most properties are implemented as getters, providing safe, read-only access to the poll's data. The class also includes several interactive methods for common operations like ending polls or fetching voter information.
Sending Polls
To create and send a new poll, include a poll
object in your MessagePayload
when sending a message. You can do this through methods like interaction.reply()
, message.reply()
, or channel.send()
.
Here's a practical example of a command that creates polls:
This example shows a complete poll command that:
- Takes a question, comma-separated answers, duration, and optional multiselect flag as input
- Validates the number of answers (2-10) and their length (max 55 chars)
- Creates and sends a poll with the specified parameters
- Uses proper error handling for invalid inputs
Working with Received Polls
When you receive a message containing a poll, you can access and interact with it through the Poll
class.
The Poll
class provides a comprehensive set of properties and methods for working with polls. The question
property gives you access to the poll question and any associated emoji, while the answers
array contains all poll answers with their IDs and media content. You can check when a poll expires through the expiry
property, which provides an ISO8601 timestamp, and determine if multiple answers are allowed with allowMultiselect
. The layoutType
property lets you know the poll's current layout, and when a poll is finalized, you can access vote counts through the results
property. The isFinalized
property helps you determine if a poll has ended.
The class also includes several useful methods. The getAnswerVoters()
method allows you to fetch the list of users who voted for a specific answer. If you need to end a poll, the end()
method will do that and return the updated message data.
Last updated on