1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-05 06:26:08 +00:00
arikawa/state/event_dispatcher.go
diamondburned c6679dc52c State: Separate Store into smaller interfaces, Cabinet API
This commit refactors the Store interface in State into smaller
interfaces in package store. These interfaces are combined into one
structure called a "Cabinet". The default implementation of those
interfaces have been rewritten in package defaultstore, while the old
no-op implementation stays with the store package.

This commit also omitted several state handlers for user events, as it
is unclear what they are actually structured like.
2020-11-29 16:57:58 -08:00

62 lines
1.4 KiB
Go

package state
import (
"github.com/diamondburned/arikawa/v2/gateway"
)
func (s *State) handleReady(ev *gateway.ReadyEvent) {
for _, g := range ev.Guilds {
// store this so we know when we need to dispatch a belated
// GuildReadyEvent
if g.Unavailable {
s.unreadyGuilds.Add(g.ID)
} else {
s.Handler.Call(&GuildReadyEvent{
GuildCreateEvent: &g,
})
}
}
}
func (s *State) handleGuildCreate(ev *gateway.GuildCreateEvent) {
switch {
// this guild was unavailable, but has come back online
case s.unavailableGuilds.Delete(ev.ID):
s.Handler.Call(&GuildAvailableEvent{
GuildCreateEvent: ev,
})
// the guild was already unavailable when connecting to the gateway
// we can dispatch a belated GuildReadyEvent
case s.unreadyGuilds.Delete(ev.ID):
s.Handler.Call(&GuildReadyEvent{
GuildCreateEvent: ev,
})
// we don't know this guild, hence we just joined it
default:
s.Handler.Call(&GuildJoinEvent{
GuildCreateEvent: ev,
})
}
}
func (s *State) handleGuildDelete(ev *gateway.GuildDeleteEvent) {
// store this so we can later dispatch a GuildAvailableEvent, once the
// guild becomes available again.
if ev.Unavailable {
s.unavailableGuilds.Add(ev.ID)
s.Handler.Call(&GuildUnavailableEvent{
GuildDeleteEvent: ev,
})
} else {
// it might have been unavailable before we left
s.unavailableGuilds.Delete(ev.ID)
s.Handler.Call(&GuildLeaveEvent{
GuildDeleteEvent: ev,
})
}
}