From 4745fa990e31fd8722f8ce49d2edf272eb1538a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 11 Oct 2023 00:16:44 +0900 Subject: [PATCH] finish role --- codegen/putopenapiof/main.go | 51 +++++++++++++++++++++++++ http.go | 10 +++++ openapi.go | 36 ++++++++++++++++++ openapi_codegen_putopenapiof.go | 23 +++++++++++ openapi_role.go | 67 +++++++++++++++++++++++++++++++++ 5 files changed, 187 insertions(+) create mode 100644 codegen/putopenapiof/main.go create mode 100644 openapi_codegen_putopenapiof.go diff --git a/codegen/putopenapiof/main.go b/codegen/putopenapiof/main.go new file mode 100644 index 0000000..ad672cd --- /dev/null +++ b/codegen/putopenapiof/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "os" + "strings" +) + +const head = `// Code generated by codegen/putopenapiof. DO NOT EDIT. + +package nano + +import ( + "io" + "unsafe" + + "github.com/pkg/errors" +) +` + +const template = ` +func (bot *Bot) putOpenAPIof[T any](ep string, body io.Reader) (*[T any], error) { + resp := &struct { + CodeMessageBase + [T any] + }{} + err := bot.PutOpenAPI(ep, resp, body) + if err != nil { + err = errors.Wrap(err, getCallerFuncName()) + return nil, err + } + return (*[T any])(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil +} +` + +func main() { + f, err := os.Create("openapi_codegen_putopenapiof.go") + if err != nil { + panic(err) + } + defer f.Close() + _, err = f.WriteString(head) + if err != nil { + panic(err) + } + for _, name := range os.Args[1:] { + _, err = f.WriteString(strings.ReplaceAll(template, "[T any]", name)) + if err != nil { + panic(err) + } + } +} diff --git a/http.go b/http.go index 0da0d08..265d7ed 100644 --- a/http.go +++ b/http.go @@ -21,6 +21,16 @@ func NewHTTPEndpointGetRequestWithAuth(ep string, auth string) (req *http.Reques return } +// NewHTTPEndpointPutRequestWithAuth 新建带鉴权头的 HTTP PUT 请求 +func NewHTTPEndpointPutRequestWithAuth(ep string, auth string, body io.Reader) (req *http.Request, err error) { + req, err = http.NewRequest("PUT", StandardAPI+ep, body) + if err != nil { + return + } + req.Header.Add("Authorization", auth) + return +} + // NewHTTPEndpointDeleteRequestWithAuth 新建带鉴权头的 HTTP DELETE 请求 func NewHTTPEndpointDeleteRequestWithAuth(ep string, auth string, body io.Reader) (req *http.Request, err error) { req, err = http.NewRequest("DELETE", StandardAPI+ep, body) diff --git a/openapi.go b/openapi.go index b368744..82b4601 100644 --- a/openapi.go +++ b/openapi.go @@ -47,6 +47,42 @@ func (bot *Bot) GetOpenAPI(ep string, ptr any) error { return nil } +//go:generate go run codegen/putopenapiof/main.go GuildRoleChannelID + +// PutOpenAPI 向 ep 发送 PUT 并获取 json 结构化数据返回写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase +func (bot *Bot) PutOpenAPI(ep string, ptr any, body io.Reader) error { + req, err := NewHTTPEndpointPutRequestWithAuth(ep, bot.Authorization(), body) + if err != nil { + return errors.Wrap(err, getCallerFuncName()) + } + resp, err := http.DefaultClient.Do(req) + if err != nil { + return errors.Wrap(err, getCallerFuncName()) + } + defer resp.Body.Close() + if resp.StatusCode == http.StatusNoContent { + return nil + } + if resp.StatusCode != http.StatusOK { + return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName()) + } + if ptr == nil { + return nil + } + err = json.NewDecoder(resp.Body).Decode(ptr) + if err != nil { + return errors.Wrap(err, getCallerFuncName()) + } + if reflect.ValueOf(ptr).Elem().Kind() == reflect.Slice { + return nil + } + respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0))))) + if respbase.C != 0 { + return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName()) + } + return nil +} + // DeleteOpenAPI 向 ep 发送 DELETE 请求 func (bot *Bot) DeleteOpenAPI(ep string, body io.Reader) error { req, err := NewHTTPEndpointDeleteRequestWithAuth(ep, bot.Authorization(), body) diff --git a/openapi_codegen_putopenapiof.go b/openapi_codegen_putopenapiof.go new file mode 100644 index 0000000..026131c --- /dev/null +++ b/openapi_codegen_putopenapiof.go @@ -0,0 +1,23 @@ +// Code generated by codegen/putopenapiof. DO NOT EDIT. + +package nano + +import ( + "io" + "unsafe" + + "github.com/pkg/errors" +) + +func (bot *Bot) putOpenAPIofGuildRoleChannelID(ep string, body io.Reader) (*GuildRoleChannelID, error) { + resp := &struct { + CodeMessageBase + GuildRoleChannelID + }{} + err := bot.PutOpenAPI(ep, resp, body) + if err != nil { + err = errors.Wrap(err, getCallerFuncName()) + return nil, err + } + return (*GuildRoleChannelID)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil +} diff --git a/openapi_role.go b/openapi_role.go index 28cf92c..e3c16b4 100644 --- a/openapi_role.go +++ b/openapi_role.go @@ -1,5 +1,21 @@ 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 @@ -72,3 +88,54 @@ func (bot *Bot) PatchGuildRole(guildid, roleid string, name string, color uint32 func (bot *Bot) DeleteGuildRole(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"` +} + +// AddRoleToMember 将频道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) AddRoleToMember(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 +} + +// RemoveRoleFromMember 将用户 user_id 从 频道 guild_id 的 role_id 身份组中移除 +// +// https://bot.q.qq.com/wiki/develop/api/openapi/guild/delete_guild_member_role.html +func (bot *Bot) RemoveRoleFromMember(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) +}