-
Notifications
You must be signed in to change notification settings - Fork 5
/
openapi_role.go
144 lines (130 loc) · 4.59 KB
/
openapi_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
134
135
136
137
138
139
140
141
142
143
144
package nano
import (
"errors"
"io"
)
const (
RoleIDAll = "1" // 全体成员
RoleIDAdmin = "2" // 管理员
RoleIDCreater = "4" // 群主/创建者
RoleIDChannelAdmin = "5" // 子频道管理员
)
var (
ErrMustGiveChannelID = errors.New("must give channel_id")
)
// Role 频道身份组对象
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/role_model.html
type Role struct {
ID string `json:"id"`
Name string `json:"name"`
Color uint32 `json:"color"`
Hoist uint32 `json:"hoist"`
Number uint32 `json:"number"`
MemberLimit uint32 `json:"member_limit"`
}
// GuildRoleList 频道身份组列表
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild_roles.html#%E8%BF%94%E5%9B%9E
type GuildRoleList struct {
GuildID string `json:"guild_id"`
Roles []Role `json:"roles"`
RoleNumLimit string `json:"role_num_limit"`
}
// GetGuildRoleListIn 获取 guild_id 指定的频道下的身份组列表
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild_roles.html
func (bot *Bot) GetGuildRoleListIn(id string) (*GuildRoleList, error) {
return bot.getOpenAPIofGuildRoleList("/guilds/" + id + "/roles")
}
// GuildRoleCreate 创建频道身份组响应
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/post_guild_role.html#%E8%BF%94%E5%9B%9E
type GuildRoleCreate struct {
RoleID string `json:"role_id"`
Role Role `json:"role"`
}
// CreateGuildRoleOf 创建频道身份组
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/post_guild_role.html
//
// 参数为非必填,但至少需要传其中之一,默认为空或 0
func (bot *Bot) CreateGuildRoleOf(id string, name string, color uint32, hoist int32) (*GuildRoleCreate, error) {
return bot.postOpenAPIofGuildRoleCreate("/guilds/"+id+"/roles", "", WriteBodyFromJSON(&struct {
N string `json:"name,omitempty"`
C uint32 `json:"color,omitempty"`
H int32 `json:"hoist,omitempty"`
}{name, color, hoist}))
}
// GuildRolePatch 修改频道身份组
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_role.html#%E8%BF%94%E5%9B%9E
type GuildRolePatch struct {
GuildID string `json:"guild_id"`
RoleID string `json:"role_id"`
Role Role `json:"role"`
}
// PatchGuildRoleOf 修改频道 guild_id 下 role_id 指定的身份组
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_role.html
func (bot *Bot) PatchGuildRoleOf(guildid, roleid string, name string, color uint32, hoist int32) (*GuildRolePatch, error) {
return bot.patchOpenAPIofGuildRolePatch("/guilds/"+guildid+"/roles/"+roleid, WriteBodyFromJSON(&struct {
N string `json:"name,omitempty"`
C uint32 `json:"color,omitempty"`
H int32 `json:"hoist,omitempty"`
}{name, color, hoist}))
}
// DeleteGuildRoleOf 删除频道 guild_id下 role_id 对应的身份组
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_role.html
func (bot *Bot) DeleteGuildRoleOf(guildid, roleid string) error {
return bot.DeleteOpenAPI("/guilds/"+guildid+"/roles/"+roleid, "", nil)
}
// GuildRoleChannelID 频道身份组成员返回 只填充了子频道 id 字段的对象
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/put_guild_member_role.html#%E5%8F%82%E6%95%B0
type GuildRoleChannelID struct {
Channel struct {
ID string `json:"id"`
} `json:"channel"`
}
// AddRoleToMemberOfGuild 将频道 guild_id 下的用户 user_id 添加到身份组 role_id
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/put_guild_member_role.html
//
// 返回 channel_id
func (bot *Bot) AddRoleToMemberOfGuild(guildid, userid, roleid, channelid string) (string, error) {
var body io.Reader
if roleid == RoleIDChannelAdmin {
if channelid == "" {
return "", ErrMustGiveChannelID
}
body = WriteBodyFromJSON(&GuildRoleChannelID{
Channel: struct {
ID string `json:"id"`
}{channelid},
})
}
r, err := bot.putOpenAPIofGuildRoleChannelID("/guilds/"+guildid+"/members/"+userid+"/roles/"+roleid, body)
if err != nil {
return "", err
}
return r.Channel.ID, nil
}
// RemoveRoleFromMemberOfGuild 将用户 user_id 从 频道 guild_id 的 role_id 身份组中移除
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_member_role.html
func (bot *Bot) RemoveRoleFromMemberOfGuild(guildid, userid, roleid, channelid string) error {
var body io.Reader
if roleid == RoleIDChannelAdmin {
if channelid == "" {
return ErrMustGiveChannelID
}
body = WriteBodyFromJSON(&GuildRoleChannelID{
Channel: struct {
ID string `json:"id"`
}{channelid},
})
}
return bot.DeleteOpenAPI("/guilds/"+guildid+"/members/"+userid+"/roles/"+roleid, "", body)
}