From 1b785b1c38e04f52fd160e1feeca65eee36cdebf Mon Sep 17 00:00:00 2001 From: "diamondburned (Forefront)" Date: Wed, 11 Mar 2020 19:41:14 -0700 Subject: [PATCH] State: Added guild emoji handlers --- state/state_events.go | 52 ++----------------------------------------- state/store.go | 37 ++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 50 deletions(-) diff --git a/state/state_events.go b/state/state_events.go index d8c991e..8b43e82 100644 --- a/state/state_events.go +++ b/state/state_events.go @@ -37,29 +37,7 @@ func (s *State) onEvent(iface interface{}) { // Handle guilds for i := range ev.Guilds { - guild := ev.Guilds[i] - - if err := s.Store.GuildSet(&guild.Guild); err != nil { - s.stateErr(err, "Failed to set guild in Ready") - } - - for i := range guild.Members { - if err := s.Store.MemberSet(guild.ID, &guild.Members[i]); err != nil { - s.stateErr(err, "Failed to set guild member in Ready") - } - } - - for i := range guild.Channels { - if err := s.Store.ChannelSet(&guild.Channels[i]); err != nil { - s.stateErr(err, "Failed to set guild channel in Ready") - } - } - - for i := range guild.Presences { - if err := s.Store.PresenceSet(guild.ID, &guild.Presences[i]); err != nil { - s.stateErr(err, "Failed to set guild presence in Ready") - } - } + handleGuildCreate(s, &ev.Guilds[i]) } // Handle private channels @@ -75,34 +53,8 @@ func (s *State) onEvent(iface interface{}) { } case *gateway.GuildCreateEvent: - if err := s.Store.GuildSet(&ev.Guild); err != nil { - s.stateErr(err, "Failed to create guild in state") - } + handleGuildCreate(s, ev) - for _, m := range ev.Members { - m := m - - if err := s.Store.MemberSet(ev.Guild.ID, &m); err != nil { - s.stateErr(err, "Failed to add a member from guild in state") - } - } - - for _, ch := range ev.Channels { - ch := ch - ch.GuildID = ev.Guild.ID // just to make sure - - if err := s.Store.ChannelSet(&ch); err != nil { - s.stateErr(err, "Failed to add a channel from guild in state") - } - } - - for _, p := range ev.Presences { - p := p - - if err := s.Store.PresenceSet(ev.Guild.ID, &p); err != nil { - s.stateErr(err, "Failed to add a presence from guild in state") - } - } case *gateway.GuildUpdateEvent: if err := s.Store.GuildSet((*discord.Guild)(ev)); err != nil { s.stateErr(err, "Failed to update guild in state") diff --git a/state/store.go b/state/store.go index 4979e8b..5bbcf4b 100644 --- a/state/store.go +++ b/state/store.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/diamondburned/arikawa/discord" + "github.com/diamondburned/arikawa/gateway" ) // Store is the state storage. It should handle mutex itself, and it should only @@ -84,3 +85,39 @@ type StoreModifier interface { // isn't in the storage. There is no strict restrictions on what uses this (the // default one does, though), so be advised. var ErrStoreNotFound = errors.New("item not found in store") + +// Helper functions + +func handleGuildCreate(s *State, guild *gateway.GuildCreateEvent) { + if err := s.Store.GuildSet(&guild.Guild); err != nil { + s.stateErr(err, "Failed to set guild in Ready") + } + + // Handle guild emojis + if guild.Emojis != nil { + if err := s.Store.EmojiSet(guild.ID, guild.Emojis); err != nil { + s.stateErr(err, "Failed to set guild emojis") + } + } + + // Handle guild member + for i := range guild.Members { + if err := s.Store.MemberSet(guild.ID, &guild.Members[i]); err != nil { + s.stateErr(err, "Failed to set guild member in Ready") + } + } + + // Handle guild channels + for i := range guild.Channels { + if err := s.Store.ChannelSet(&guild.Channels[i]); err != nil { + s.stateErr(err, "Failed to set guild channel in Ready") + } + } + + // Handle guild presences + for i := range guild.Presences { + if err := s.Store.PresenceSet(guild.ID, &guild.Presences[i]); err != nil { + s.stateErr(err, "Failed to set guild presence in Ready") + } + } +}