Skip to content

Commit

Permalink
add PollCreateBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
mlnrDev committed Apr 22, 2024
1 parent b666515 commit c51764a
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
21 changes: 20 additions & 1 deletion discord/poll.go
Expand Up @@ -3,6 +3,7 @@ package discord
import (
"time"

"github.com/disgoorg/json"
"github.com/disgoorg/snowflake/v2"
)

Expand All @@ -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"`
Expand Down
61 changes: 61 additions & 0 deletions 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
}

0 comments on commit c51764a

Please sign in to comment.