Skip to content
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions api/utils/button_collector.go
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()) {
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 {
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()
}
}
60 changes: 60 additions & 0 deletions api/utils/command_collector.go
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()
}
}
66 changes: 66 additions & 0 deletions api/utils/reaction_collector.go
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()
}
}
66 changes: 66 additions & 0 deletions api/utils/select_collector.go
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()
}
}
2 changes: 1 addition & 1 deletion example/examplebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func messageListener(event *events.GuildMessageCreateEvent) {

case "repeat":
go func() {
ch, cls := util.NewMessageCollector(event.Disgo(), func(m *api.Message) bool {
ch, cls := util.NewMessageCollectorFromChannel(event.MessageChannel(), func(m *api.Message) bool {
return !m.Author.IsBot && m.ChannelID == event.ChannelID
})

Expand Down
4 changes: 1 addition & 3 deletions internal/event_manager_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ func (e *EventManagerImpl) Dispatch(event api.Event) {

// AddEventListeners adds one or more api.EventListener(s) to the api.EventManager
func (e *EventManagerImpl) AddEventListeners(listeners ...api.EventListener) {
for _, listener := range listeners {
e.listeners = append(e.listeners, listener)
}
e.listeners = append(e.listeners, listeners...)
}

// RemoveEventListener removes one or more api.EventListener(s) from the api.EventManager
Expand Down