1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-01 12:34:28 +00:00
arikawa/utils/bot/extras/arguments/link.go

63 lines
1.3 KiB
Go
Raw Normal View History

2020-04-09 04:25:50 +00:00
package arguments
import (
"errors"
"regexp"
2021-06-02 02:53:19 +00:00
"github.com/diamondburned/arikawa/v3/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 {
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{
GuildID: discord.GuildID(gID),
ChannelID: discord.ChannelID(cID),
MessageID: discord.MessageID(mID),
2020-04-09 04:25:50 +00:00
}
}