forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dutil.go
70 lines (53 loc) · 1.27 KB
/
dutil.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
package common
import (
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
)
// IsRoleAbove returns whether role a is above b,
// checking positions first,
// and if they're the same
// (both being 1, new roles always have 1 as position)
// then it checks by lower id
func IsRoleAbove(a, b *discordgo.Role) bool {
if a == nil {
return false
}
if b == nil {
return true
}
if a.Position != b.Position {
return a.Position > b.Position
}
if a.ID == b.ID {
return false
}
return a.ID < b.ID
}
// Channels are a collection of Channels
type DiscordChannels []*discordgo.Channel
func (r DiscordChannels) Len() int {
return len(r)
}
func (r DiscordChannels) Less(i, j int) bool {
return r[i].Position < r[j].Position
}
func (r DiscordChannels) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
type DiscordRoles []*discordgo.Role
func (r DiscordRoles) Len() int {
return len(r)
}
func (r DiscordRoles) Less(i, j int) bool {
return IsRoleAbove(r[i], r[j])
}
func (r DiscordRoles) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}
// ChannelOrThreadParentID returns either cs.ID for channels or cs.ParentID for threads
func ChannelOrThreadParentID(cs *dstate.ChannelState) int64 {
if cs.Type.IsThread() {
return cs.ParentID
}
return cs.ID
}