This repository has been archived by the owner on Apr 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getters.go
284 lines (222 loc) · 6.12 KB
/
getters.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"database/sql"
"errors"
"fmt"
"math"
"github.com/bwmarrin/discordgo"
)
// Channels
func getWelcomeChannel(s *discordgo.Session, g *discordgo.Guild) (*discordgo.Channel, error) {
// Select the presentation channel
channelID, err := selectWelcomeChannel(g)
if err != nil {
return nil, err
}
// Turn it into a channel
return s.Channel(channelID)
}
func getPresentationChannel(s *discordgo.Session, g *discordgo.Guild) (channel *discordgo.Channel, err error) {
// Select the presentation channel
channelID, err := selectPresentationChannel(g)
if err != nil {
return nil, err
}
// Turn it into a channel
return s.Channel(channelID)
}
func getFameChannel(s *discordgo.Session, g *discordgo.Guild) (channel *discordgo.Channel, err error) {
// Select the hall of fame channel
channelID, err := selectFameChannel(g)
if err != nil {
return nil, err
}
// Turn it into a channel
return s.Channel(channelID)
}
// Roles
func getRoles(s *discordgo.Session, g *discordgo.Guild) (admin *discordgo.Role, mod *discordgo.Role, light *discordgo.Role, absynthe *discordgo.Role, obsidian *discordgo.Role, shadow *discordgo.Role, eel *discordgo.Role, npc *discordgo.Role) {
admin, _ = getRoleAdmin(s, g)
mod, _ = getRoleMod(s, g)
light, _ = getRoleLight(s, g)
absynthe, _ = getRoleAbsynthe(s, g)
obsidian, _ = getRoleObsidian(s, g)
shadow, _ = getRoleShadow(s, g)
eel, _ = getRoleEel(s, g)
npc, _ = getRoleNPC(s, g)
return admin, mod, light, absynthe, obsidian, shadow, eel, npc
}
func getRole(s *discordgo.Session, g *discordgo.Guild, table string) (role *discordgo.Role, err error) {
// Get a role ID from the database
roleID, err := selectRole(g, table)
if err != nil {
return nil, err
}
// Get Role
return s.State.Role(g.ID, roleID)
}
func getRoleAdmin(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableAdmin)
}
func getRoleMod(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableMod)
}
func getRoleLight(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableLight)
}
func getRoleAbsynthe(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableAbsynthe)
}
func getRoleObsidian(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableObsidian)
}
func getRoleShadow(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableShadow)
}
func getRoleEel(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableEel)
}
func getRoleNPC(s *discordgo.Session, g *discordgo.Guild) (role *discordgo.Role, err error) {
return getRole(s, g, tableNPC)
}
func getRoleByString(s *discordgo.Session, g *discordgo.Guild, str string) (role *discordgo.Role, err error) {
// Get roles
roles, err := s.GuildRoles(g.ID)
if err != nil {
fmt.Println("Couldn't get the guild's roles.")
fmt.Println("Guild :", g.Name)
fmt.Println("Role :", str)
fmt.Println(err.Error())
return
}
// Return the first occurence
for _, role := range roles {
if role.Name == str || role.ID == str {
return role, nil
}
}
return nil, errors.New("this role doesn't exist")
}
// Self-Assignable Role
func getSAR(s *discordgo.Session, g *discordgo.Guild, r *discordgo.Role) (role *discordgo.Role, err error) {
// Get a role ID from the database
roleID, err := selectSAR(g, r)
if err != nil {
return nil, err
}
// Get Role
return s.State.Role(g.ID, roleID)
}
// Self-Assignable Roles
func getSARs(s *discordgo.Session, g *discordgo.Guild) (roles []*discordgo.Role, err error) {
// Get role IDs from the database
roleIDs, err := selectSARs(g)
if err != nil {
return roles, err
}
// Append each and every roles
for _, roleID := range roleIDs {
role, err := s.State.Role(g.ID, roleID)
if err != nil {
continue
}
roles = append(roles, role)
}
return
}
// Colours
func getColour(s *discordgo.Session, g *discordgo.Guild, m *discordgo.Member) (colour int, err error) {
// Check if the member is null.
// This function should be re-written to accept the user then get its member.
if m == nil {
return colourNPC, err
}
admin, err := isAdmin(s, g, m)
if admin {
return colourAdministrator, err
}
mod, err := isMod(s, g, m)
if mod {
return colourModerator, err
}
light, err := isLight(s, g, m)
if light {
return colourLight, err
}
absynthe, err := isAbsynthe(s, g, m)
if absynthe {
return colourAbsynthe, err
}
obsidian, err := isObsidian(s, g, m)
if obsidian {
return colourObsidian, err
}
shadow, err := isShadow(s, g, m)
if shadow {
return colourShadow, err
}
eel, err := isEel(s, g, m)
if eel {
return colourEel, err
}
npc, err := isNPC(s, g, m)
if npc {
return colourNPC, err
}
if m.User.Bot {
return colourBot, err
}
return colourNPC, err
}
// Reactions
func getMinimumReactions(g *discordgo.Guild, c *discordgo.Channel) (int, error) {
// Guild
var onlineCount float64
for _, presence := range g.Presences {
if presence.Status == discordgo.StatusOnline {
onlineCount++
}
}
guildMin := int(math.Ceil(math.Sqrt(onlineCount)))
// Channel
channelMin, err := selectMinimumReactions(c)
if err == sql.ErrNoRows {
_, err = insertMinimumReactions(c, pinAbsoluteMinimum)
}
// Absolute
minimums := []int{
guildMin,
channelMin,
pinAbsoluteMinimum,
}
// Get the maximum minimum
var max int
for _, min := range minimums {
if min > max {
max = min
}
}
return max, err
}
// Lover
func getLover(s *discordgo.Session, g *discordgo.Guild) (member *discordgo.Member, err error) {
// Get potential lovers
members, err := selectLovers(g)
if err != nil {
return nil, err
}
// For each potential lovers
for _, potential := range members {
// Get member
member, err := s.GuildMember(g.ID, potential)
if err != nil {
continue
}
// Check if light / mod / admin / owner
is, err := isLightOrOver(s, g, member)
if !is {
return member, err
}
}
return nil, errors.New("there's no potential lover in this guild")
}