PollBuilder

export declare class PollBuilder implements JSONEncodable<RESTAPIPoll>
export declare class PollBuilder implements JSONEncodable<RESTAPIPoll>
A builder that creates API-compatible JSON data for polls.
constructor(data?)
Creates a new poll from API data.
NameTypeOptionalDescription
dataPartial<RESTAPIPoll>YesThe API data to create this poll with
Readonly
answers:readonly PollAnswerBuilder[]
Gets the answers of this poll.
addAnswers(...answers):this
Appends answers to the poll.
Remarks
This method accepts either an array of answers or a variable number of answer parameters. The maximum amount of answers that can be added is 10.
Example
Using an array:
const answers: APIPollMedia[] = ...;
const poll = new PollBuilder()
.addAnswers(answers);
const answers: APIPollMedia[] = ...;
const poll = new PollBuilder()
.addAnswers(answers);
Example
Using rest parameters (variadic):
const poll = new PollBuilder()
.addAnswers(
{ text: 'Answer 1' },
{ text: 'Answer 2' },
);
const poll = new PollBuilder()
.addAnswers(
{ text: 'Answer 1' },
{ text: 'Answer 2' },
);
NameTypeOptionalDescription
...answersRestOrArray<Omit<APIPollAnswer, 'answer_id'> | PollAnswerBuilder | ((builder: PollAnswerBuilder) => PollAnswerBuilder)>NoThe answers to add
clearDuration():this
Clears the duration for this poll.
clearLayoutType():this
Clears the layout type for this poll.
setAnswers(...answers):this
Sets the answers for this poll.
Remarks
This method is an alias for spliceAnswers. More specifically, it splices the entire array of answers, replacing them with the provided answers.You can set a maximum of 10 answers.
NameTypeOptionalDescription
...answersRestOrArray<Omit<APIPollAnswer, 'answer_id'> | PollAnswerBuilder | ((builder: PollAnswerBuilder) => PollAnswerBuilder)>NoThe answers to set
setDuration(duration):this
Sets how long this poll will be open for in hours.
Remarks
Minimum duration is 1, with maximum duration being 768 (32 days). Default if none specified is 24 (one day).
NameTypeOptionalDescription
durationnumberNoThe amount of hours this poll will be open for
setLayoutType(type):this
Sets the layout type for this poll.
Remarks
This method is redundant while only one type of poll layout exists (PollLayoutType.Default) with Discord using that as the layout type if none is specified.
NameTypeOptionalDescription
typePollLayoutTypeNoThe type of poll layout to use
setMultiSelect(multiSelect?):this
Sets whether multi-select is enabled for this poll.
NameTypeOptionalDescription
multiSelectbooleanYesWhether to allow multi-select
setQuestion(options):this
Sets the question for this poll.
NameTypeOptionalDescription
optionsOmit<APIPollMedia, 'emoji'> | PollQuestionBuilder | ((builder: PollQuestionBuilder) => PollQuestionBuilder)NoThe data to use for this poll's question
spliceAnswers(index, deleteCount, ...answers):this
Removes, replaces, or inserts answers for this poll.
Remarks
This method behaves similarly to Array.prototype.splice(). The maximum amount of answers that can be added is 10.It's useful for modifying and adjusting order of the already-existing answers of a poll.
Example
Remove the first answer:
poll.spliceAnswers(0, 1);
poll.spliceAnswers(0, 1);
Example
Remove the first n answers:
const n = 4;
poll.spliceAnswers(0, n);
const n = 4;
poll.spliceAnswers(0, n);
Example
Remove the last answer:
poll.spliceAnswers(-1, 1);
poll.spliceAnswers(-1, 1);
NameTypeOptionalDescription
indexnumberNoThe index to start at
deleteCountnumberNoThe number of answers to remove
...answers(Omit<APIPollAnswer, 'answer_id'> | PollAnswerBuilder | ((builder: PollAnswerBuilder) => PollAnswerBuilder))[]NoThe replacing answer objects
toJSON(validationOverride?):RESTAPIPoll
Serializes this builder to API-compatible JSON data.Note that by disabling validation, there is no guarantee that the resulting object will be valid.
NameTypeOptionalDescription
validationOverridebooleanYesForce validation to run/not run regardless of your global preference
updateQuestion(updater):this
Updates the question of this poll.
NameTypeOptionalDescription
updater(builder: PollQuestionBuilder) => voidNoThe function to update the question with