1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-16 03:44:26 +00:00
arikawa/state/event_dispatcher.go
diamondburned (Forefront) 88dd0f8995 State now handles MsgCreate's missing Member.User field, some bug fixes
This addresses discord/discord-api-docs#1440.

State documentation has been added, which documents the store and
handlers as well.

Bug fixes include:

- PreHandler being called after the state handler; it is now called
before as documented.
- Minor behavior changes regarding Guild Create events. Refer to State's
documentation.
2020-06-19 00:33:22 -07:00

59 lines
1.4 KiB
Go

package state
import (
"github.com/diamondburned/arikawa/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) {
// this guild was unavailable, but has come back online
if 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
} else if s.unreadyGuilds.Delete(ev.ID) {
s.Handler.Call(&GuildReadyEvent{
GuildCreateEvent: ev,
})
} else { // we don't know this guild, hence we just joined it
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,
})
}
}