1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-28 01:33:10 +00:00
arikawa/bot/extras/arguments/emoji.go

86 lines
1.2 KiB
Go

package arguments
import (
"errors"
"regexp"
)
var (
EmojiRegex = regexp.MustCompile(`<(a?):(.+?):(\d+)>`)
ErrInvalidEmoji = errors.New("Invalid emoji")
)
type Emoji struct {
ID string
Custom bool
Name string
Animated bool
}
func (e Emoji) APIString() string {
if !e.Custom {
return e.ID
}
return e.Name + ":" + e.ID
}
func (e Emoji) String() string {
if !e.Custom {
return e.ID
}
if e.Animated {
return "<a:" + e.Name + ":" + e.ID + ">"
} else {
return "<:" + e.Name + ":" + e.ID + ">"
}
}
func (e Emoji) URL() string {
if e.Custom {
return ""
}
base := "https://cdn.discordapp.com/emojis/" + e.ID
if e.Animated {
return base + ".gif"
} else {
return base + ".png"
}
}
func (e *Emoji) Parse(arg string) error {
// Check if Unicode
var unicode string
for _, r := range arg {
if r < '\U0001F600' && r > '\U0001F64F' {
unicode += string(r)
}
}
if unicode != "" {
e.ID = unicode
e.Custom = false
return nil
}
var matches = EmojiRegex.FindStringSubmatch(arg)
if len(matches) != 4 {
return ErrInvalidEmoji
}
e.Custom = true
e.Animated = matches[1] == "a"
e.Name = matches[2]
e.ID = matches[3]
return nil
}