Organize/refactor util package

This commit is contained in:
ayntgl 2022-01-22 21:22:35 +04:00
parent 3730f41e3e
commit c3e572b8ce
3 changed files with 18 additions and 32 deletions

View file

@ -2,28 +2,10 @@ package util
import (
"encoding/json"
"regexp"
"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 FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Message) {
for i, m := range ms {
if m.ID == mID {
@ -34,20 +16,6 @@ func FindMessageByID(ms []*discordgo.Message, mID string) (int, *discordgo.Messa
return -1, nil
}
func ChannelIsUnread(s *discordgo.State, c *discordgo.Channel) bool {
if c.LastMessageID == "" {
return false
}
for _, rs := range s.ReadState {
if c.ID == rs.ID {
return c.LastMessageID != rs.LastMessageID
}
}
return false
}
func HasPermission(s *discordgo.State, cID string, p int64) bool {
perm, err := s.UserChannelPermissions(s.User.ID, cID)
if err != nil {

View file

@ -1,11 +1,29 @@
package util
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 != "" {