From 296428d53e4febb5a82082d3c61628fbd396fd13 Mon Sep 17 00:00:00 2001 From: NikkyAI Date: Mon, 17 Sep 2018 21:25:06 +0200 Subject: [PATCH] Fix Discord mentions by populating the nickMemberMap at connect (#498) --- bridge/discord/discord.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index 25a6a7a6..0265f843 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -89,6 +89,21 @@ func (b *Bdiscord) Connect() error { for _, channel := range b.Channels { b.Log.Debugf("found channel %#v", channel) } + // obtaining guild members and initializing nickname mapping + b.Lock() + members, err := b.c.GuildMembers(b.guildID, "", 1000) + if err != nil { + b.Log.Error("Error obtaining guild members", err) + return err + } + for _, member := range members { + b.userMemberMap[member.User.ID] = member + b.nickMemberMap[member.User.Username] = member + if member.Nick != "" { + b.nickMemberMap[member.Nick] = member + } + } + b.Unlock() return nil } @@ -298,7 +313,10 @@ func (b *Bdiscord) memberUpdate(s *discordgo.Session, m *discordgo.GuildMemberUp b.Log.Debugf("%s: memberupdate: user %s (nick %s) changes nick to %s", b.Account, m.Member.User.Username, b.userMemberMap[m.Member.User.ID].Nick, m.Member.Nick) } b.userMemberMap[m.Member.User.ID] = m.Member - b.nickMemberMap[m.Member.Nick] = m.Member + b.nickMemberMap[m.Member.User.Username] = m.Member + if m.Member.Nick != "" { + b.nickMemberMap[m.Member.Nick] = m.Member + } b.Unlock() }