discordo/ui/util.go
2022-02-17 12:00:46 +04:00

54 lines
1.1 KiB
Go

package ui
import (
"regexp"
"strings"
"github.com/ayntgl/discordgo"
)
var (
boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`)
italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`)
underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`)
strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
)
func parseMarkdown(md string) string {
var res string
res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
return res
}
func channelToString(c *discordgo.Channel) string {
var repr string
if c.Name != "" {
repr = "#" + c.Name
} else if len(c.Recipients) == 1 {
rp := c.Recipients[0]
repr = rp.Username + "#" + rp.Discriminator
} else {
rps := make([]string, len(c.Recipients))
for i, r := range c.Recipients {
rps[i] = r.Username + "#" + r.Discriminator
}
repr = strings.Join(rps, ", ")
}
return repr
}
func hasKeybinding(ks []string, k string) bool {
for _, repr := range ks {
if repr == k {
return true
}
}
return false
}