util.go 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package ui
  2. import (
  3. "regexp"
  4. "strings"
  5. "github.com/ayntgl/discordgo"
  6. )
  7. var (
  8. boldRegex = regexp.MustCompile(`(?m)\*\*(.*?)\*\*`)
  9. italicRegex = regexp.MustCompile(`(?m)\*(.*?)\*`)
  10. underlineRegex = regexp.MustCompile(`(?m)__(.*?)__`)
  11. strikeThroughRegex = regexp.MustCompile(`(?m)~~(.*?)~~`)
  12. )
  13. func parseMarkdown(md string) string {
  14. var res string
  15. res = boldRegex.ReplaceAllString(md, "[::b]$1[::-]")
  16. res = italicRegex.ReplaceAllString(res, "[::i]$1[::-]")
  17. res = underlineRegex.ReplaceAllString(res, "[::u]$1[::-]")
  18. res = strikeThroughRegex.ReplaceAllString(res, "[::s]$1[::-]")
  19. return res
  20. }
  21. func channelToString(c *discordgo.Channel) string {
  22. var repr string
  23. if c.Name != "" {
  24. repr = "#" + c.Name
  25. } else if len(c.Recipients) == 1 {
  26. rp := c.Recipients[0]
  27. repr = rp.Username + "#" + rp.Discriminator
  28. } else {
  29. rps := make([]string, len(c.Recipients))
  30. for i, r := range c.Recipients {
  31. rps[i] = r.Username + "#" + r.Discriminator
  32. }
  33. repr = strings.Join(rps, ", ")
  34. }
  35. return repr
  36. }