2020-01-19 06:06:00 +00:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
2020-02-07 16:31:10 +00:00
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/api/rate"
|
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
EmojiRegex = regexp.MustCompile(`<(a?):(.+?):(\d+)>`)
|
|
|
|
|
2020-05-16 21:14:49 +00:00
|
|
|
ErrInvalidEmoji = errors.New("invalid emoji")
|
2020-01-19 06:06:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Emoji struct {
|
2020-07-21 20:27:59 +00:00
|
|
|
ID discord.EmojiID
|
2020-04-09 04:33:46 +00:00
|
|
|
Name string
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
Custom bool
|
|
|
|
Animated bool
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:01:59 +00:00
|
|
|
func (e Emoji) APIString() string {
|
2020-01-24 03:54:47 +00:00
|
|
|
if !e.Custom {
|
2020-04-09 04:33:46 +00:00
|
|
|
return e.Name
|
2020-01-24 03:54:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 04:33:46 +00:00
|
|
|
return e.Name + ":" + e.ID.String()
|
2020-01-24 03:54:47 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 04:01:59 +00:00
|
|
|
func (e Emoji) String() string {
|
|
|
|
if !e.Custom {
|
2020-04-09 04:33:46 +00:00
|
|
|
return e.Name
|
2020-01-24 04:01:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if e.Animated {
|
2020-04-09 04:33:46 +00:00
|
|
|
return "<a:" + e.Name + ":" + e.ID.String() + ">"
|
2020-01-24 04:01:59 +00:00
|
|
|
} else {
|
2020-04-09 04:33:46 +00:00
|
|
|
return "<:" + e.Name + ":" + e.ID.String() + ">"
|
2020-01-24 04:01:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 ""
|
|
|
|
}
|
|
|
|
|
2020-04-09 04:33:46 +00:00
|
|
|
base := "https://cdn.discordapp.com/emojis/" + e.ID.String()
|
2020-01-24 03:54:47 +00:00
|
|
|
|
|
|
|
if e.Animated {
|
|
|
|
return base + ".gif"
|
|
|
|
} else {
|
|
|
|
return base + ".png"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 04:25:50 +00:00
|
|
|
func (e *Emoji) Usage() string {
|
|
|
|
return "emoji"
|
|
|
|
}
|
|
|
|
|
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
|
2020-02-07 16:31:10 +00:00
|
|
|
if rate.StringIsEmojiOnly(arg) {
|
2020-04-09 04:33:46 +00:00
|
|
|
e.Name = 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
|
|
|
|
}
|
|
|
|
|
2020-04-09 04:33:46 +00:00
|
|
|
id, err := discord.ParseSnowflake(matches[3])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-19 06:06:00 +00:00
|
|
|
e.Custom = true
|
|
|
|
e.Animated = matches[1] == "a"
|
|
|
|
e.Name = matches[2]
|
2020-07-21 20:27:59 +00:00
|
|
|
e.ID = discord.EmojiID(id)
|
2020-01-19 06:06:00 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|