-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
fetch.go
235 lines (216 loc) · 7.46 KB
/
fetch.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Package fetch provides functionalities to fetch roles,
// channels, members and users by so called resolavbles.
// That means, these functions try to match a member, role
// or channel by their names, displaynames, IDs or mentions
// as greedy as prossible.
package fetch
import (
"regexp"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
RoleCheckFuncs = []func(*discordgo.Role, string) bool{
// 1. ID exact match
func(r *discordgo.Role, resolvable string) bool {
return r.ID == resolvable
},
// 2. name exact match
func(r *discordgo.Role, resolvable string) bool {
return r.Name == resolvable
},
// 3. name lowercased exact match
func(r *discordgo.Role, resolvable string) bool {
return strings.EqualFold(r.Name, resolvable)
},
// 4. name lowercased startswith
func(r *discordgo.Role, resolvable string) bool {
return strings.HasPrefix(strings.ToLower(r.Name), strings.ToLower(resolvable))
},
// 5. name lowercased contains
func(r *discordgo.Role, resolvable string) bool {
return strings.Contains(strings.ToLower(r.Name), strings.ToLower(resolvable))
},
}
MemberCheckFuncs = []func(*discordgo.Member, string) bool{
// 1. ID exact match
func(r *discordgo.Member, resolvable string) bool {
return r.User.ID == resolvable
},
// 2. username exact match
func(r *discordgo.Member, resolvable string) bool {
return r.User.Username == resolvable
},
// 3. username lowercased exact match
func(r *discordgo.Member, resolvable string) bool {
return strings.EqualFold(r.User.Username, resolvable)
},
// 4. username lowercased startswith
func(r *discordgo.Member, resolvable string) bool {
return strings.HasPrefix(strings.ToLower(r.User.Username), strings.ToLower(resolvable))
},
// 5. username lowercased contains
func(r *discordgo.Member, resolvable string) bool {
return strings.Contains(strings.ToLower(r.User.Username), strings.ToLower(resolvable))
},
// 6. nick exact match
func(r *discordgo.Member, resolvable string) bool {
return r.Nick == resolvable
},
// 7. nick lowercased exact match
func(r *discordgo.Member, resolvable string) bool {
return r.Nick != "" && strings.EqualFold(r.Nick, resolvable)
},
// 8. nick lowercased starts with
func(r *discordgo.Member, resolvable string) bool {
return r.Nick != "" && strings.HasPrefix(strings.ToLower(r.Nick), strings.ToLower(resolvable))
},
// 9. nick lowercased contains
func(r *discordgo.Member, resolvable string) bool {
return r.Nick != "" && strings.Contains(strings.ToLower(r.Nick), strings.ToLower(resolvable))
},
}
ChannelCheckFuncs = []func(*discordgo.Channel, string) bool{
// 1. ID exact match
func(r *discordgo.Channel, resolvable string) bool {
return r.ID == resolvable
},
// 2. mention exact match
func(r *discordgo.Channel, resolvable string) bool {
l := len(resolvable)
return l > 3 && r.ID == resolvable[2:l-1]
},
// 3. name exact match
func(r *discordgo.Channel, resolvable string) bool {
return r.Name == resolvable
},
// 4. name lowercased exact match
func(r *discordgo.Channel, resolvable string) bool {
return strings.EqualFold(r.Name, resolvable)
},
// 5. name lowercased starts with
func(r *discordgo.Channel, resolvable string) bool {
return strings.HasPrefix(strings.ToLower(r.Name), strings.ToLower(resolvable))
},
// 6. name lowercased contains
func(r *discordgo.Channel, resolvable string) bool {
return strings.Contains(strings.ToLower(r.Name), strings.ToLower(resolvable))
},
}
GuildCheckFuncs = []func(*discordgo.Guild, string) bool{
// 1. ID exact match
func(r *discordgo.Guild, resolvable string) bool {
return r.ID == resolvable
},
// 2. mention exact match
func(r *discordgo.Guild, resolvable string) bool {
l := len(resolvable)
return l > 3 && r.ID == resolvable[2:l-1]
},
// 3. name exact match
func(r *discordgo.Guild, resolvable string) bool {
return r.Name == resolvable
},
// 4. name lowercased exact match
func(r *discordgo.Guild, resolvable string) bool {
return strings.EqualFold(r.Name, resolvable)
},
// 5. name lowercased starts with
func(r *discordgo.Guild, resolvable string) bool {
return strings.HasPrefix(strings.ToLower(r.Name), strings.ToLower(resolvable))
},
// 6. name lowercased contains
func(r *discordgo.Guild, resolvable string) bool {
return strings.Contains(strings.ToLower(r.Name), strings.ToLower(resolvable))
},
}
)
// FetchRole tries to fetch a role on the specified guild
// by given resolvable and returns this role, when found.
// You can pass a condition function which ignores the result
// if this functions returns false on the given object.
// If no object was found, ErrNotFound is returned.
// If any other unexpected error occurs during fetching,
// this error is returned as well.
func FetchRole(s DataOutlet, guildID, resolvable string, condition ...func(*discordgo.Role) bool) (*discordgo.Role, error) {
roles, err := s.GuildRoles(guildID)
if err != nil {
return nil, err
}
rx := regexp.MustCompile("<@&|>")
resolvable = rx.ReplaceAllString(resolvable, "")
for _, checkFunc := range RoleCheckFuncs {
for _, r := range roles {
if len(condition) > 0 && condition[0] != nil {
if !condition[0](r) {
continue
}
}
if checkFunc(r, resolvable) {
return r, nil
}
}
}
return nil, ErrNotFound
}
// FetchMember tries to fetch a member on the specified guild
// by given resolvable and returns this member, when found.
// You can pass a condition function which ignores the result
// if this functions returns false on the given object.
// If no object was found, ErrNotFound is returned.
// If any other unexpected error occurs during fetching,
// this error is returned as well.
func FetchMember(s DataOutlet, guildID, resolvable string, condition ...func(*discordgo.Member) bool) (*discordgo.Member, error) {
rx := regexp.MustCompile("<@|!|>")
resolvable = rx.ReplaceAllString(resolvable, "")
var lastUserID string
for {
members, err := s.GuildMembers(guildID, lastUserID, 1000)
if err != nil {
return nil, err
}
if len(members) < 1 {
break
}
lastUserID = members[len(members)-1].User.ID
for _, checkFunc := range MemberCheckFuncs {
for _, m := range members {
if len(condition) > 0 && condition[0] != nil {
if !condition[0](m) {
continue
}
}
if checkFunc(m, resolvable) {
return m, nil
}
}
}
}
return nil, ErrNotFound
}
// FetchChannel tries to fetch a channel on the specified guild
// by given resolvable and returns this channel, when found.
// You can pass a condition function which ignores the result
// if this functions returns false on the given object.
// If no object was found, ErrNotFound is returned.
// If any other unexpected error occurs during fetching,
// this error is returned as well.
func FetchChannel(s DataOutlet, guildID, resolvable string, condition ...func(*discordgo.Channel) bool) (*discordgo.Channel, error) {
channels, err := s.GuildChannels(guildID)
if err != nil {
return nil, err
}
for _, checkFunc := range ChannelCheckFuncs {
for _, c := range channels {
if len(condition) > 0 && condition[0] != nil {
if !condition[0](c) {
continue
}
}
if checkFunc(c, resolvable) {
return c, nil
}
}
}
return nil, ErrNotFound
}