-
Couldn't load subscription status.
- Fork 51
Implement collectors (Closes #60) #62
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
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
9426c1f
Add reaction collector
beeb32b
Add button collector
4961423
Add docs
beef6a3
Merge branch 'development' into feature/collectors
arynxd 700953d
Merge branch 'development' into feature/collectors
arynxd 3b0e428
Add select collector
e6c8c47
Fixed merge issue
e65951f
Fix docs
c8f816b
Fix docs
56ae9a0
Add command collector
d59cb98
Fix naming
86613bc
Add overloads
fff3bf2
Apply naming changes
arynxd 87f833b
Finish changes
7064d94
Change button collector naming
45d06cc
Merged refactor branch into collectors
arynxd ac4c91c
Merge branch 'development' into feature/collectors
topi314 23b3dd0
rework collectors
topi314 4433ca8
fix message collecto example
topi314 fabca79
added timeout to example
topi314 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "github.com/DisgoOrg/disgo/api" | ||
| "github.com/DisgoOrg/disgo/api/events" | ||
| ) | ||
|
|
||
| // NewButtonCollector gives you a channel to receive on and a function to close the collector | ||
| func NewButtonCollector(disgo api.Disgo, channelID api.Snowflake, guildID api.Snowflake, messageID api.Snowflake, filter ButtonFilter) (chan *api.ButtonInteraction, func()) { | ||
| ch := make(chan *api.ButtonInteraction) | ||
|
|
||
| col := &ButtonCollector{ | ||
| Filter: filter, | ||
| Channel: ch, | ||
| ChannelID: channelID, | ||
| GuildID: guildID, | ||
| MessageID: messageID, | ||
| } | ||
|
|
||
| cls := func() { | ||
| close(ch) | ||
| disgo.EventManager().RemoveEventListener(col) | ||
| } | ||
|
|
||
| col.Close = cls | ||
|
|
||
| disgo.EventManager().AddEventListeners(col) | ||
|
|
||
| return ch, cls | ||
| } | ||
|
|
||
| // NewButtonCollectorFromMessage is an overload of NewButtonCollector that takes an api.Message for information | ||
| //goland:noinspection GoUnusedExportedFunction | ||
| func NewButtonCollectorFromMessage(message *api.Message, filter ButtonFilter) (chan *api.ButtonInteraction, func()) { | ||
arynxd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return NewButtonCollector(message.Disgo, message.ChannelID, message.ID, *message.GuildID, filter) | ||
| } | ||
|
|
||
| // ButtonFilter used to filter api.ButtonInteraction for ButtonCollector | ||
| type ButtonFilter func(reaction *api.ButtonInteraction) bool | ||
|
|
||
| // ButtonCollector used to collect api.ButtonInteraction(s) from an api.Message using a ButtonFilter function | ||
| type ButtonCollector struct { | ||
arynxd marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Channel chan *api.ButtonInteraction | ||
| Filter ButtonFilter | ||
| Close func() | ||
| ChannelID api.Snowflake | ||
| GuildID api.Snowflake | ||
| MessageID api.Snowflake | ||
| } | ||
|
|
||
| // OnEvent used to get events for the ButtonCollector | ||
| func (b *ButtonCollector) OnEvent(e interface{}) { | ||
| if event, ok := e.(*events.ButtonClickEvent); ok { | ||
| if !b.Filter(event.ButtonInteraction) { | ||
| return | ||
| } | ||
|
|
||
| b.Channel <- event.ButtonInteraction | ||
| } else if event, ok := e.(*events.GuildChannelDeleteEvent); ok && event.ChannelID == b.ChannelID { | ||
| b.Close() | ||
| } else if event, ok := e.(events.GuildLeaveEvent); ok && event.GuildID == b.GuildID { | ||
| b.Close() | ||
| } else if event, ok := e.(events.MessageDeleteEvent); ok && event.MessageID == b.MessageID { | ||
| b.Close() | ||
| } | ||
| } | ||
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,60 @@ | ||
| package utils | ||
|
|
||
| import ( | ||
| "github.com/DisgoOrg/disgo/api" | ||
| "github.com/DisgoOrg/disgo/api/events" | ||
| ) | ||
|
|
||
| // NewCommandCollectorFromMember is an overload of NewCommandCollector that takes an api.Member for information | ||
| //goland:noinspection GoUnusedExportedFunction | ||
| func NewCommandCollectorFromMember(member *api.Member, filter CommandFilter) (chan *api.CommandInteraction, func()) { | ||
| return NewCommandCollector(member.Disgo, member.GuildID, member.User.ID, filter) | ||
| } | ||
|
|
||
| // NewCommandCollector gives you a channel to receive on and a function to close the collector | ||
| func NewCommandCollector(disgo api.Disgo, guildID api.Snowflake, memberID api.Snowflake, filter CommandFilter) (chan *api.CommandInteraction, func()) { | ||
| ch := make(chan *api.CommandInteraction) | ||
|
|
||
| col := &CommandCollector{ | ||
| Filter: filter, | ||
| Channel: ch, | ||
| GuildID: guildID, | ||
| MemberID: memberID, | ||
| } | ||
|
|
||
| cls := func() { | ||
| close(ch) | ||
| disgo.EventManager().RemoveEventListener(col) | ||
| } | ||
|
|
||
| col.Close = cls | ||
|
|
||
| disgo.EventManager().AddEventListeners(col) | ||
|
|
||
| return ch, cls | ||
| } | ||
|
|
||
| // CommandFilter used to filter api.CommandInteraction in a CommandCollector | ||
| type CommandFilter func(reaction *api.CommandInteraction) bool | ||
|
|
||
| // CommandCollector used to collect api.CommandInteraction(s) using a CommandFilter function | ||
| type CommandCollector struct { | ||
| Channel chan *api.CommandInteraction | ||
| Filter CommandFilter | ||
| Close func() | ||
| GuildID api.Snowflake | ||
| MemberID api.Snowflake | ||
| } | ||
|
|
||
| // OnEvent used to get events for the CommandCollector | ||
| func (c *CommandCollector) OnEvent(e interface{}) { | ||
| if event, ok := e.(*events.CommandEvent); ok { | ||
| if !c.Filter(event.CommandInteraction) { | ||
| return | ||
| } | ||
|
|
||
| c.Channel <- event.CommandInteraction | ||
| } else if event, ok := e.(events.GuildLeaveEvent); ok && event.GuildID == c.GuildID { | ||
| c.Close() | ||
| } | ||
| } |
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 utils | ||
|
|
||
| import ( | ||
| "github.com/DisgoOrg/disgo/api" | ||
| "github.com/DisgoOrg/disgo/api/events" | ||
| ) | ||
|
|
||
| // NewReactionAddCollector gives you a channel to receive on and a function to close the collector | ||
| func NewReactionAddCollector(disgo api.Disgo, channelID api.Snowflake, guildID api.Snowflake, messageID api.Snowflake, filter ReactionFilter) (chan *api.MessageReaction, func()) { | ||
| ch := make(chan *api.MessageReaction) | ||
|
|
||
| col := &ReactionAddCollector{ | ||
| Filter: filter, | ||
| Channel: ch, | ||
| ChannelID: channelID, | ||
| GuildID: guildID, | ||
| MessageID: messageID, | ||
| } | ||
|
|
||
| cls := func() { | ||
| close(ch) | ||
| disgo.EventManager().RemoveEventListener(col) | ||
| } | ||
|
|
||
| col.Close = cls | ||
|
|
||
| disgo.EventManager().AddEventListeners(col) | ||
|
|
||
| return ch, cls | ||
| } | ||
|
|
||
| // NewReactionAddCollectorFromMessage is an overload of NewReactionCollector that takes an api.Message for information | ||
| //goland:noinspection GoUnusedExportedFunction | ||
| func NewReactionAddCollectorFromMessage(message *api.Message, filter ReactionFilter) (chan *api.MessageReaction, func()) { | ||
| return NewReactionAddCollector(message.Disgo, message.ChannelID, message.ID, *message.GuildID, filter) | ||
| } | ||
|
|
||
| // ReactionFilter used to filter api.MessageReaction in a ReactionCollector | ||
| type ReactionFilter func(reaction *events.MessageReactionAddEvent) bool | ||
|
|
||
| // ReactionAddCollector used to collect api.MessageReaction(s) from an api.Message using a ReactionFilter function | ||
| type ReactionAddCollector struct { | ||
| Channel chan *api.MessageReaction | ||
| Filter ReactionFilter | ||
| Close func() | ||
| ChannelID api.Snowflake | ||
| GuildID api.Snowflake | ||
| MessageID api.Snowflake | ||
| } | ||
|
|
||
| // OnEvent used to get events for the ReactionCollector | ||
| func (r *ReactionAddCollector) OnEvent(e interface{}) { | ||
| if event, ok := e.(*events.MessageReactionAddEvent); ok { | ||
| if !r.Filter(event) { | ||
| return | ||
| } | ||
|
|
||
| r.Channel <- &event.MessageReaction | ||
| } else if event, ok := e.(*events.GuildChannelDeleteEvent); ok && event.ChannelID == r.ChannelID { | ||
| r.Close() | ||
| } else if event, ok := e.(events.GuildLeaveEvent); ok && event.GuildID == r.GuildID { | ||
| r.Close() | ||
| } else if event, ok := e.(events.MessageDeleteEvent); ok && event.MessageID == r.MessageID { | ||
| r.Close() | ||
| } | ||
| } |
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 utils | ||
|
|
||
| import ( | ||
| "github.com/DisgoOrg/disgo/api" | ||
| "github.com/DisgoOrg/disgo/api/events" | ||
| ) | ||
|
|
||
| // NewSelectMenuSubmitCollector gives you a channel to receive on and a function to close the collector | ||
| func NewSelectMenuSubmitCollector(disgo api.Disgo, channelID api.Snowflake, guildID api.Snowflake, messageID api.Snowflake, filter SelectFilter) (chan *api.SelectMenuInteraction, func()) { | ||
| ch := make(chan *api.SelectMenuInteraction) | ||
|
|
||
| col := &SelectMenuSubmitCollector{ | ||
| Filter: filter, | ||
| Channel: ch, | ||
| ChannelID: channelID, | ||
| GuildID: guildID, | ||
| MessageID: messageID, | ||
| } | ||
|
|
||
| cls := func() { | ||
| close(ch) | ||
| disgo.EventManager().RemoveEventListener(col) | ||
| } | ||
|
|
||
| col.Close = cls | ||
|
|
||
| disgo.EventManager().AddEventListeners(col) | ||
|
|
||
| return ch, cls | ||
| } | ||
|
|
||
| // NewSelectMenuSubmitCollectorFromMessage is an overload of NewSelectCollector that takes an api.Message for information | ||
| //goland:noinspection GoUnusedExportedFunction | ||
| func NewSelectMenuSubmitCollectorFromMessage(message *api.Message, filter SelectFilter) (chan *api.SelectMenuInteraction, func()) { | ||
| return NewSelectMenuSubmitCollector(message.Disgo, message.ChannelID, message.ID, *message.GuildID, filter) | ||
| } | ||
|
|
||
| // SelectFilter used to filter api.MessageReaction in a SelectCollector | ||
| type SelectFilter func(reaction *api.SelectMenuInteraction) bool | ||
|
|
||
| // SelectMenuSubmitCollector used to collect api.SelectMenuInteraction(s) from an api.Message using a SelectFilter function | ||
| type SelectMenuSubmitCollector struct { | ||
| Channel chan *api.SelectMenuInteraction | ||
| Filter SelectFilter | ||
| Close func() | ||
| ChannelID api.Snowflake | ||
| GuildID api.Snowflake | ||
| MessageID api.Snowflake | ||
| } | ||
|
|
||
| // OnEvent used to get events for the SelectCollector | ||
| func (s *SelectMenuSubmitCollector) OnEvent(e interface{}) { | ||
| if event, ok := e.(*events.SelectMenuSubmitEvent); ok { | ||
| if !s.Filter(event.SelectMenuInteraction) { | ||
| return | ||
| } | ||
|
|
||
| s.Channel <- event.SelectMenuInteraction | ||
| } else if event, ok := e.(*events.GuildChannelDeleteEvent); ok && event.ChannelID == s.ChannelID { | ||
| s.Close() | ||
| } else if event, ok := e.(events.GuildLeaveEvent); ok && event.GuildID == s.GuildID { | ||
| s.Close() | ||
| } else if event, ok := e.(events.MessageDeleteEvent); ok && event.MessageID == s.MessageID { | ||
| s.Close() | ||
| } | ||
| } |
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
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.