github.com/fumiama/NanoBot@v0.0.0-20231122134259-c22d8183efca/openapi_role.go (about) 1 package nano 2 3 import ( 4 "errors" 5 "io" 6 ) 7 8 const ( 9 RoleIDAll = "1" // 全体成员 10 RoleIDAdmin = "2" // 管理员 11 RoleIDCreater = "4" // 群主/创建者 12 RoleIDChannelAdmin = "5" // 子频道管理员 13 ) 14 15 var ( 16 ErrMustGiveChannelID = errors.New("must give channel_id") 17 ) 18 19 // Role 频道身份组对象 20 // 21 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/role_model.html 22 type Role struct { 23 ID string `json:"id"` 24 Name string `json:"name"` 25 Color uint32 `json:"color"` 26 Hoist uint32 `json:"hoist"` 27 Number uint32 `json:"number"` 28 MemberLimit uint32 `json:"member_limit"` 29 } 30 31 // GuildRoleList 频道身份组列表 32 // 33 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild_roles.html#%E8%BF%94%E5%9B%9E 34 type GuildRoleList struct { 35 GuildID string `json:"guild_id"` 36 Roles []Role `json:"roles"` 37 RoleNumLimit string `json:"role_num_limit"` 38 } 39 40 // GetGuildRoleListIn 获取 guild_id 指定的频道下的身份组列表 41 // 42 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild_roles.html 43 func (bot *Bot) GetGuildRoleListIn(id string) (*GuildRoleList, error) { 44 return bot.getOpenAPIofGuildRoleList("/guilds/" + id + "/roles") 45 } 46 47 // GuildRoleCreate 创建频道身份组响应 48 // 49 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/post_guild_role.html#%E8%BF%94%E5%9B%9E 50 type GuildRoleCreate struct { 51 RoleID string `json:"role_id"` 52 Role Role `json:"role"` 53 } 54 55 // CreateGuildRoleOf 创建频道身份组 56 // 57 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/post_guild_role.html 58 // 59 // 参数为非必填,但至少需要传其中之一,默认为空或 0 60 func (bot *Bot) CreateGuildRoleOf(id string, name string, color uint32, hoist int32) (*GuildRoleCreate, error) { 61 return bot.postOpenAPIofGuildRoleCreate("/guilds/"+id+"/roles", "", WriteBodyFromJSON(&struct { 62 N string `json:"name,omitempty"` 63 C uint32 `json:"color,omitempty"` 64 H int32 `json:"hoist,omitempty"` 65 }{name, color, hoist})) 66 } 67 68 // GuildRolePatch 修改频道身份组 69 // 70 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_role.html#%E8%BF%94%E5%9B%9E 71 type GuildRolePatch struct { 72 GuildID string `json:"guild_id"` 73 RoleID string `json:"role_id"` 74 Role Role `json:"role"` 75 } 76 77 // PatchGuildRoleOf 修改频道 guild_id 下 role_id 指定的身份组 78 // 79 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/patch_guild_role.html 80 func (bot *Bot) PatchGuildRoleOf(guildid, roleid string, name string, color uint32, hoist int32) (*GuildRolePatch, error) { 81 return bot.patchOpenAPIofGuildRolePatch("/guilds/"+guildid+"/roles/"+roleid, WriteBodyFromJSON(&struct { 82 N string `json:"name,omitempty"` 83 C uint32 `json:"color,omitempty"` 84 H int32 `json:"hoist,omitempty"` 85 }{name, color, hoist})) 86 } 87 88 // DeleteGuildRoleOf 删除频道 guild_id下 role_id 对应的身份组 89 // 90 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_role.html 91 func (bot *Bot) DeleteGuildRoleOf(guildid, roleid string) error { 92 return bot.DeleteOpenAPI("/guilds/"+guildid+"/roles/"+roleid, "", nil) 93 } 94 95 // GuildRoleChannelID 频道身份组成员返回 只填充了子频道 id 字段的对象 96 // 97 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/put_guild_member_role.html#%E5%8F%82%E6%95%B0 98 type GuildRoleChannelID struct { 99 Channel struct { 100 ID string `json:"id"` 101 } `json:"channel"` 102 } 103 104 // AddRoleToMemberOfGuild 将频道 guild_id 下的用户 user_id 添加到身份组 role_id 105 // 106 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/put_guild_member_role.html 107 // 108 // 返回 channel_id 109 func (bot *Bot) AddRoleToMemberOfGuild(guildid, userid, roleid, channelid string) (string, error) { 110 var body io.Reader 111 if roleid == RoleIDChannelAdmin { 112 if channelid == "" { 113 return "", ErrMustGiveChannelID 114 } 115 body = WriteBodyFromJSON(&GuildRoleChannelID{ 116 Channel: struct { 117 ID string `json:"id"` 118 }{channelid}, 119 }) 120 } 121 r, err := bot.putOpenAPIofGuildRoleChannelID("/guilds/"+guildid+"/members/"+userid+"/roles/"+roleid, body) 122 if err != nil { 123 return "", err 124 } 125 return r.Channel.ID, nil 126 } 127 128 // RemoveRoleFromMemberOfGuild 将用户 user_id 从 频道 guild_id 的 role_id 身份组中移除 129 // 130 // https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_member_role.html 131 func (bot *Bot) RemoveRoleFromMemberOfGuild(guildid, userid, roleid, channelid string) error { 132 var body io.Reader 133 if roleid == RoleIDChannelAdmin { 134 if channelid == "" { 135 return ErrMustGiveChannelID 136 } 137 body = WriteBodyFromJSON(&GuildRoleChannelID{ 138 Channel: struct { 139 ID string `json:"id"` 140 }{channelid}, 141 }) 142 } 143 return bot.DeleteOpenAPI("/guilds/"+guildid+"/members/"+userid+"/roles/"+roleid, "", body) 144 }