-
Notifications
You must be signed in to change notification settings - Fork 927
/
dutil.go
60 lines (45 loc) · 1.18 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
package common
import (
"github.com/jonas747/discordgo/v2"
"github.com/jonas747/dstate/v4"
)
// IsRoleAbove returns wether 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 checjs by lower id
func IsRoleAbove(a, b *discordgo.Role) bool {
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
}