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

finishi audio & optimize http

This commit is contained in:
源文雨
2023-10-14 13:15:38 +09:00
parent 215cc6ac82
commit 5a088daf11
4 changed files with 85 additions and 125 deletions

View File

@@ -28,4 +28,19 @@ type Handler struct {
OnMessageDelete func(s int, bot *Bot, d *Message)
// GUILD_MESSAGE_REACTIONS (1 << 10)
OnMessageReactionAdd func(s int, bot *Bot, d *MessageReaction)
OnMessageReactionRemove func(s int, bot *Bot, d *MessageReaction)
// DIRECT_MESSAGE (1 << 12)
OnDirectMessageCreate func(s int, bot *Bot, d *Message)
OnDirectMessageDelete func(s int, bot *Bot, d *Message)
// OPEN_FORUMS_EVENT (1 << 18) // 论坛事件, 此为公域的论坛事件
OnOpenForumThreadCreate func(s int, bot *Bot, d *Thread)
OnOpenForumThreadUpdate func(s int, bot *Bot, d *Thread)
OnOpenForumThreadDelete func(s int, bot *Bot, d *Thread)
OnOpenForumPostCreate func(s int, bot *Bot, d *Post)
OnOpenForumPostDelete func(s int, bot *Bot, d *Post)
OnOpenForumReplyCreate func(s int, bot *Bot, d *Reply)
OnOpenForumReplyDelete func(s int, bot *Bot, d *Reply)
}

View File

@@ -16,6 +16,9 @@ import (
base14 "github.com/fumiama/go-base16384"
)
// HTTPRequsetConstructer ...
type HTTPRequsetConstructer func(ep string, contenttype string, auth string, body io.Reader) (*http.Request, error)
func newHTTPEndpointRequestWithAuth(method, contenttype, ep string, auth string, body io.Reader) (req *http.Request, err error) {
req, err = http.NewRequest(method, OpenAPI+ep, body)
if err != nil {

View File

@@ -19,11 +19,8 @@ func checkrespbaseunsafe(ptr any) error {
return nil
}
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel Member RoleMembers GuildRoleList ChannelPermissions Message MessageSetting PinsMessage Schedule MessageReactionUsers
// GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error {
req, err := NewHTTPEndpointGetRequestWithAuth(ep, contenttype, bot.Authorization(), nil)
func (bot *Bot) dohttprequest(constructer HTTPRequsetConstructer, ep, contenttype string, ptr any, body io.Reader) error {
req, err := constructer(ep, contenttype, bot.Authorization(), body)
if err != nil {
return errors.Wrap(err, getCallerFuncName())
}
@@ -51,148 +48,45 @@ func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error {
return checkrespbaseunsafe(ptr)
}
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel Member RoleMembers GuildRoleList ChannelPermissions Message MessageSetting PinsMessage Schedule MessageReactionUsers
// GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) GetOpenAPI(ep, contenttype string, ptr any) error {
return bot.dohttprequest(NewHTTPEndpointGetRequestWithAuth, ep, contenttype, ptr, nil)
}
// GetOpenAPIWithBody 不规范地从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) GetOpenAPIWithBody(ep, contenttype string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointGetRequestWithAuth(ep, contenttype, 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.StatusBadRequest {
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
}
return checkrespbaseunsafe(ptr)
return bot.dohttprequest(NewHTTPEndpointGetRequestWithAuth, ep, contenttype, ptr, body)
}
//go:generate go run codegen/putopenapiof/main.go GuildRoleChannelID PinsMessage
// PutOpenAPI 向 ep 发送 PUT 并获取 json 结构化数据返回写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PutOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointPutRequestWithAuth(ep, contenttype, 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.StatusBadRequest {
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
}
return checkrespbaseunsafe(ptr)
return bot.dohttprequest(NewHTTPEndpointPutRequestWithAuth, ep, contenttype, ptr, body)
}
// DeleteOpenAPI 向 ep 发送 DELETE 请求
func (bot *Bot) DeleteOpenAPI(ep, contenttype string, body io.Reader) error {
req, err := NewHTTPEndpointDeleteRequestWithAuth(ep, contenttype, 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.StatusBadRequest {
return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
}
return nil
return bot.dohttprequest(NewHTTPEndpointDeleteRequestWithAuth, ep, contenttype, nil, body)
}
// DeleteOpenAPIWithPtr 带返回值地向 ep 发送 DELETE 请求
func (bot *Bot) DeleteOpenAPIWithPtr(ep, contenttype string, ptr any, body io.Reader) error {
return bot.dohttprequest(NewHTTPEndpointDeleteRequestWithAuth, ep, contenttype, ptr, body)
}
//go:generate go run codegen/postopenapiof/main.go Channel GuildRoleCreate Message DMS
// PostOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PostOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointPostRequestWithAuth(ep, contenttype, 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.StatusBadRequest {
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
}
return checkrespbaseunsafe(ptr)
return bot.dohttprequest(NewHTTPEndpointPostRequestWithAuth, ep, contenttype, ptr, body)
}
//go:generate go run codegen/patchopenapiof/main.go Channel GuildRolePatch
// PatchOpenAPI 从 ep 得到 json 结构化数据返回值写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
func (bot *Bot) PatchOpenAPI(ep, contenttype string, ptr any, body io.Reader) error {
req, err := NewHTTPEndpointPatchRequestWithAuth(ep, contenttype, 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.StatusBadRequest {
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
}
return checkrespbaseunsafe(ptr)
return bot.dohttprequest(NewHTTPEndpointPatchRequestWithAuth, ep, contenttype, ptr, body)
}

View File

@@ -1,3 +1,51 @@
package nano
// AudioAction 音频事件
//
// https://bot.q.qq.com/wiki/develop/api/openapi/audio/model.html
type AudioAction struct {
ChannelID string `json:"channel_id"`
GuildID string `json:"guild_id"`
AudioURL string `json:"audio_url"`
Text string `json:"text"`
}
// AudioControlStatus https://bot.q.qq.com/wiki/develop/api/openapi/audio/model.html#status
type AudioControlStatus int
const (
AudioControlStatusStart AudioControlStatus = iota
AudioControlStatusPause
AudioControlStatusResume
AudioControlStatusStop
)
// AudioControl 控制子频道 channel_id 下的音频
//
// https://bot.q.qq.com/wiki/develop/api/openapi/audio/audio_control.html
type AudioControl struct {
AudioURL string `json:"audio_url"`
Text string `json:"text"`
Status AudioControlStatus `json:"status"`
}
// ControlAudioInChannel 控制子频道 channel_id 下的音频
//
// https://bot.q.qq.com/wiki/develop/api/openapi/audio/audio_control.html
func (bot *Bot) ControlAudioInChannel(id string, control *AudioControl) error {
return bot.PostOpenAPI("/channels/"+id+"/audio", "", &CodeMessageBase{}, WriteBodyFromJSON(control))
}
// OpenMic 机器人在 channel_id 对应的语音子频道上麦
//
// https://bot.q.qq.com/wiki/develop/api/openapi/audio/put_mic.html
func (bot *Bot) OpenMicInChannel(id string) error {
return bot.PutOpenAPI("/channels/"+id+"/mic", "", &CodeMessageBase{}, nil)
}
// CloseMicInChannel 机器人在 channel_id 对应的语音子频道下麦
//
// https://bot.q.qq.com/wiki/develop/api/openapi/audio/delete_mic.html
func (bot *Bot) CloseMicInChannel(id string) error {
return bot.DeleteOpenAPI("/channels/"+id+"/mic", "", nil)
}