2020-06-06 20:47:15 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2021-06-02 02:53:19 +00:00
|
|
|
"github.com/diamondburned/arikawa/v3/gateway"
|
2020-06-06 20:47:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *State) handleReady(ev *gateway.ReadyEvent) {
|
2021-05-29 11:21:47 +00:00
|
|
|
s.guildMutex.Lock()
|
|
|
|
defer s.guildMutex.Unlock()
|
|
|
|
|
2020-06-06 20:47:15 +00:00
|
|
|
for _, g := range ev.Guilds {
|
2021-05-29 11:21:47 +00:00
|
|
|
s.unreadyGuilds[g.ID] = struct{}{}
|
2020-06-06 20:47:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) handleGuildCreate(ev *gateway.GuildCreateEvent) {
|
2021-05-29 11:21:47 +00:00
|
|
|
s.guildMutex.Lock()
|
|
|
|
|
|
|
|
var derivedEvent interface{}
|
|
|
|
|
|
|
|
// The guild was previously announced to us in the ready event, and has now
|
|
|
|
// become available.
|
|
|
|
if _, ok := s.unreadyGuilds[ev.ID]; ok {
|
|
|
|
delete(s.unreadyGuilds, ev.ID)
|
|
|
|
derivedEvent = &GuildReadyEvent{GuildCreateEvent: ev}
|
|
|
|
|
|
|
|
// The guild was previously announced as unavailable through a guild
|
|
|
|
// delete event, and has now become available again.
|
|
|
|
} else if _, ok = s.unavailableGuilds[ev.ID]; ok {
|
|
|
|
delete(s.unavailableGuilds, ev.ID)
|
|
|
|
derivedEvent = &GuildAvailableEvent{GuildCreateEvent: ev}
|
|
|
|
|
|
|
|
// We don't know this guild, hence it's new.
|
|
|
|
} else {
|
|
|
|
derivedEvent = &GuildJoinEvent{GuildCreateEvent: ev}
|
2020-06-06 20:47:15 +00:00
|
|
|
}
|
2021-05-29 11:21:47 +00:00
|
|
|
|
|
|
|
// Unlock here already, so we don't block the mutex if there are
|
|
|
|
// long-blocking synchronous handlers.
|
|
|
|
s.guildMutex.Unlock()
|
|
|
|
s.Handler.Call(derivedEvent)
|
2020-06-06 20:47:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *State) handleGuildDelete(ev *gateway.GuildDeleteEvent) {
|
2021-05-29 11:21:47 +00:00
|
|
|
s.guildMutex.Lock()
|
|
|
|
|
2020-06-06 20:47:15 +00:00
|
|
|
// store this so we can later dispatch a GuildAvailableEvent, once the
|
|
|
|
// guild becomes available again.
|
|
|
|
if ev.Unavailable {
|
2021-05-29 11:21:47 +00:00
|
|
|
s.unavailableGuilds[ev.ID] = struct{}{}
|
|
|
|
s.guildMutex.Unlock()
|
2020-06-06 20:47:15 +00:00
|
|
|
|
2021-05-29 11:21:47 +00:00
|
|
|
s.Handler.Call(&GuildUnavailableEvent{GuildDeleteEvent: ev})
|
2020-06-06 20:47:15 +00:00
|
|
|
} else {
|
2021-05-29 11:21:47 +00:00
|
|
|
// Possible scenario requiring this would be leaving the guild while
|
|
|
|
// unavailable.
|
|
|
|
delete(s.unavailableGuilds, ev.ID)
|
|
|
|
s.guildMutex.Unlock()
|
2020-06-06 20:47:15 +00:00
|
|
|
|
2021-05-29 11:21:47 +00:00
|
|
|
s.Handler.Call(&GuildLeaveEvent{GuildDeleteEvent: ev})
|
2020-06-06 20:47:15 +00:00
|
|
|
}
|
|
|
|
}
|