From ce0d4e5ee3b4a4b673ec2bbaba9f6e79d81a8ffd Mon Sep 17 00:00:00 2001 From: Chris Rhodes Date: Thu, 31 Dec 2015 18:20:48 -0800 Subject: [PATCH] Add a method on Message to replace mentions with usernames. --- structs.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/structs.go b/structs.go index 3715c90..40c42f9 100644 --- a/structs.go +++ b/structs.go @@ -13,7 +13,9 @@ package discordgo import ( "encoding/json" + "fmt" "net" + "strings" "time" "github.com/gorilla/websocket" @@ -97,6 +99,16 @@ type Message struct { ChannelID string `json:"channel_id"` } +// ContentWithMentionsReplaced will replace all @ mentions with the +// username of the mention. +func (m *Message) ContentWithMentionsReplaced() string { + content := m.Content + for _, user := range m.Mentions { + content = strings.Replace(content, fmt.Sprintf("<@%s>", user.ID), fmt.Sprintf("@%s", user.Username), -1) + } + return content +} + // An Attachment stores data for message attachments. type Attachment struct { //TODO figure this out }