From c51764ac789e160832ae823cb7cc060a10d62c93 Mon Sep 17 00:00:00 2001 From: mlnrDev Date: Mon, 22 Apr 2024 18:50:40 +0200 Subject: [PATCH] add PollCreateBuilder --- discord/poll.go | 21 +++++++++++- discord/poll_create_builder.go | 61 ++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 discord/poll_create_builder.go diff --git a/discord/poll.go b/discord/poll.go index 36695c63..03f1e019 100644 --- a/discord/poll.go +++ b/discord/poll.go @@ -3,6 +3,7 @@ package discord import ( "time" + "github.com/disgoorg/json" "github.com/disgoorg/snowflake/v2" ) @@ -17,12 +18,30 @@ type Poll struct { type PollCreate struct { Question PollMedia `json:"question"` - Answers []PollAnswer `json:"answers"` + Answers []PollMedia `json:"-"` Duration int `json:"duration"` AllowMultiselect bool `json:"allow_multiselect"` LayoutType PollLayoutType `json:"layout_type,omitempty"` } +func (p PollCreate) MarshalJSON() ([]byte, error) { + type pollCreate PollCreate + + answers := make([]PollAnswer, 0, len(p.Answers)) + for _, answer := range p.Answers { + answers = append(answers, PollAnswer{ + PollMedia: answer, + }) + } + return json.Marshal(struct { + Answers []PollAnswer `json:"answers"` + pollCreate + }{ + Answers: answers, + pollCreate: pollCreate(p), + }) +} + type PollMedia struct { Text *string `json:"text"` Emoji *PartialEmoji `json:"emoji,omitempty"` diff --git a/discord/poll_create_builder.go b/discord/poll_create_builder.go new file mode 100644 index 00000000..c6f8ecc0 --- /dev/null +++ b/discord/poll_create_builder.go @@ -0,0 +1,61 @@ +package discord + +// PollCreateBuilder helps create PollCreate structs easier +type PollCreateBuilder struct { + PollCreate +} + +// SetQuestion sets the question of the Poll +func (b *PollCreateBuilder) SetQuestion(text string) *PollCreateBuilder { + b.Question = PollMedia{ + Text: &text, + } + return b +} + +// SetPollAnswers sets the answers of the Poll +func (b *PollCreateBuilder) SetPollAnswers(answers ...PollMedia) *PollCreateBuilder { + b.Answers = answers + return b +} + +// AddPollAnswer adds an answer to the Poll +func (b *PollCreateBuilder) AddPollAnswer(text string, emoji *PartialEmoji) *PollCreateBuilder { + b.Answers = append(b.Answers, PollMedia{ + Text: &text, + Emoji: emoji, + }) + return b +} + +// RemoveAnswer removes an answer from the Poll +func (b *PollCreateBuilder) RemoveAnswer(i int) *PollCreateBuilder { + if len(b.Answers) > i { + b.Answers = append(b.Answers[:i], b.Answers[i+1:]...) + } + return b +} + +// ClearAnswers removes all answers of the Poll +func (b *PollCreateBuilder) ClearAnswers() *PollCreateBuilder { + b.Answers = []PollMedia{} + return b +} + +// SetDuration sets the duration of the Poll (in seconds) +func (b *PollCreateBuilder) SetDuration(duration int) *PollCreateBuilder { + b.Duration = duration + return b +} + +// SetAllowMultiselect sets whether users will be able to vote for more than one answer of the Poll +func (b *PollCreateBuilder) SetAllowMultiselect(multiselect bool) *PollCreateBuilder { + b.AllowMultiselect = multiselect + return b +} + +// SetLayoutType sets the layout of the Poll +func (b *PollCreateBuilder) SetLayoutType(layout PollLayoutType) *PollCreateBuilder { + b.LayoutType = layout + return b +}