From edc56dedc7af057ef222d116d2e002dd03f14093 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 20 Aug 2021 13:47:33 -0400 Subject: [PATCH] discord: resolve guild nicks --- connectors/discord/discord.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/connectors/discord/discord.go b/connectors/discord/discord.go index d885de7..896e6b7 100644 --- a/connectors/discord/discord.go +++ b/connectors/discord/discord.go @@ -128,10 +128,12 @@ func (d *Discord) GetEmojiList() map[string]string { if d.emojiCache != nil { return d.emojiCache } - guidID := d.config.Get("discord.guildid", "") - if guidID == "" { + guildID := d.config.Get("discord.guildid", "") + if guildID == "" { + log.Error().Msg("no guild ID set") + return map[string]string{} } - e, err := d.client.GuildEmojis(guidID) + e, err := d.client.GuildEmojis(guildID) if err != nil { log.Error().Err(err).Msg("could not retrieve emojis") return map[string]string{} @@ -170,9 +172,23 @@ func (d *Discord) convertUser(u *discordgo.User) *user.User { if err != nil { log.Error().Err(err).Msg("error getting avatar") } + nick := u.Username + + guildID := d.config.Get("discord.guildid", "") + if guildID == "" { + log.Error().Msg("no guild ID set") + } else { + mem, err := d.client.GuildMember(guildID, u.ID) + if err != nil { + log.Error().Err(err).Msg("could not get guild member") + } else if mem.Nick != "" { + nick = mem.Nick + } + } + return &user.User{ ID: u.ID, - Name: u.Username, + Name: nick, Admin: false, Icon: d.client.State.User.AvatarURL("64"), IconImg: img, @@ -212,9 +228,11 @@ func (d *Discord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreate tStamp, _ := m.Timestamp.Parse() + author := d.convertUser(m.Author) + msg := msg.Message{ ID: m.ID, - User: d.convertUser(m.Author), + User: author, Channel: m.ChannelID, ChannelName: ch.Name, Body: text,