seyfert/src/builders/Poll.ts
JustEvil d9aa4a56d0
refactor: moved functions (#252)
* refactor: moved functions
- Moved functions
- Fixed imports
- Removed duplicated file

* chore: apply formatting

* fix: import
2024-08-22 17:32:55 -04:00

52 lines
1.5 KiB
TypeScript

import { type APIPollMedia, PollLayoutType, type RESTAPIPollCreate } from '../types';
import { resolvePartialEmoji, type DeepPartial, type EmojiResolvable, type RestOrArray } from '../common';
export class PollBuilder {
constructor(public data: DeepPartial<RESTAPIPollCreate> = {}) {
this.data.layout_type = PollLayoutType.Default;
}
addAnswers(...answers: RestOrArray<PollMedia>) {
this.data.answers = (this.data.answers ?? []).concat(
answers.flat().map(x => ({ poll_media: this.resolvedPollMedia(x) })),
);
return this;
}
setAnswers(...answers: RestOrArray<PollMedia>) {
this.data.answers = answers.flat().map(x => ({ poll_media: this.resolvedPollMedia(x) }));
return this;
}
setQuestion(data: PollMedia) {
this.data.question ??= {};
const { emoji, text } = this.resolvedPollMedia(data);
this.data.question.text = text;
this.data.question.emoji = emoji;
return this;
}
setDuration(hours: number) {
this.data.duration = hours;
return this;
}
allowMultiselect(value = true) {
this.data.allow_multiselect = value;
return this;
}
toJSON(): RESTAPIPollCreate {
return { ...this.data } as RESTAPIPollCreate;
}
private resolvedPollMedia(data: PollMedia) {
if (!data.emoji) return { text: data.text };
const resolve = resolvePartialEmoji(data.emoji);
if (!resolve) throw new Error('Invalid Emoji');
return { text: data.text, emoji: resolve };
}
}
export type PollMedia = Omit<APIPollMedia, 'emoji'> & { emoji?: EmojiResolvable };