fix: poll builders (#10783)

* fix: poll builders

- Fixed validations
- Added missing documentation
- Removed redundant code
- Consistency™️

* fix: tests

* feat: missing answers test
This commit is contained in:
Almeida
2025-03-01 14:57:00 +00:00
committed by GitHub
parent 88bfeaab22
commit d1f56ffb2a
7 changed files with 73 additions and 33 deletions

View File

@@ -6,14 +6,14 @@ export const pollQuestionPredicate = z.object({ text: z.string().min(1).max(300)
export const pollAnswerMediaPredicate = z.object({
text: z.string().min(1).max(55),
emoji: emojiPredicate.nullish(),
emoji: emojiPredicate.optional(),
});
export const pollAnswerPredicate = z.object({ poll_media: pollAnswerMediaPredicate });
export const pollPredicate = z.object({
question: pollQuestionPredicate,
answers: z.array(pollAnswerPredicate).max(10),
answers: z.array(pollAnswerPredicate).min(1).max(10),
duration: z.number().min(1).max(768).optional(),
allow_multiselect: z.boolean().optional(),
layout_type: z.nativeEnum(PollLayoutType).optional(),

View File

@@ -161,7 +161,7 @@ export class PollBuilder implements JSONEncodable<RESTAPIPoll> {
* @param updater - The function to update the question with
*/
public updateQuestion(updater: (builder: PollQuestionBuilder) => void): this {
updater((this.data.question ??= new PollQuestionBuilder()));
updater(this.data.question);
return this;
}

View File

@@ -9,8 +9,16 @@ export interface PollAnswerData extends Omit<APIPollAnswer, 'answer_id' | 'poll_
}
export class PollAnswerBuilder {
/**
* The API data associated with this poll answer.
*/
protected readonly data: PollAnswerData;
/**
* Creates a new poll answer from API data.
*
* @param data - The API data to create this poll answer with
*/
public constructor(data: Partial<Omit<APIPollAnswer, 'answer_id'>> = {}) {
this.data = {
...structuredClone(data),
@@ -35,8 +43,9 @@ export class PollAnswerBuilder {
*
* @param updater - The function to update the media with
*/
public updateMedia(updater: (builder: PollAnswerMediaBuilder) => void) {
updater((this.data.poll_media ??= new PollAnswerMediaBuilder()));
public updateMedia(updater: (builder: PollAnswerMediaBuilder) => void): this {
updater(this.data.poll_media);
return this;
}
/**
@@ -47,10 +56,12 @@ export class PollAnswerBuilder {
* @param validationOverride - Force validation to run/not run regardless of your global preference
*/
public toJSON(validationOverride?: boolean): Omit<APIPollAnswer, 'answer_id'> {
const { poll_media, ...rest } = this.data;
const data = {
...structuredClone(this.data),
...structuredClone(rest),
// Disable validation because the pollAnswerPredicate below will validate this as well
poll_media: this.data.poll_media?.toJSON(false),
poll_media: poll_media.toJSON(false),
};
validate(pollAnswerPredicate, data, validationOverride);

View File

@@ -4,11 +4,11 @@ import { pollAnswerMediaPredicate } from './Assertions.js';
import { PollMediaBuilder } from './PollMedia.js';
/**
* A builder that creates API-compatible JSON data for poll answers.
* A builder that creates API-compatible JSON data for the media of a poll answer.
*/
export class PollAnswerMediaBuilder extends PollMediaBuilder {
/**
* Sets the emoji for this poll answer.
* Sets the emoji for this poll answer media.
*
* @param emoji - The emoji to use
*/
@@ -18,18 +18,21 @@ export class PollAnswerMediaBuilder extends PollMediaBuilder {
}
/**
* Clears the emoji for this poll answer.
* Clears the emoji for this poll answer media.
*/
public clearEmoji(): this {
this.data.emoji = undefined;
return this;
}
/**
* {@inheritDoc PollMediaBuilder.toJSON}
*/
public override toJSON(validationOverride?: boolean): APIPollMedia {
const clone = structuredClone(this.data);
validate(pollAnswerMediaPredicate, clone, validationOverride);
return clone as APIPollMedia;
return clone;
}
}

View File

@@ -1,6 +1,12 @@
import type { APIPollMedia } from 'discord-api-types/v10';
/**
* The base poll media builder that contains common symbols for poll media builders.
*/
export abstract class PollMediaBuilder {
/**
* The API data associated with this poll media.
*/
protected readonly data: Partial<APIPollMedia>;
/**

View File

@@ -7,11 +7,14 @@ import { PollMediaBuilder } from './PollMedia.js';
* A builder that creates API-compatible JSON data for a poll question.
*/
export class PollQuestionBuilder extends PollMediaBuilder {
/**
* {@inheritDoc PollMediaBuilder.toJSON}
*/
public override toJSON(validationOverride?: boolean): Omit<APIPollMedia, 'emoji'> {
const clone = structuredClone(this.data);
validate(pollQuestionPredicate, clone, validationOverride);
return clone as Omit<APIPollMedia, 'emoji'>;
return clone;
}
}