-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmembers.go
202 lines (151 loc) · 5.03 KB
/
members.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
package guildedgo
import (
"encoding/json"
"errors"
"fmt"
"log"
)
type ServerMember struct {
User User `json:"user"`
// (must have unique items true)
RoleIds []int `json:"roleIds"`
Nickname string `json:"nickname,omitempty"`
// The ISO 8601 timestamp that the member was created at
JoinedAt string `json:"joinedAt"`
// (default false)
IsOwner bool `json:"isOwner,omitempty"`
}
type ServerMemberSummary struct {
User UserSummary `json:"user"`
// (must have unique items true)
RoleIds []int `json:"roleIds"`
}
type ServerMemberPermissions struct {
Permissions []string `json:"permissions"`
}
type ServerMemberBan struct {
User UserSummary `json:"user"`
// The reason for the ban as submitted by the banner
Reason string `json:"reason,omitempty"`
// The ID of the user who created this server member ban
CreatedBy string `json:"createdBy"`
// The ISO 8601 timestamp that the server member ban was created at
CreatedAt string `json:"createdAt"`
}
type NicknameResponse struct {
Nickname string `json:"nickname"`
}
type ServerMemberResponse struct {
Member ServerMember `json:"member"`
}
type MembersService interface {
UpdateMemberNickname(userId string, nickname string) (*NicknameResponse, error)
DeleteMemberNickname(userId string) error
GetServerMember(serverId string, userId string) (*ServerMember, error)
KickMember(userId string) error
BanMember(userId string, reason string) (*ServerMemberBan, error)
IsMemberBanned(userId string) (*ServerMemberBan, error)
UnbanMember(userId string) error
GetServerMembers() (*[]ServerMemberSummary, error)
}
type membersEndpoints struct{}
func (e *membersEndpoints) Nickname(serverId, userId string) string {
return guildedApi + "/servers" + serverId + "/members" + userId + "/nickname"
}
func (e *membersEndpoints) GetMember(serverId, userId string) string {
return guildedApi + "/servers/" + serverId + "/members/" + userId
}
func (e *membersEndpoints) GetMembers(serverId string) string {
return guildedApi + "/servers/" + serverId + "/members"
}
func (e *membersEndpoints) Ban(serverId, userId string) string {
return guildedApi + "/servers/" + serverId + "/bans/" + userId
}
type membersService struct {
client *Client
endpoints membersEndpoints
}
var _ MembersService = &membersService{}
func (service *membersService) UpdateMemberNickname(userId string, nickname string) (*NicknameResponse, error) {
endpoint := service.endpoints.Nickname(service.client.ServerID, userId)
body := &NicknameResponse{
Nickname: nickname,
}
resp, err := service.client.PutRequest(endpoint, body)
if err != nil {
return nil, err
}
var nick NicknameResponse
err = json.Unmarshal(resp, &nick)
if err != nil {
return nil, err
}
return &nick, nil
}
func (service *membersService) DeleteMemberNickname(userId string) error {
endpoint := service.endpoints.Nickname(service.client.ServerID, userId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to delete member nickname. Error: %s", err.Error()))
}
return nil
}
func (service *membersService) GetServerMember(serverId string, userId string) (*ServerMember, error) {
endpoint := service.endpoints.GetMember(serverId, userId)
var member ServerMemberResponse
err := service.client.GetRequestV2(endpoint, &member)
if err != nil {
log.Println(err.Error())
return nil, errors.New(fmt.Sprintf("Failed to get member. Error: %s", err.Error()))
}
return &member.Member, nil
}
func (service *membersService) KickMember(userId string) error {
endpoint := service.endpoints.GetMember(service.client.ServerID, userId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
return errors.New(fmt.Sprintf("Failed to kick member. Error: %s", err.Error()))
}
return nil
}
func (service *membersService) BanMember(userId string, reason string) (*ServerMemberBan, error) {
endpoint := service.endpoints.Ban(service.client.ServerID, userId)
// No need to build a struct here
body := map[string]string{
"reason": reason,
}
var ban ServerMemberBan
err := service.client.PostRequestV2(endpoint, body, &ban)
if err != nil {
return nil, err
}
return &ban, nil
}
func (service *membersService) IsMemberBanned(userId string) (*ServerMemberBan, error) {
// Do we want to use the serverID from the config, or manually input it?
endpoint := service.endpoints.Ban(service.client.ServerID, userId)
var ban ServerMemberBan
err := service.client.GetRequestV2(endpoint, &ban)
if err != nil {
return nil, err
}
return &ban, nil
}
func (service *membersService) UnbanMember(userId string) error {
endpoint := service.endpoints.Ban(service.client.ServerID, userId)
_, err := service.client.DeleteRequest(endpoint)
if err != nil {
log.Fatalln(err.Error())
return err
}
return nil
}
func (service *membersService) GetServerMembers() (*[]ServerMemberSummary, error) {
endpoint := service.endpoints.GetMembers(service.client.ServerID)
var members []ServerMemberSummary
err := service.client.GetRequestV2(endpoint, &members)
if err != nil {
return nil, err
}
return &members, nil
}