1
0
mirror of https://github.com/fumiama/NanoBot.git synced 2026-06-07 19:50:24 +08:00

finish role

This commit is contained in:
源文雨
2023-10-11 00:16:44 +09:00
parent 50939b98aa
commit 4745fa990e
5 changed files with 187 additions and 0 deletions

View File

@@ -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)