mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-05 10:40:24 +08:00
finish role
This commit is contained in:
51
codegen/putopenapiof/main.go
Normal file
51
codegen/putopenapiof/main.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
10
http.go
10
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)
|
||||
|
||||
36
openapi.go
36
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)
|
||||
|
||||
23
openapi_codegen_putopenapiof.go
Normal file
23
openapi_codegen_putopenapiof.go
Normal file
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user