mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-10 13:10:26 +08:00
fix array
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -19,3 +19,5 @@
|
|||||||
|
|
||||||
# Go workspace file
|
# Go workspace file
|
||||||
go.work
|
go.work
|
||||||
|
|
||||||
|
bot_test.go
|
||||||
|
|||||||
36
openapi.go
36
openapi.go
@@ -4,15 +4,16 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"reflect"
|
||||||
"strconv"
|
"strconv"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"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 {
|
func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
|
||||||
req, err := NewHTTPEndpointGetRequestWithAuth(ep, bot.Authorization())
|
req, err := NewHTTPEndpointGetRequestWithAuth(ep, bot.Authorization())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -23,10 +24,19 @@ func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
|
|||||||
return errors.Wrap(err, getCallerFuncName())
|
return errors.Wrap(err, getCallerFuncName())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
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)
|
err = json.NewDecoder(resp.Body).Decode(ptr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, getCallerFuncName())
|
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)))))
|
respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0)))))
|
||||||
if respbase.C != 0 {
|
if respbase.C != 0 {
|
||||||
return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName())
|
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
|
//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 {
|
func (bot *Bot) PostOpenAPI(ep string, ptr any, body io.Reader) error {
|
||||||
req, err := NewHTTPEndpointPostRequestWithAuth(ep, bot.Authorization(), body)
|
req, err := NewHTTPEndpointPostRequestWithAuth(ep, bot.Authorization(), body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -64,10 +74,19 @@ func (bot *Bot) PostOpenAPI(ep string, ptr any, body io.Reader) error {
|
|||||||
return errors.Wrap(err, getCallerFuncName())
|
return errors.Wrap(err, getCallerFuncName())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
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)
|
err = json.NewDecoder(resp.Body).Decode(ptr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, getCallerFuncName())
|
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)))))
|
respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0)))))
|
||||||
if respbase.C != 0 {
|
if respbase.C != 0 {
|
||||||
return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName())
|
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
|
//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 {
|
func (bot *Bot) PatchOpenAPI(ep string, ptr any, body io.Reader) error {
|
||||||
req, err := NewHTTPEndpointPatchRequestWithAuth(ep, bot.Authorization(), body)
|
req, err := NewHTTPEndpointPatchRequestWithAuth(ep, bot.Authorization(), body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -88,10 +107,19 @@ func (bot *Bot) PatchOpenAPI(ep string, ptr any, body io.Reader) error {
|
|||||||
return errors.Wrap(err, getCallerFuncName())
|
return errors.Wrap(err, getCallerFuncName())
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
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)
|
err = json.NewDecoder(resp.Body).Decode(ptr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, getCallerFuncName())
|
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)))))
|
respbase := (*CodeMessageBase)(*(*unsafe.Pointer)(unsafe.Add(unsafe.Pointer(&ptr), unsafe.Sizeof(uintptr(0)))))
|
||||||
if respbase.C != 0 {
|
if respbase.C != 0 {
|
||||||
return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName())
|
return errors.Wrap(errors.New("code: "+strconv.Itoa(respbase.C)+", msg: "+respbase.M), getCallerFuncName())
|
||||||
|
|||||||
@@ -60,14 +60,12 @@ type Channel struct {
|
|||||||
Permissions string `json:"permissions"`
|
Permissions string `json:"permissions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// ChannelArray []Channel 的别名
|
|
||||||
type ChannelArray []Channel
|
|
||||||
|
|
||||||
// GetChannelsOfGuild 获取 guild_id 指定的频道下的子频道列表
|
// GetChannelsOfGuild 获取 guild_id 指定的频道下的子频道列表
|
||||||
//
|
//
|
||||||
// https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channels.html
|
// https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channels.html
|
||||||
func (bot *Bot) GetChannelsOfGuild(id string) (*ChannelArray, error) {
|
func (bot *Bot) GetChannelsOfGuild(id string) (channels []Channel, err error) {
|
||||||
return bot.getOpenAPIofChannelArray("/guilds/" + id + "/channels")
|
err = bot.GetOpenAPI("/guilds/"+id+"/channels", &channels)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChannelByID 用于获取 channel_id 指定的子频道的详情
|
// GetChannelByID 用于获取 channel_id 指定的子频道的详情
|
||||||
|
|||||||
@@ -47,19 +47,6 @@ func (bot *Bot) getOpenAPIofGuild(ep string) (*Guild, error) {
|
|||||||
return (*Guild)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
|
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) {
|
func (bot *Bot) getOpenAPIofChannel(ep string) (*Channel, error) {
|
||||||
resp := &struct {
|
resp := &struct {
|
||||||
CodeMessageBase
|
CodeMessageBase
|
||||||
@@ -72,16 +59,3 @@ func (bot *Bot) getOpenAPIofChannel(ep string) (*Channel, error) {
|
|||||||
}
|
}
|
||||||
return (*Channel)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
|
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -17,9 +17,6 @@ type Guild struct {
|
|||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// GuildArray []Guild 的别名
|
|
||||||
type GuildArray []Guild
|
|
||||||
|
|
||||||
// GetGuildByID 获取 guild_id 指定的频道的详情
|
// GetGuildByID 获取 guild_id 指定的频道的详情
|
||||||
//
|
//
|
||||||
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild.html
|
// https://bot.q.qq.com/wiki/develop/api/openapi/guild/get_guild.html
|
||||||
|
|||||||
@@ -22,10 +22,11 @@ func (bot *Bot) GetMe() (*User, error) {
|
|||||||
// GetMyGuilds 获取当前用户(机器人)频道列表,支持分页
|
// GetMyGuilds 获取当前用户(机器人)频道列表,支持分页
|
||||||
//
|
//
|
||||||
// https://bot.q.qq.com/wiki/develop/api/openapi/user/guilds.html
|
// https://bot.q.qq.com/wiki/develop/api/openapi/user/guilds.html
|
||||||
func (bot *Bot) GetMyGuilds(before, after string, limit int) (*GuildArray, error) {
|
func (bot *Bot) GetMyGuilds(before, after string, limit int) (guilds []Guild, err error) {
|
||||||
return bot.getOpenAPIofGuildArray(WriteHTTPQueryIfNotNil("/users/@me/guilds",
|
err = bot.GetOpenAPI(WriteHTTPQueryIfNotNil("/users/@me/guilds",
|
||||||
"before", before,
|
"before", before,
|
||||||
"after", after,
|
"after", after,
|
||||||
"limit", limit,
|
"limit", limit,
|
||||||
))
|
), &guilds)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user