-
Notifications
You must be signed in to change notification settings - Fork 51
Add polls #346
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add polls #346
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
f842904
Add polls
05382f1
fix AnswerID type
43f6126
remove omitempty for now
a04c409
add current intent requirement
e2d7f3a
add PollCreate
d2d286b
fix ExpirePoll endpoint route
8a73887
add intents
e25946c
split poll events into DM and guild
3479848
consistent wording
c7e441c
add answer votes paging
d2c169d
make Expiry a pointer
d771026
add permission
b666515
fix Results type
c51764a
add PollCreateBuilder
6f85c0f
address review
fef1c94
you might wanna build the builder idk
fce3f55
add ClearPoll instead of accepting a pointer
c812a4f
Update discord/poll_create_builder.go
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 { | ||
topi314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*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 hidden or 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 hidden or 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 { | ||
topi314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.