forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct_role.go
133 lines (113 loc) · 2.71 KB
/
struct_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package disgord
import "sync"
func NewRole() *Role {
return &Role{}
}
// Role https://discordapp.com/developers/docs/topics/permissions#role-object
type Role struct {
sync.RWMutex `json:"-"`
ID Snowflake `json:"id"`
Name string `json:"name"`
Color uint `json:"color"`
Hoist bool `json:"hoist"`
Position uint `json:"position"`
Permissions uint64 `json:"permissions"`
Managed bool `json:"managed"`
Mentionable bool `json:"mentionable"`
guildID Snowflake
}
func (r *Role) Mention() string {
return "<@&" + r.ID.String() + ">"
}
// SetGuildID link role to a guild before running session.SaveToDiscord(*Role)
func (r *Role) SetGuildID(id Snowflake) {
r.ID = id
}
func (r *Role) DeepCopy() (copy interface{}) {
copy = NewRole()
r.CopyOverTo(copy)
return
}
func (r *Role) CopyOverTo(other interface{}) (err error) {
var ok bool
var role *Role
if role, ok = other.(*Role); !ok {
return NewErrorUnsupportedType("given interface{} was not a *Role")
}
r.RLock()
role.Lock()
role.ID = r.ID
role.Name = r.Name
role.Color = r.Color
role.Hoist = r.Hoist
role.Position = r.Position
role.Permissions = r.Permissions
role.Managed = r.Managed
role.Mentionable = r.Mentionable
role.guildID = r.guildID
r.RUnlock()
role.Unlock()
return
}
func (r *Role) saveToDiscord(session Session) (err error) {
if r.guildID.Empty() {
err = NewErrorMissingSnowflake("role has no guildID")
return
}
var role *Role
if r.ID.Empty() {
// create role
params := CreateGuildRoleParams{
Name: r.Name,
Permissions: r.Permissions,
Color: r.Color,
Hoist: r.Hoist,
Mentionable: r.Mentionable,
}
role, err = session.CreateGuildRole(r.guildID, ¶ms)
if err != nil {
return
}
err = role.CopyOverTo(r)
} else {
// modify/update role
params := ModifyGuildRoleParams{
Name: r.Name,
Permissions: r.Permissions,
Color: r.Color,
Hoist: r.Hoist,
Mentionable: r.Mentionable,
}
role, err = session.ModifyGuildRole(r.guildID, r.ID, ¶ms)
if err != nil {
return
}
if role.Position != r.Position {
// update the position
params := ModifyGuildRolePositionsParams{
ID: r.ID,
Position: r.Position,
}
_, err = session.ModifyGuildRolePositions(r.guildID, ¶ms)
if err != nil {
return
}
role.Position = r.Position
}
}
return
}
func (r *Role) deleteFromDiscord(session Session) (err error) {
if r.ID.Empty() {
err = NewErrorMissingSnowflake("role has no ID")
return
}
if r.guildID.Empty() {
err = NewErrorMissingSnowflake("role has no guildID")
return
}
err = session.DeleteGuildRole(r.guildID, r.ID)
return
}
//func (r *Role) Clear() {
//}