github.com/diamondburned/arikawa/v2@v2.1.0/bot/extras/arguments/mention.go (about) 1 package arguments 2 3 import ( 4 "errors" 5 "regexp" 6 7 "github.com/diamondburned/arikawa/v2/discord" 8 ) 9 10 var ( 11 ChannelRegex = regexp.MustCompile(`<#(\d+)>`) 12 UserRegex = regexp.MustCompile(`<@!?(\d+)>`) 13 RoleRegex = regexp.MustCompile(`<@&(\d+)>`) 14 ) 15 16 // 17 18 type ChannelMention discord.ChannelID 19 20 func (m *ChannelMention) Parse(arg string) error { 21 return grabFirst(ChannelRegex, "channel mention", arg, (*discord.Snowflake)(m)) 22 } 23 24 func (m *ChannelMention) Usage() string { 25 return "#channel" 26 } 27 28 func (m *ChannelMention) ID() discord.ChannelID { 29 return discord.ChannelID(*m) 30 } 31 32 func (m *ChannelMention) Mention() string { 33 return "<#" + m.ID().String() + ">" 34 } 35 36 // 37 38 type UserMention discord.UserID 39 40 func (m *UserMention) Parse(arg string) error { 41 return grabFirst(UserRegex, "user mention", arg, (*discord.Snowflake)(m)) 42 } 43 44 func (m *UserMention) Usage() string { 45 return "@user" 46 } 47 48 func (m *UserMention) ID() discord.UserID { 49 return discord.UserID(*m) 50 } 51 52 func (m *UserMention) Mention() string { 53 return "<@" + m.ID().String() + ">" 54 } 55 56 // 57 58 type RoleMention discord.RoleID 59 60 func (m *RoleMention) Parse(arg string) error { 61 return grabFirst(RoleRegex, "role mention", arg, (*discord.Snowflake)(m)) 62 } 63 64 func (m *RoleMention) Usage() string { 65 return "@role" 66 } 67 68 func (m *RoleMention) ID() discord.RoleID { 69 return discord.RoleID(*m) 70 } 71 72 func (m *RoleMention) Mention() string { 73 return "<@&" + m.ID().String() + ">" 74 } 75 76 // 77 78 func grabFirst(reg *regexp.Regexp, item, input string, output *discord.Snowflake) error { 79 matches := reg.FindStringSubmatch(input) 80 if len(matches) < 2 { 81 return errors.New("invalid " + item) 82 } 83 84 id, err := discord.ParseSnowflake(matches[1]) 85 if err != nil { 86 return errors.New("invalid " + item) 87 } 88 89 *output = id 90 return nil 91 }