From d9e0580e45d05adbbda4588fee69d75185bdd707 Mon Sep 17 00:00:00 2001 From: mavolin <48887425+mavolin@users.noreply.github.com> Date: Sat, 16 May 2020 22:13:40 +0200 Subject: [PATCH] State: implement #76 --- state/state.go | 66 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 18 deletions(-) diff --git a/state/state.go b/state/state.go index 47341a2..77670b7 100644 --- a/state/state.go +++ b/state/state.go @@ -121,34 +121,49 @@ func (s *State) MemberDisplayName(guildID, userID discord.Snowflake) (string, er return member.Nick, nil } -func (s *State) AuthorColor(message *gateway.MessageCreateEvent) discord.Color { +func (s *State) AuthorColor(message *gateway.MessageCreateEvent) (discord.Color, error) { if !message.GuildID.Valid() { - return discord.DefaultMemberColor + return 0, nil } if message.Member != nil { guild, err := s.Guild(message.GuildID) if err != nil { - return discord.DefaultMemberColor + return 0, nil } - return discord.MemberColor(*guild, *message.Member) + return discord.MemberColor(*guild, *message.Member), nil } return s.MemberColor(message.GuildID, message.Author.ID) } -func (s *State) MemberColor(guildID, userID discord.Snowflake) discord.Color { - member, err := s.Member(guildID, userID) - if err != nil { - return discord.DefaultMemberColor +func (s *State) MemberColor(guildID, userID discord.Snowflake) (discord.Color, error) { + var wg sync.WaitGroup + + g, gerr := s.Store.Guild(guildID) + if gerr != nil { + wg.Add(1) + go func() { + g, gerr = s.Session.Guild(guildID) + wg.Done() + }() } - guild, err := s.Guild(guildID) - if err != nil { - return discord.DefaultMemberColor + m, merr := s.Store.Member(guildID, userID) + if merr != nil { + m, merr = s.Member(guildID, userID) + if merr != nil { + return 0, errors.Wrap(merr, "failed to get member") + } } - return discord.MemberColor(*guild, *member) + wg.Wait() + + if gerr != nil { + return 0, errors.Wrap(merr, "failed to get guild") + } + + return discord.MemberColor(*g, *m), nil } //// @@ -159,14 +174,29 @@ func (s *State) Permissions(channelID, userID discord.Snowflake) (discord.Permis return 0, errors.Wrap(err, "Failed to get channel") } - g, err := s.Guild(ch.GuildID) - if err != nil { - return 0, errors.Wrap(err, "Failed to get guild") + var wg sync.WaitGroup + + g, gerr := s.Store.Guild(ch.GuildID) + if gerr != nil { + wg.Add(1) + go func() { + g, gerr = s.Session.Guild(ch.GuildID) + wg.Done() + }() } - m, err := s.Member(ch.GuildID, userID) - if err != nil { - return 0, errors.Wrap(err, "Failed to get member") + m, merr := s.Store.Member(ch.GuildID, userID) + if merr != nil { + m, merr = s.Member(ch.GuildID, userID) + if merr != nil { + return 0, errors.Wrap(merr, "failed to get member") + } + } + + wg.Wait() + + if gerr != nil { + return 0, errors.Wrap(merr, "failed to get guild") } return discord.CalcOverwrites(*g, *ch, *m), nil