better mention resolving, sendable mentions

This commit is contained in:
Cynthia Foxwell 2023-08-17 23:31:38 -06:00
parent 7cfc6d77a1
commit 41d3a6608f
2 changed files with 50 additions and 5 deletions

View file

@ -2,6 +2,7 @@ package commands
import ( import (
"fmt" "fmt"
"regexp"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
@ -11,9 +12,13 @@ import (
"github.com/mgutz/ansi" "github.com/mgutz/ansi"
) )
var REGEX_MENTION = regexp.MustCompile("@([a-z0-9._]{1,32})")
func SendMode() { func SendMode() {
client := state.GetClient() client := state.GetClient()
currentGuild := state.GetCurrentGuild()
channelId := state.GetCurrentChannel() channelId := state.GetCurrentChannel()
if channelId == "" { if channelId == "" {
fmt.Print("<not in a channel>\n\r") fmt.Print("<not in a channel>\n\r")
@ -79,6 +84,30 @@ func SendMode() {
} }
} else { } else {
fmt.Print(prompt, input, "\n\r") fmt.Print(prompt, input, "\n\r")
input := REGEX_MENTION.ReplaceAllStringFunc(input, func(match string) string {
matches := REGEX_MENTION.FindStringSubmatch(match)
username := matches[1]
parsedGuildId, err := discord.ParseSnowflake(currentGuild)
if err != nil {
return match
}
members, err := client.MemberStore.Members(discord.GuildID(parsedGuildId))
if err != nil {
return match
}
for _, member := range members {
if member.User.Username == username {
return member.User.ID.Mention()
}
}
return match
})
_, err := client.SendMessage(channel.ID, input) _, err := client.SendMessage(channel.ID, input)
if err != nil { if err != nil {

View file

@ -102,12 +102,28 @@ func ReplaceMarkdown(content string, noColor bool) string {
if err != nil { if err != nil {
return "@Unknown User" return "@Unknown User"
} }
currentGuild := state.GetCurrentGuild()
if currentGuild == "" {
user, err := client.User(discord.UserID(parsedId)) user, err := client.User(discord.UserID(parsedId))
if err != nil { if err != nil {
return "@Unknown User" return "@Unknown User"
} }
return "@" + user.Username return "@" + user.Username
} else {
parsedGuildId, err := discord.ParseSnowflake(currentGuild)
if err != nil {
return "@Unknown User"
}
member, err := client.MemberStore.Member(discord.GuildID(parsedGuildId), discord.UserID(parsedId))
if err != nil {
return "@Unknown User"
}
return "@" + member.User.Username
}
}) })
content = replaceAllWithCallback(*REGEX_ROLE_MENTION, content, func(matches []string) string { content = replaceAllWithCallback(*REGEX_ROLE_MENTION, content, func(matches []string) string {