From 381269311172c9586589b6438f960b173578ca25 Mon Sep 17 00:00:00 2001 From: Wim Date: Tue, 23 May 2017 22:26:37 +0200 Subject: [PATCH] Replace long ids in channel metions (discord). Fixes #174 --- bridge/discord/discord.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/bridge/discord/discord.go b/bridge/discord/discord.go index f6f3077f..f379ede6 100644 --- a/bridge/discord/discord.go +++ b/bridge/discord/discord.go @@ -140,6 +140,7 @@ func (b *bdiscord) messageCreate(s *discordgo.Session, m *discordgo.MessageCreat m.Message.Content = b.replaceRoleMentions(m.Message.Content) } m.Message.Content = b.stripCustomoji(m.Message.Content) + m.Message.Content = b.replaceChannelMentions(m.Message.Content) b.Remote <- config.Message{Username: username, Text: m.ContentWithMentionsReplaced(), Channel: channelName, Account: b.Account, Avatar: "https://cdn.discordapp.com/avatars/" + m.Author.ID + "/" + m.Author.Avatar + ".jpg"} } @@ -213,6 +214,25 @@ func (b *bdiscord) replaceRoleMentions(text string) string { return text } +func (b *bdiscord) replaceChannelMentions(text string) string { + var err error + re := regexp.MustCompile("<#[0-9]+>") + text = re.ReplaceAllStringFunc(text, func(m string) string { + channel := b.getChannelName(m[2 : len(m)-1]) + // if at first don't succeed, try again + if channel == "" { + b.Channels, err = b.c.GuildChannels(b.guildID) + if err != nil { + return "#unknownchannel" + } + channel = b.getChannelName(m[2 : len(m)-1]) + return channel + } + return channel + }) + return text +} + func (b *bdiscord) stripCustomoji(text string) string { // <:doge:302803592035958784> re := regexp.MustCompile("<(:.*?:)[0-9]+>")