util.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. }
  37. func hasKeybinding(ks []string, k string) bool {
  38. for _, repr := range ks {
  39. if repr == k {
  40. return true
  41. }
  42. }
  43. return false
  44. }