mirror of
https://github.com/diamondburned/arikawa.git
synced 2024-11-30 18:53:30 +00:00
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package arguments
|
|
|
|
import (
|
|
"errors"
|
|
"regexp"
|
|
|
|
"github.com/diamondburned/arikawa/v3/discord"
|
|
)
|
|
|
|
// (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(
|
|
`https://(ptb\.|canary\.)?discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)`,
|
|
)
|
|
|
|
// MessageURL contains info from a MessageURL
|
|
type MessageURL struct {
|
|
GuildID discord.GuildID
|
|
ChannelID discord.ChannelID
|
|
MessageID discord.MessageID
|
|
}
|
|
|
|
func (url *MessageURL) Parse(arg string) error {
|
|
u := ParseMessageURL(arg)
|
|
if u == nil {
|
|
return errors.New("invalid MessageURL format")
|
|
}
|
|
*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{
|
|
GuildID: discord.GuildID(gID),
|
|
ChannelID: discord.ChannelID(cID),
|
|
MessageID: discord.MessageID(mID),
|
|
}
|
|
}
|