|
| 1 | +package discord |
| 2 | + |
| 3 | +import ( |
| 4 | + "time" |
| 5 | + |
| 6 | + "github.com/disgoorg/json" |
| 7 | + "github.com/disgoorg/snowflake/v2" |
| 8 | +) |
| 9 | + |
| 10 | +type Poll struct { |
| 11 | + Question PollMedia `json:"question"` |
| 12 | + Answers []PollAnswer `json:"answers"` |
| 13 | + Expiry *time.Time `json:"expiry"` |
| 14 | + AllowMultiselect bool `json:"allow_multiselect"` |
| 15 | + LayoutType PollLayoutType `json:"layout_type"` |
| 16 | + Results *PollResults `json:"results"` |
| 17 | +} |
| 18 | + |
| 19 | +type PollCreate struct { |
| 20 | + Question PollMedia `json:"question"` |
| 21 | + Answers []PollMedia `json:"-"` |
| 22 | + Duration int `json:"duration"` |
| 23 | + AllowMultiselect bool `json:"allow_multiselect"` |
| 24 | + LayoutType PollLayoutType `json:"layout_type,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +func (p PollCreate) MarshalJSON() ([]byte, error) { |
| 28 | + type pollCreate PollCreate |
| 29 | + |
| 30 | + answers := make([]PollAnswer, 0, len(p.Answers)) |
| 31 | + for _, answer := range p.Answers { |
| 32 | + answers = append(answers, PollAnswer{ |
| 33 | + PollMedia: answer, |
| 34 | + }) |
| 35 | + } |
| 36 | + return json.Marshal(struct { |
| 37 | + Answers []PollAnswer `json:"answers"` |
| 38 | + pollCreate |
| 39 | + }{ |
| 40 | + Answers: answers, |
| 41 | + pollCreate: pollCreate(p), |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +type PollMedia struct { |
| 46 | + Text *string `json:"text"` |
| 47 | + Emoji *PartialEmoji `json:"emoji,omitempty"` |
| 48 | +} |
| 49 | + |
| 50 | +type PollAnswer struct { |
| 51 | + AnswerID *int `json:"answer_id,omitempty"` |
| 52 | + PollMedia PollMedia `json:"poll_media"` |
| 53 | +} |
| 54 | + |
| 55 | +type PollResults struct { |
| 56 | + IsFinalized bool `json:"is_finalized"` |
| 57 | + AnswerCounts []PollAnswerCount `json:"answer_counts"` |
| 58 | +} |
| 59 | + |
| 60 | +type PollAnswerCount struct { |
| 61 | + ID snowflake.ID `json:"id"` |
| 62 | + Count int `json:"count"` |
| 63 | + MeVoted bool `json:"me_voted"` |
| 64 | +} |
| 65 | + |
| 66 | +type PollLayoutType int |
| 67 | + |
| 68 | +const ( |
| 69 | + PollLayoutTypeDefault PollLayoutType = iota + 1 |
| 70 | +) |
0 commit comments