mirror of
https://github.com/fumiama/NanoBot.git
synced 2026-06-05 02:30:23 +08:00
fix delete
This commit is contained in:
2
bot.go
2
bot.go
@@ -6,7 +6,7 @@ import "time"
|
||||
type Bot struct {
|
||||
AppID string // AppID is BotAppID(开发者ID)
|
||||
Token string // Token is 机器人令牌
|
||||
Key string // Key is 机器人密钥
|
||||
Secret string // Secret is 机器人密钥
|
||||
Timeout time.Duration // Timeout is API 调用超时
|
||||
}
|
||||
|
||||
|
||||
4
http.go
4
http.go
@@ -22,8 +22,8 @@ func NewHTTPEndpointGetRequestWithAuth(ep string, auth string) (req *http.Reques
|
||||
}
|
||||
|
||||
// NewHTTPEndpointDeleteRequestWithAuth 新建带鉴权头的 HTTP DELETE 请求
|
||||
func NewHTTPEndpointDeleteRequestWithAuth(ep string, auth string) (req *http.Request, err error) {
|
||||
req, err = http.NewRequest("DELETE", StandardAPI+ep, nil)
|
||||
func NewHTTPEndpointDeleteRequestWithAuth(ep string, auth string, body io.Reader) (req *http.Request, err error) {
|
||||
req, err = http.NewRequest("DELETE", StandardAPI+ep, body)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
18
openapi.go
18
openapi.go
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel
|
||||
//go:generate go run codegen/getopenapiof/main.go ShardWSSGateway User Guild Channel Member RoleMembers
|
||||
|
||||
// GetOpenAPI 从 ep 获取 json 结构化数据写到 ptr, ptr 除 Slice 外必须在开头继承 CodeMessageBase
|
||||
func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
|
||||
@@ -24,6 +24,9 @@ func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
|
||||
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())
|
||||
}
|
||||
@@ -45,8 +48,8 @@ func (bot *Bot) GetOpenAPI(ep string, ptr any) error {
|
||||
}
|
||||
|
||||
// DeleteOpenAPI 向 ep 发送 DELETE 请求
|
||||
func (bot *Bot) DeleteOpenAPI(ep string) error {
|
||||
req, err := NewHTTPEndpointDeleteRequestWithAuth(ep, bot.Authorization())
|
||||
func (bot *Bot) DeleteOpenAPI(ep string, body io.Reader) error {
|
||||
req, err := NewHTTPEndpointDeleteRequestWithAuth(ep, bot.Authorization(), body)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, getCallerFuncName())
|
||||
}
|
||||
@@ -55,6 +58,9 @@ func (bot *Bot) DeleteOpenAPI(ep string) error {
|
||||
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())
|
||||
}
|
||||
@@ -74,6 +80,9 @@ 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.StatusNoContent {
|
||||
return nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||
}
|
||||
@@ -107,6 +116,9 @@ 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.StatusNoContent {
|
||||
return nil
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errors.Wrap(errors.New("code: "+strconv.Itoa(resp.StatusCode)+", msg: "+resp.Status), getCallerFuncName())
|
||||
}
|
||||
|
||||
@@ -120,7 +120,7 @@ func (bot *Bot) PatchChannel(id string, config *ChannelPatch) (*Channel, error)
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/channel/delete_channel.html
|
||||
func (bot *Bot) DeleteChannel(id string) error {
|
||||
return bot.DeleteOpenAPI("/channels/" + id)
|
||||
return bot.DeleteOpenAPI("/channels/"+id, nil)
|
||||
}
|
||||
|
||||
// GetOnlineNumsInChannel 查询音视频/直播子频道 channel_id 的在线成员数
|
||||
|
||||
@@ -59,3 +59,29 @@ func (bot *Bot) getOpenAPIofChannel(ep string) (*Channel, error) {
|
||||
}
|
||||
return (*Channel)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
|
||||
}
|
||||
|
||||
func (bot *Bot) getOpenAPIofMember(ep string) (*Member, error) {
|
||||
resp := &struct {
|
||||
CodeMessageBase
|
||||
Member
|
||||
}{}
|
||||
err := bot.GetOpenAPI(ep, resp)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, getCallerFuncName())
|
||||
return nil, err
|
||||
}
|
||||
return (*Member)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
|
||||
}
|
||||
|
||||
func (bot *Bot) getOpenAPIofRoleMembers(ep string) (*RoleMembers, error) {
|
||||
resp := &struct {
|
||||
CodeMessageBase
|
||||
RoleMembers
|
||||
}{}
|
||||
err := bot.GetOpenAPI(ep, resp)
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, getCallerFuncName())
|
||||
return nil, err
|
||||
}
|
||||
return (*RoleMembers)(unsafe.Add(unsafe.Pointer(resp), unsafe.Sizeof(CodeMessageBase{}))), nil
|
||||
}
|
||||
|
||||
65
openapi_member.go
Normal file
65
openapi_member.go
Normal file
@@ -0,0 +1,65 @@
|
||||
package nano
|
||||
|
||||
import "time"
|
||||
|
||||
// Member 成员对象 Member and MemberWithGuildID
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/member/model.html
|
||||
type Member struct {
|
||||
GuildID string `json:"guild_id"` // MemberWithGuildID only
|
||||
User User `json:"user"`
|
||||
Nick string `json:"nick"`
|
||||
Roles []string `json:"roles"`
|
||||
JoinedAt time.Time `json:"joined_at"`
|
||||
Deaf bool `json:"deaf"`
|
||||
Mute bool `json:"mute"`
|
||||
Pending bool `json:"pending"`
|
||||
}
|
||||
|
||||
// GetGuildMembers 获取 guild_id 指定的频道中所有成员的详情列表,支持分页
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_members.html
|
||||
func (bot *Bot) GetGuildMembers(id, after string, limit int) (members []Member, err error) {
|
||||
err = bot.GetOpenAPI(WriteHTTPQueryIfNotNil("/guilds/"+id+"/members",
|
||||
"after", after,
|
||||
"limit", limit,
|
||||
), &members)
|
||||
return
|
||||
}
|
||||
|
||||
// RoleMembers 频道身份组成员列表
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_role_members.html#%E8%BF%94%E5%9B%9E
|
||||
type RoleMembers struct {
|
||||
Data []Member `json:"data"`
|
||||
Next string `json:"next"`
|
||||
}
|
||||
|
||||
// GetRoleMembers 获取 guild_id 频道中指定role_id身份组下所有成员的详情列表,支持分页
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_role_members.html
|
||||
func (bot *Bot) GetRoleMembers(guildid, roleid, startindex string, limit int) (*RoleMembers, error) {
|
||||
return bot.getOpenAPIofRoleMembers(WriteHTTPQueryIfNotNil("/guilds/"+guildid+"/roles/"+roleid+"/members",
|
||||
"start_index", startindex,
|
||||
"limit", limit,
|
||||
))
|
||||
}
|
||||
|
||||
// GetGuildMember 获取 guild_id 指定的频道中 user_id 对应成员的详细信息
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/member/get_member.html
|
||||
func (bot *Bot) GetGuildMember(guildid, userid string) (*Member, error) {
|
||||
return bot.getOpenAPIofMember("/guilds/" + guildid + "/members/" + userid)
|
||||
}
|
||||
|
||||
// DeleteGuildMember 删除 guild_id 指定的频道下的成员 user_id
|
||||
//
|
||||
// https://bot.q.qq.com/wiki/develop/api/openapi/member/delete_member.html
|
||||
//
|
||||
// - delhistmsgdays: 消息撤回时间范围仅支持固定的天数:3,7,15,30。 特殊的时间范围:-1: 撤回全部消息。默认值为0不撤回任何消息。
|
||||
func (bot *Bot) DeleteGuildMember(guildid, userid string, addblklst bool, delhistmsgdays int) error {
|
||||
return bot.DeleteOpenAPI("/guilds/"+guildid+"/members/"+userid, WriteBodyFromJSON(&struct {
|
||||
A bool `json:"add_blacklist"`
|
||||
D int `json:"delete_history_msg_days"`
|
||||
}{addblklst, delhistmsgdays}))
|
||||
}
|
||||
Reference in New Issue
Block a user