-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add polls * fix AnswerID type * remove omitempty for now * add current intent requirement * add PollCreate * fix ExpirePoll endpoint route * add intents * split poll events into DM and guild * consistent wording * add answer votes paging * make Expiry a pointer * add permission * fix Results type * add PollCreateBuilder * address review * you might wanna build the builder idk * add ClearPoll instead of accepting a pointer * Update discord/poll_create_builder.go
- Loading branch information
Sebastian
authored
Apr 22, 2024
1 parent
1c2e34f
commit a587875
Showing
21 changed files
with
486 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package discord | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/disgoorg/json" | ||
"github.com/disgoorg/snowflake/v2" | ||
) | ||
|
||
type Poll struct { | ||
Question PollMedia `json:"question"` | ||
Answers []PollAnswer `json:"answers"` | ||
Expiry *time.Time `json:"expiry"` | ||
AllowMultiselect bool `json:"allow_multiselect"` | ||
LayoutType PollLayoutType `json:"layout_type"` | ||
Results *PollResults `json:"results"` | ||
} | ||
|
||
type PollCreate struct { | ||
Question PollMedia `json:"question"` | ||
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"` | ||
} | ||
|
||
type PollAnswer struct { | ||
AnswerID *int `json:"answer_id,omitempty"` | ||
PollMedia PollMedia `json:"poll_media"` | ||
} | ||
|
||
type PollResults struct { | ||
IsFinalized bool `json:"is_finalized"` | ||
AnswerCounts []PollAnswerCount `json:"answer_counts"` | ||
} | ||
|
||
type PollAnswerCount struct { | ||
ID snowflake.ID `json:"id"` | ||
Count int `json:"count"` | ||
MeVoted bool `json:"me_voted"` | ||
} | ||
|
||
type PollLayoutType int | ||
|
||
const ( | ||
PollLayoutTypeDefault PollLayoutType = iota + 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 hours) | ||
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 | ||
} | ||
|
||
// Build builds the PollCreateBuilder to a PollCreate struct | ||
func (b *PollCreateBuilder) Build() PollCreate { | ||
return b.PollCreate | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/disgoorg/snowflake/v2" | ||
) | ||
|
||
// GenericDMMessagePollVote is called upon receiving DMMessagePollVoteAdd or DMMessagePollVoteRemove (requires gateway.IntentDirectMessagePolls) | ||
type GenericDMMessagePollVote struct { | ||
*GenericEvent | ||
UserID snowflake.ID | ||
ChannelID snowflake.ID | ||
MessageID snowflake.ID | ||
AnswerID int | ||
} | ||
|
||
// DMMessagePollVoteAdd indicates that a discord.User voted on a discord.Poll in a DM (requires gateway.IntentDirectMessagePolls) | ||
type DMMessagePollVoteAdd struct { | ||
*GenericDMMessagePollVote | ||
} | ||
|
||
// DMMessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll in a DM (requires gateway.IntentDirectMessagePolls) | ||
type DMMessagePollVoteRemove struct { | ||
*GenericDMMessagePollVote | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/disgoorg/disgo/discord" | ||
"github.com/disgoorg/snowflake/v2" | ||
) | ||
|
||
// GenericGuildMessagePollVote is called upon receiving GuildMessagePollVoteAdd or GuildMessagePollVoteRemove (requires gateway.IntentGuildMessagePolls) | ||
type GenericGuildMessagePollVote struct { | ||
*GenericEvent | ||
UserID snowflake.ID | ||
ChannelID snowflake.ID | ||
MessageID snowflake.ID | ||
GuildID snowflake.ID | ||
AnswerID int | ||
} | ||
|
||
// Guild returns the discord.Guild where the GenericGuildMessagePollVote happened | ||
func (e *GenericGuildMessagePollVote) Guild() (discord.Guild, bool) { | ||
return e.Client().Caches().Guild(e.GuildID) | ||
} | ||
|
||
// Channel returns the discord.GuildMessageChannel where the GenericGuildMessagePollVote happened | ||
func (e *GenericGuildMessagePollVote) Channel() (discord.GuildMessageChannel, bool) { | ||
return e.Client().Caches().GuildMessageChannel(e.ChannelID) | ||
} | ||
|
||
// GuildMessagePollVoteAdd indicates that a discord.User voted on a discord.Poll in a discord.Guild (requires gateway.IntentGuildMessagePolls) | ||
type GuildMessagePollVoteAdd struct { | ||
*GenericGuildMessagePollVote | ||
} | ||
|
||
// GuildMessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll in a discord.Guild (requires gateway.IntentGuildMessagePolls) | ||
type GuildMessagePollVoteRemove struct { | ||
*GenericGuildMessagePollVote | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/disgoorg/disgo/discord" | ||
"github.com/disgoorg/snowflake/v2" | ||
) | ||
|
||
// GenericMessagePollVote is a generic poll vote event (requires gateway.IntentGuildMessagePolls and/or gateway.IntentDirectMessagePolls) | ||
type GenericMessagePollVote struct { | ||
*GenericEvent | ||
UserID snowflake.ID | ||
ChannelID snowflake.ID | ||
MessageID snowflake.ID | ||
GuildID *snowflake.ID | ||
AnswerID int | ||
} | ||
|
||
// Guild returns the discord.Guild where the GenericMessagePollVote happened or empty if it happened in DMs | ||
func (e *GenericMessagePollVote) Guild() (discord.Guild, bool) { | ||
if e.GuildID == nil { | ||
return discord.Guild{}, false | ||
} | ||
return e.Client().Caches().Guild(*e.GuildID) | ||
} | ||
|
||
// MessagePollVoteAdd indicates that a discord.User voted on a discord.Poll (requires gateway.IntentGuildMessagePolls and/or gateway.IntentDirectMessagePolls) | ||
type MessagePollVoteAdd struct { | ||
*GenericMessagePollVote | ||
} | ||
|
||
// MessagePollVoteRemove indicates that a discord.User removed their vote on a discord.Poll (requires gateway.IntentGuildMessagePolls and/or gateway.IntentDirectMessagePolls) | ||
type MessagePollVoteRemove struct { | ||
*GenericMessagePollVote | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.