-
Notifications
You must be signed in to change notification settings - Fork 1
/
role.go
110 lines (94 loc) · 2.92 KB
/
role.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
package discord
import (
"github.com/BOOMfinity/bfcord/discord/permissions"
"github.com/BOOMfinity/go-utils/inlineif"
"github.com/andersfylling/snowflake/v5"
"golang.org/x/exp/slices"
)
// Role
//
// Reference: https://discord.com/developers/docs/topics/permissions#role-object
type Role struct {
Name string `json:"name"`
Icon string `json:"icon,omitempty"`
UnicodeEmoji string `json:"unicode_emoji,omitempty"`
Permissions permissions.Permission `json:"permissions"`
Tags RoleTags `json:"tags,omitempty"`
ID snowflake.ID `json:"id"`
Color int `json:"color"`
Position int `json:"position"`
GuildID snowflake.ID `json:"guild_id"`
Hoist bool `json:"hoist"`
Managed bool `json:"managed"`
Mentionable bool `json:"mentionable"`
}
func (r Role) Guild(api ClientQuery) GuildQuery {
return api.Guild(r.GuildID)
}
// ComparePosition compares this role's position to other one.
func (r Role) ComparePosition(other *Role) int {
if r.Position == other.Position {
return int(other.ID - r.ID)
}
return r.Position - other.Position
}
func (r Role) Mention() string {
return "<@" + r.ID.String() + ">"
}
// RoleTags
//
// Reference: https://discord.com/developers/docs/topics/permissions#role-object-role-tags-structure
type RoleTags struct {
BotID snowflake.ID `json:"bot_id,omitempty"`
IntegrationID snowflake.ID `json:"integration_id,omitempty"`
PremiumSubscriber snowflake.ID `json:"premium_subscriber,omitempty"`
}
type RoleCreate struct {
Name *string `json:"name,omitempty"`
Permissions *permissions.Permission `json:"permissions,omitempty"`
Color *int64 `json:"color,omitempty"`
Hoist *bool `json:"hoist,omitempty"`
Icon *string `json:"icon,omitempty"`
UnicodeEmoji *string `json:"unicode_emoji,omitempty"`
Mentionable *bool `json:"mentionable,omitempty"`
}
type RoleSlice []Role
func (rs RoleSlice) Highest() (highest *Role) {
if len(rs) == 0 {
return nil
}
for i := range rs {
if highest == nil {
highest = &rs[0]
continue
}
if rs[i].ComparePosition(highest) > 0 {
highest = &rs[i]
}
}
return
}
func (rs RoleSlice) HighestWithin(member *Member) (highest *Role) {
if len(rs) == 0 {
return nil
}
for i := range rs {
if highest == nil {
highest = inlineif.IfElse(slices.Contains(member.Roles, rs[i].ID), &rs[i], nil)
continue
}
if slices.Contains(member.Roles, rs[i].ID) && rs[i].ComparePosition(highest) > 0 {
highest = &rs[i]
}
}
return
}
func (rs RoleSlice) Find(id snowflake.ID) *Role {
index := slices.IndexFunc(rs, func(r Role) bool {
return r.ID == id
})
if index == -1 {
return nil
}
return &rs[index]
}