-
Notifications
You must be signed in to change notification settings - Fork 0
/
args.go
137 lines (113 loc) · 3.31 KB
/
args.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package gumi
import (
"regexp"
"strconv"
"strings"
)
var (
// RegexUserMention defines the regex a user mention has to match
RegexUserMention = regexp.MustCompile("<@!?(\\d+)>")
// RegexRoleMention defines the regex a role mention has to match
RegexRoleMention = regexp.MustCompile("<@&(\\d+)>")
// RegexChannelMention defines the regex a channel mention has to match
RegexChannelMention = regexp.MustCompile("<#(\\d+)>")
)
//Arguments represents arguments used in a command.
type Arguments struct {
Raw string
Arguments []*Argument
}
//Argument represents a single argument
type Argument struct {
Raw string
}
func ParseArguments(raw string) *Arguments {
fields := strings.Fields(raw)
args := make([]*Argument, 0, len(fields))
for _, f := range fields {
args = append(args, &Argument{f})
}
return &Arguments{
Arguments: args,
Raw: raw,
}
}
// AsSingle parses the given arguments as a single one
func (arguments *Arguments) AsSingle() *Argument {
return &Argument{
Raw: arguments.Raw,
}
}
// Len returns the length of the arguments
func (arguments *Arguments) Len() int {
return len(arguments.Arguments)
}
// Get returns the n'th argument
func (arguments *Arguments) Get(n int) *Argument {
if arguments.Len() <= n {
return &Argument{
Raw: "",
}
}
return arguments.Arguments[n]
}
// Remove removes the n'th argument
func (arguments *Arguments) Remove(n int) {
// Check if the given index is valid
if arguments.Len() <= n {
return
}
// Set the new argument slice
arguments.Arguments = append(arguments.Arguments[:n], arguments.Arguments[n+1:]...)
// Set the new raw string
raw := ""
for _, argument := range arguments.Arguments {
raw += argument.Raw + " "
}
arguments.Raw = strings.TrimSpace(raw)
}
// AsBool parses the given argument into a boolean
func (argument *Argument) AsBool() (bool, error) {
return strconv.ParseBool(argument.Raw)
}
// AsInt parses the given argument into an int32
func (argument *Argument) AsInt() (int, error) {
return strconv.Atoi(argument.Raw)
}
// AsInt64 parses the given argument into an int64
func (argument *Argument) AsInt64() (int64, error) {
return strconv.ParseInt(argument.Raw, 10, 64)
}
// AsUserMentionID returns the ID of the mentioned user or an empty string if it is no mention
func (argument *Argument) AsUserMentionID() string {
// Check if the argument is a user mention
matches := RegexUserMention.MatchString(argument.Raw)
if !matches {
return ""
}
// Parse the user ID
userID := RegexUserMention.FindStringSubmatch(argument.Raw)[1]
return userID
}
// AsRoleMentionID returns the ID of the mentioned role or an empty string if it is no mention
func (argument *Argument) AsRoleMentionID() string {
// Check if the argument is a role mention
matches := RegexRoleMention.MatchString(argument.Raw)
if !matches {
return ""
}
// Parse the role ID
roleID := RegexRoleMention.FindStringSubmatch(argument.Raw)[1]
return roleID
}
// AsChannelMentionID returns the ID of the mentioned channel or an empty string if it is no mention
func (argument *Argument) AsChannelMentionID() string {
// Check if the argument is a channel mention
matches := RegexChannelMention.MatchString(argument.Raw)
if !matches {
return ""
}
// Parse the channel ID
channelID := RegexChannelMention.FindStringSubmatch(argument.Raw)[1]
return channelID
}