github.com/diamondburned/arikawa/v2@v2.1.0/bot/extras/arguments/link.go (about) 1 package arguments 2 3 import ( 4 "errors" 5 "regexp" 6 7 "github.com/diamondburned/arikawa/v2/discord" 8 ) 9 10 // (empty) so it matches standard links 11 // | OR 12 // canary. matches canary MessageURL 13 // 3 `(\d+)` for guild ID, channel ID and message ID 14 var Regex = regexp.MustCompile( 15 `https://(ptb\.|canary\.)?discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)`, 16 ) 17 18 // MessageURL contains info from a MessageURL 19 type MessageURL struct { 20 GuildID discord.GuildID 21 ChannelID discord.ChannelID 22 MessageID discord.MessageID 23 } 24 25 func (url *MessageURL) Parse(arg string) error { 26 u := ParseMessageURL(arg) 27 if u == nil { 28 return errors.New("invalid MessageURL format") 29 } 30 *url = *u 31 return nil 32 } 33 34 func (url *MessageURL) Usage() string { 35 return "https\u200b://discordapp.com/channels/\\*/\\*/\\*" 36 } 37 38 // ParseMessageURL parses the MessageURL into a smartlink 39 func ParseMessageURL(url string) *MessageURL { 40 ss := Regex.FindAllStringSubmatch(url, -1) 41 if ss == nil { 42 return nil 43 } 44 45 if len(ss) == 0 || len(ss[0]) != 5 { 46 return nil 47 } 48 49 gID, err1 := discord.ParseSnowflake(ss[0][2]) 50 cID, err2 := discord.ParseSnowflake(ss[0][3]) 51 mID, err3 := discord.ParseSnowflake(ss[0][4]) 52 53 if err1 != nil || err2 != nil || err3 != nil { 54 return nil 55 } 56 57 return &MessageURL{ 58 GuildID: discord.GuildID(gID), 59 ChannelID: discord.ChannelID(cID), 60 MessageID: discord.MessageID(mID), 61 } 62 }