2020-04-09 04:25:50 +00:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"regexp"
|
|
|
|
|
2020-10-28 22:39:59 +00:00
|
|
|
"github.com/diamondburned/arikawa/v2/discord"
|
2020-04-09 04:25:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// (empty) so it matches standard links
|
|
|
|
// | OR
|
|
|
|
// canary. matches canary MessageURL
|
|
|
|
// 3 `(\d+)` for guild ID, channel ID and message ID
|
|
|
|
var Regex = regexp.MustCompile(
|
2020-05-14 07:19:51 +00:00
|
|
|
`https://(ptb\.|canary\.)?discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)`,
|
2020-04-09 04:25:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// MessageURL contains info from a MessageURL
|
|
|
|
type MessageURL struct {
|
2020-07-21 20:27:59 +00:00
|
|
|
GuildID discord.GuildID
|
|
|
|
ChannelID discord.ChannelID
|
|
|
|
MessageID discord.MessageID
|
2020-04-09 04:25:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (url *MessageURL) Parse(arg string) error {
|
|
|
|
u := ParseMessageURL(arg)
|
|
|
|
if u == nil {
|
2020-05-16 21:14:49 +00:00
|
|
|
return errors.New("invalid MessageURL format")
|
2020-04-09 04:25:50 +00:00
|
|
|
}
|
|
|
|
*url = *u
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (url *MessageURL) Usage() string {
|
|
|
|
return "https\u200b://discordapp.com/channels/\\*/\\*/\\*"
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseMessageURL parses the MessageURL into a smartlink
|
|
|
|
func ParseMessageURL(url string) *MessageURL {
|
|
|
|
ss := Regex.FindAllStringSubmatch(url, -1)
|
|
|
|
if ss == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(ss) == 0 || len(ss[0]) != 5 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
gID, err1 := discord.ParseSnowflake(ss[0][2])
|
|
|
|
cID, err2 := discord.ParseSnowflake(ss[0][3])
|
|
|
|
mID, err3 := discord.ParseSnowflake(ss[0][4])
|
|
|
|
|
|
|
|
if err1 != nil || err2 != nil || err3 != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &MessageURL{
|
2020-07-21 20:27:59 +00:00
|
|
|
GuildID: discord.GuildID(gID),
|
|
|
|
ChannelID: discord.ChannelID(cID),
|
|
|
|
MessageID: discord.MessageID(mID),
|
2020-04-09 04:25:50 +00:00
|
|
|
}
|
|
|
|
}
|