arikawa/bot/extras/arguments/emoji.go

80 lines
1.1 KiB
Go
Raw Normal View History

2020-01-19 06:06:00 +00:00
package arguments
import (
"errors"
"regexp"
"github.com/diamondburned/arikawa/api/rate"
2020-01-19 06:06:00 +00:00
)
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 {
2020-01-24 03:54:47 +00:00
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 + ">"
}
}
2020-01-24 03:54:47 +00:00
func (e Emoji) URL() string {
2020-02-05 04:29:45 +00:00
if !e.Custom {
2020-01-24 03:54:47 +00:00
return ""
}
base := "https://cdn.discordapp.com/emojis/" + e.ID
if e.Animated {
return base + ".gif"
} else {
return base + ".png"
}
}
2020-01-19 06:06:00 +00:00
func (e *Emoji) Parse(arg string) error {
2020-01-25 02:49:33 +00:00
// Check if Unicode emoji
if rate.StringIsEmojiOnly(arg) {
2020-01-25 02:49:33 +00:00
e.ID = arg
2020-01-19 06:06:00 +00:00
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
}