Skip to content

Commit

Permalink
make ListenerFunc[E Event] a struct so it's comparable, cleanup event…
Browse files Browse the repository at this point in the history
… collector & make ListenerAdapter.OnEvent a pointer receiver
  • Loading branch information
topi314 committed May 20, 2022
1 parent a6e8703 commit bf955b2
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 33 deletions.
9 changes: 5 additions & 4 deletions _examples/message_collector/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
log.Infof("disgo version: %s", disgo.Version)

client, err := disgo.New(token,
bot.WithGatewayConfigOpts(gateway.WithGatewayIntents(discord.GatewayIntentGuilds, discord.GatewayIntentGuildMessages, discord.GatewayIntentDirectMessages)),
bot.WithGatewayConfigOpts(gateway.WithGatewayIntents(discord.GatewayIntentGuilds, discord.GatewayIntentGuildMessages, discord.GatewayIntentDirectMessages, discord.GatewayIntentMessageContent)),
bot.WithEventListenerFunc(onMessageCreate),
)
if err != nil {
Expand All @@ -52,9 +52,10 @@ func onMessageCreate(event *events.MessageCreateEvent) {
}
if event.Message.Content == "start" {
go func() {
ch, cls := bot.NewEventCollector(event.Client(), func(event *events.MessageCreateEvent) bool {
return event.ChannelID == event.ChannelID && event.Message.Author.ID == event.Message.Author.ID && event.Message.Content != ""
ch, cls := bot.NewEventCollector(event.Client(), func(event2 *events.MessageCreateEvent) bool {
return event.ChannelID == event2.ChannelID && event.Message.Author.ID == event2.Message.Author.ID && event2.Message.Content != ""
})
defer cls()
i := 1
str := ">>> "
ctx, clsCtx := context.WithTimeout(context.Background(), 20*time.Second)
Expand All @@ -69,8 +70,8 @@ func onMessageCreate(event *events.MessageCreateEvent) {
str += strconv.Itoa(i) + ". " + messageEvent.Message.Content + "\n\n"

if i == 3 {
cls()
_, _ = event.Client().Rest().CreateMessage(messageEvent.ChannelID, discord.NewMessageCreateBuilder().SetContent(str).Build())
return
}
i++
}
Expand Down
2 changes: 1 addition & 1 deletion bot/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func WithEventListeners(eventListeners ...EventListener) ConfigOpt {
}

func WithEventListenerFunc[E Event](listenerFunc func(e E)) ConfigOpt {
return WithEventListeners(ListenerFunc[E](listenerFunc))
return WithEventListeners(NewListenerFunc(listenerFunc))
}

func WithGateway(gateway gateway.Gateway) ConfigOpt {
Expand Down
28 changes: 8 additions & 20 deletions bot/event_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,18 @@ func NewEventCollector[E Event](client Client, filterFunc func(e E) bool) (<-cha
ch := make(chan E)
var once sync.Once

coll := &collector[E]{
FilterFunc: filterFunc,
Chan: ch,
}
client.EventManager().AddEventListeners(coll)
handler := NewListenerFunc(func(e E) {
if !filterFunc(e) {
return
}
ch <- e
})
client.EventManager().AddEventListeners(handler)

return ch, func() {
once.Do(func() {
client.EventManager().RemoveEventListeners(coll)
client.EventManager().RemoveEventListeners(handler)
close(ch)
})
}
}

type collector[E Event] struct {
FilterFunc func(e E) bool
Chan chan<- E
}

func (c *collector[E]) OnEvent(e Event) {
if event, ok := e.(E); ok {
if !c.FilterFunc(event) {
return
}
c.Chan <- event
}
}
14 changes: 8 additions & 6 deletions bot/event_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ type EventListener interface {
OnEvent(event Event)
}

var _ EventListener = (ListenerFunc[Event])(nil)
var _ EventListener = (*ListenerFunc[Event])(nil)

func NewListenerFunc[E Event](f func(e E)) ListenerFunc[E] {
return f
func NewListenerFunc[E Event](f func(e E)) *ListenerFunc[E] {
return &ListenerFunc[E]{F: f}
}

type ListenerFunc[E Event] func(e E)
type ListenerFunc[E Event] struct {
F func(e E)
}

func (l ListenerFunc[E]) OnEvent(e Event) {
func (l *ListenerFunc[E]) OnEvent(e Event) {
if event, ok := e.(E); ok {
l(event)
l.F(event)
}
}

Expand Down
2 changes: 1 addition & 1 deletion bot/event_manager_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func WithListeners(listeners ...EventListener) EventManagerConfigOpt {
}

func WithListenerFunc[E Event](listenerFunc func(e E)) EventManagerConfigOpt {
return WithListeners(ListenerFunc[E](listenerFunc))
return WithListeners(NewListenerFunc(listenerFunc))
}

func WithRawEventsEnabled() EventManagerConfigOpt {
Expand Down
2 changes: 1 addition & 1 deletion events/listener_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ type ListenerAdapter struct {
}

// OnEvent is getting called everytime we receive an event
func (l ListenerAdapter) OnEvent(event bot.Event) {
func (l *ListenerAdapter) OnEvent(event bot.Event) {
switch e := event.(type) {
case *HeartbeatEvent:
if listener := l.OnHeartbeat; listener != nil {
Expand Down

0 comments on commit bf955b2

Please sign in to comment.