Skip to content

Commit bd4fe33

Browse files
committed
convert mentions to @Username
1 parent 196b4a2 commit bd4fe33

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

transcript.go

+26
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package main
22

33
import (
44
"os"
5+
"regexp"
56
"sort"
7+
"strconv"
68
"strings"
79

810
"github.com/disgoorg/disgo/discord"
@@ -69,6 +71,7 @@ func (t *Transcript) AddMessage(m discord.Message) {
6971
name = t.nameOverride[m.Author.ID.String()].(string)
7072
} else {
7173
name = m.Author.Username
74+
t.nameOverride[m.Author.ID.String()] = name
7275
}
7376

7477
// create a new block
@@ -233,6 +236,7 @@ func (t *Transcript) SaveTranscript() {
233236

234237
// preprocess message to handle newlines
235238
content := strings.ReplaceAll(m.Content, "\n", "\n> ")
239+
content = t.replaceMentions(content)
236240

237241
writeToFile(f, "> "+content)
238242
writeToFile(f, "")
@@ -259,3 +263,25 @@ func writeToFile(file *os.File, content string) {
259263
log.Panic(err)
260264
}
261265
}
266+
267+
func (t *Transcript) replaceMentions(content string) string {
268+
outerPart := regexp.MustCompile("<@!*&*|>")
269+
270+
return regexp.MustCompile("<@!*&*[0-9]+>").ReplaceAllStringFunc(content, func(s string) string {
271+
idStr := outerPart.ReplaceAllString(s, "")
272+
273+
idNum, err := strconv.ParseUint(idStr, 10, 64)
274+
if err != nil {
275+
eris.Wrap(err, "failed to parse id")
276+
log.Panic(err)
277+
}
278+
279+
id := snowflake.ID(idNum)
280+
281+
if val, ok := t.nameOverride[id.String()].(string); ok {
282+
return "@" + val
283+
} else {
284+
return s
285+
}
286+
})
287+
}

0 commit comments

Comments
 (0)