1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-05 02:30:23 +08:00

fix array

This commit is contained in:
源文雨
2023-10-10 22:32:02 +09:00
parent 30554f4ffc
commit e8fd4c0c54
6 changed files with 41 additions and 41 deletions

2
.gitignore vendored
View File

@@ -19,3 +19,5 @@
# Go workspace file
go.work
bot_test.go

View File

@@ -4,15 +4,16 @@ import (
"encoding/json"
"io"
"net/http"
"reflect"
"strconv"
"unsafe"
"github.com/pkg/errors"
)
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild GuildArray Channel ChannelArray
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel
// GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 必须在开头继承 CodeMessageBase
// GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
req, err := NewHTTPEndpointGetRequestWithAuth(ep, bot.Authorization())
if err != nil {
@@ -23,10 +24,19 @@ func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
return errors.Wrap(err, getCallerFuncName())
}
defer resp.Body.Close()
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())
@@ -53,7 +63,7 @@ func (bot *Bot) DeleteOpenAPI(ep string) error {
//go:generate go run codegen/postopenapiof/main.go Channel
// PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 必须在开头继承 CodeMessageBase
// PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PostOpenAPI(ep string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointPostRequestWithAuth(ep, bot.Authorization(), body)
if err != nil {
@@ -64,10 +74,19 @@ func (bot *Bot) PostOpenAPI(ep string, ptr any, body io.Reader) error {
return errors.Wrap(err, getCallerFuncName())
}
defer resp.Body.Close()
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())
@@ -77,7 +96,7 @@ func (bot *Bot) PostOpenAPI(ep string, ptr any, body io.Reader) error {
//go:generate go run codegen/patchopenapiof/main.go Channel
// PatchOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 必须在开头继承 CodeMessageBase
// PatchOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PatchOpenAPI(ep string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointPatchRequestWithAuth(ep, bot.Authorization(), body)
if err != nil {
@@ -88,10 +107,19 @@ func (bot *Bot) PatchOpenAPI(ep string, ptr any, body io.Reader) error {
return errors.Wrap(err, getCallerFuncName())
}
defer resp.Body.Close()
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())

View File

@@ -60,14 +60,12 @@ type Channel struct {
Permissions string `json:"permissions"`
}
// ChannelArray []Channel 的别名
type ChannelArray []Channel
// GetChannelsOfGuild 获取 guild_id 指定的频道下的子频道列表
//
// https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channels.html
func (bot *Bot) GetChannelsOfGuild(id string) (*ChannelArray, error) {
return bot.getOpenAPIofChannelArray("/guilds/" + id + "/channels")
func (bot *Bot) GetChannelsOfGuild(id string) (channels []Channel, err error) {
err = bot.GetOpenAPI("/guilds/"+id+"/channels", &channels)
return
}
// GetChannelByID 用于获取 channel_id 指定的子频道的详情

View File

@@ -47,19 +47,6 @@ func (bot *Bot) getOpenAPIofGuild(ep string) (*Guild, error) {
return (*Guild)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
}
func (bot *Bot) getOpenAPIofGuildArray(ep string) (*GuildArray, error) {
resp := &struct {
CodeMessageBase
GuildArray
}{}
err := bot.GetOpenAPI(ep, resp)
if err != nil {
err = errors.Wrap(err, getCallerFuncName())
return nil, err
}
return (*GuildArray)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
}
func (bot *Bot) getOpenAPIofChannel(ep string) (*Channel, error) {
resp := &struct {
CodeMessageBase
@@ -72,16 +59,3 @@ func (bot *Bot) getOpenAPIofChannel(ep string) (*Channel, error) {
}
return (*Channel)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
}
func (bot *Bot) getOpenAPIofChannelArray(ep string) (*ChannelArray, error) {
resp := &struct {
CodeMessageBase
ChannelArray
}{}
err := bot.GetOpenAPI(ep, resp)
if err != nil {
err = errors.Wrap(err, getCallerFuncName())
return nil, err
}
return (*ChannelArray)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
}

View File

@@ -17,9 +17,6 @@ type Guild struct {
Description string `json:"description"`
}
// GuildArray []Guild 的别名
type GuildArray []Guild
// GetGuildByID 获取 guild_id 指定的频道的详情
//
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild.html

View File

@@ -22,10 +22,11 @@ func (bot *Bot) GetMe() (*User, error) {
// GetMyGuilds 获取当前用户(机器人)频道列表,支持分页
//
// https://bot.q.qq.com/wiki/develop/api/openapi/user/guilds.html
func (bot *Bot) GetMyGuilds(before, after string, limit int) (*GuildArray, error) {
return bot.getOpenAPIofGuildArray(WriteHTTPQueryIfNotNil("/users/@me/guilds",
func (bot *Bot) GetMyGuilds(before, after string, limit int) (guilds []Guild, err error) {
err = bot.GetOpenAPI(WriteHTTPQueryIfNotNil("/users/@me/guilds",
"before", before,
"after", after,
"limit", limit,
))
), &guilds)
return
}