util: parse message mentions
This commit is contained in:
parent
c9dd1fcf02
commit
1f78c6dac8
1 changed files with 45 additions and 27 deletions
|
|
@ -9,16 +9,18 @@ import (
|
|||
)
|
||||
|
||||
func WriteMessage(v *tview.TextView, clientID discord.UserID, m discord.Message) {
|
||||
m.Content = parseMessageMentions(m.Content, m.Mentions, clientID)
|
||||
|
||||
var b strings.Builder
|
||||
// $ ╭ AUTHOR_USERNAME (BOT) MESSAGE_CONTENT*linebreak*
|
||||
writeReferencedMessage(&b, clientID, m.ReferencedMessage)
|
||||
// $ AUTHOR_USERNAME (BOT)*spacee*
|
||||
writeAuthor(&b, clientID, m.Author)
|
||||
// $ MESSAGE_CONTENT
|
||||
writeContent(&b, m.Content)
|
||||
b.WriteString(m.Content)
|
||||
// $ *space*(edited)
|
||||
if m.EditedTimestamp.IsValid() {
|
||||
b.WriteString(" [::d](edited)[-:-:-]")
|
||||
b.WriteString(" [::d](edited)[::-]")
|
||||
}
|
||||
// $ *linebreak*EMBED
|
||||
writeEmbeds(&b, m.Embeds)
|
||||
|
|
@ -28,6 +30,30 @@ func WriteMessage(v *tview.TextView, clientID discord.UserID, m discord.Message)
|
|||
fmt.Fprintln(v, b.String())
|
||||
}
|
||||
|
||||
func parseMessageMentions(content string, mentions []discord.GuildUser, clientID discord.UserID) string {
|
||||
for i := range mentions {
|
||||
mUser := mentions[i]
|
||||
|
||||
var color string
|
||||
if mUser.ID == clientID {
|
||||
color = "[#000000:#FEE75C]"
|
||||
} else {
|
||||
color = "[:#5865F2]"
|
||||
}
|
||||
|
||||
content = strings.NewReplacer(
|
||||
// <@!USER_ID>
|
||||
fmt.Sprintf("<@!%d>", mUser.ID),
|
||||
color+"@"+mUser.Username+"[-:-]",
|
||||
// <@USER_ID>
|
||||
fmt.Sprintf("<@%d>", mUser.ID),
|
||||
color+"@"+mUser.Username+"[-:-]",
|
||||
).Replace(content)
|
||||
}
|
||||
|
||||
return content
|
||||
}
|
||||
|
||||
func writeEmbeds(b *strings.Builder, embeds []discord.Embed) {
|
||||
for range embeds {
|
||||
b.WriteString("\n<EMBED>")
|
||||
|
|
@ -37,53 +63,45 @@ func writeEmbeds(b *strings.Builder, embeds []discord.Embed) {
|
|||
func writeAttachments(b *strings.Builder, attachments []discord.Attachment) {
|
||||
for i := range attachments {
|
||||
a := attachments[i]
|
||||
b.WriteString("\n")
|
||||
b.WriteString("[")
|
||||
b.WriteString(a.Filename)
|
||||
b.WriteString("]: ")
|
||||
b.WriteString("\n[" + a.Filename + "]: ")
|
||||
b.WriteString(a.URL)
|
||||
}
|
||||
}
|
||||
|
||||
func writeAuthor(b *strings.Builder, clientID discord.UserID, u discord.User) {
|
||||
if clientID == u.ID {
|
||||
b.WriteString("[#59E3E3]")
|
||||
if u.ID == clientID {
|
||||
b.WriteString("[#57F287]")
|
||||
} else {
|
||||
b.WriteString("[#E95678]")
|
||||
b.WriteString("[#ED4245]")
|
||||
}
|
||||
|
||||
b.WriteString(u.Username)
|
||||
b.WriteString("[-:-:-] ")
|
||||
b.WriteString(u.Username + "[-] ")
|
||||
|
||||
if u.Bot {
|
||||
b.WriteString("[#59E3E3]BOT[-:-:-] ")
|
||||
b.WriteString("[#EB459E]BOT[-] ")
|
||||
}
|
||||
}
|
||||
|
||||
func writeReferencedMessage(b *strings.Builder, clientID discord.UserID, rm *discord.Message) {
|
||||
if rm != nil {
|
||||
rm.Content = parseMessageMentions(rm.Content, rm.Mentions, clientID)
|
||||
|
||||
b.WriteRune(' ')
|
||||
b.WriteRune('\u256D')
|
||||
b.WriteRune(' ')
|
||||
|
||||
if clientID == rm.Author.ID {
|
||||
b.WriteString("[#59E3E3::d]")
|
||||
if rm.Author.ID == clientID {
|
||||
b.WriteString("[#57F287::d]")
|
||||
} else {
|
||||
b.WriteString("[#E95678::d]")
|
||||
b.WriteString("[#ED4245::d]")
|
||||
}
|
||||
|
||||
b.WriteString(rm.Author.Username)
|
||||
b.WriteString(rm.Author.Username + "[-] ")
|
||||
|
||||
if rm.Author.Bot {
|
||||
b.WriteString("[#EB459E]BOT[-] ")
|
||||
}
|
||||
// Reset foreground
|
||||
b.WriteString("[-::] ")
|
||||
|
||||
writeContent(b, rm.Content)
|
||||
b.WriteString("[-:-:-]\n")
|
||||
}
|
||||
}
|
||||
|
||||
func writeContent(b *strings.Builder, c string) {
|
||||
if c != "" {
|
||||
c = tview.Escape(c)
|
||||
b.WriteString(c)
|
||||
b.WriteString(rm.Content + "[::-]\n")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue