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