Skip to content

Commit

Permalink
feat: get_guild_member_list api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrs4s committed Dec 8, 2021
1 parent 7278f99 commit e69051e
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
56 changes: 51 additions & 5 deletions coolq/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/segmentio/asm/base64"
"math"
"os"
"path"
Expand All @@ -29,6 +30,19 @@ import (
"github.com/Mrs4s/go-cqhttp/modules/filter"
)

type guildMemberPageToken struct {
guildID uint64
nextIndex uint32
nextRoleID uint64
nextQueryParam string
}

var defaultPageToken = &guildMemberPageToken{
guildID: 0,
nextIndex: 0,
nextRoleID: 2,
}

// CQGetLoginInfo 获取登录号信息
//
// https://git.io/Jtz1I
Expand Down Expand Up @@ -128,17 +142,49 @@ func (bot *CQBot) CQGetGuildChannelList(guildID uint64, noCache bool) global.MSG
return OK(channels)
}

/*
// CQGetGuildMembers 获取频道成员列表
// @route(get_guild_members)
func (bot *CQBot) CQGetGuildMembers(guildID uint64) global.MSG {
// @route(get_guild_member_list)
func (bot *CQBot) CQGetGuildMembers(guildID uint64, nextToken string) global.MSG {
guild := bot.Client.GuildService.FindGuild(guildID)
if guild == nil {
return Failed(100, "GUILD_NOT_FOUND")
}
return OK(nil) // todo
token := defaultPageToken
if nextToken != "" {
i, exists := bot.nextTokenCache.Get(nextToken)
if !exists {
return Failed(100, "NEXT_TOKEN_NOT_EXISTS")
}
token = i.(*guildMemberPageToken)
if token.guildID != guildID {
return Failed(100, "GUILD_NOT_MATCH")
}
}
ret, err := bot.Client.GuildService.FetchGuildMemberListWithRole(guildID, 0, token.nextIndex, token.nextRoleID, token.nextQueryParam)
if err != nil {
return Failed(100, "API_ERROR", err.Error())
}
res := global.MSG{
"members": convertGuildMemberInfo(ret.Members),
"finished": ret.Finished,
"next_token": nil,
}
if !ret.Finished {
next := &guildMemberPageToken{
guildID: guildID,
nextIndex: ret.NextIndex,
nextRoleID: ret.NextRoleId,
nextQueryParam: ret.NextQueryParam,
}
id := base64.StdEncoding.EncodeToString(binary.NewWriterF(func(w *binary.Writer) {
w.WriteUInt64(uint64(time.Now().UnixNano()))
w.WriteString(utils.RandomString(5))
}))
bot.nextTokenCache.Add(id, next, time.Minute*10)
res["next_token"] = id
}
return OK(res)
}
*/

// CQGetGuildRoles 获取频道角色列表
// @route(get_guild_roles)
Expand Down
4 changes: 3 additions & 1 deletion coolq/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type CQBot struct {

friendReqCache sync.Map
tempSessionCache sync.Map
nextTokenCache *utils.Cache
}

// Event 事件
Expand Down Expand Up @@ -67,7 +68,8 @@ func (e *Event) JSONString() string {
// NewQQBot 初始化一个QQBot实例
func NewQQBot(cli *client.QQClient) *CQBot {
bot := &CQBot{
Client: cli,
Client: cli,
nextTokenCache: utils.NewCache(time.Second * 10),
}
bot.Client.OnPrivateMessage(bot.privateMessageEvent)
bot.Client.OnGroupMessage(bot.groupMessageEvent)
Expand Down
16 changes: 10 additions & 6 deletions coolq/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ func convertGroupMemberInfo(groupID int64, m *client.GroupMemberInfo) global.MSG
}
}

func convertGuildMemberInfo(m *client.GuildMemberInfo) global.MSG {
return global.MSG{
"tiny_id": fU64(m.TinyId),
"title": m.Title,
"nickname": m.Nickname,
"role": m.Role,
func convertGuildMemberInfo(m []*client.GuildMemberInfo) (r []global.MSG) {
for _, mem := range m {
r = append(r, global.MSG{
"tiny_id": fU64(mem.TinyId),
"title": mem.Title,
"nickname": mem.Nickname,
"role_id": fU64(mem.Role),
"role_name": mem.RoleName,
})
}
return
}

func (bot *CQBot) formatGroupMessage(m *message.GroupMessage) global.MSG {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.17
require (
github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f
github.com/Microsoft/go-winio v0.5.1
github.com/Mrs4s/MiraiGo v0.0.0-20211208080234-25c67a3ee1c1
github.com/Mrs4s/MiraiGo v0.0.0-20211208132533-8cd25e02fcf3
github.com/dustin/go-humanize v1.0.0
github.com/fumiama/go-hide-param v0.1.4
github.com/gabriel-vasile/mimetype v1.4.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f/g
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY=
github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84=
github.com/Mrs4s/MiraiGo v0.0.0-20211208080234-25c67a3ee1c1 h1:UipCzEST10GzJnvlhHsY4g39xzwVzSHE+5Go9d0dTPY=
github.com/Mrs4s/MiraiGo v0.0.0-20211208080234-25c67a3ee1c1/go.mod h1:YD9gBKkxC9lPPtx3doYXRG26VBkK6YXjrS76cv01C5w=
github.com/Mrs4s/MiraiGo v0.0.0-20211208132533-8cd25e02fcf3 h1:w42Z7gE+HjBcjXDjVZ8o5rzNgAzt7nhuL84HOYpIhRE=
github.com/Mrs4s/MiraiGo v0.0.0-20211208132533-8cd25e02fcf3/go.mod h1:YD9gBKkxC9lPPtx3doYXRG26VBkK6YXjrS76cv01C5w=
github.com/RomiChan/protobuf v0.0.0-20211204042931-ff4f35848737 h1:p4o7/eSoP39jwnGZz08N1IpH/mNzg9SdCn7kPM9A9BE=
github.com/RomiChan/protobuf v0.0.0-20211204042931-ff4f35848737/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE=
github.com/bits-and-blooms/bitset v1.2.1 h1:M+/hrU9xlMp7t4TyTDQW97d3tRPVuKFC6zBEK16QnXY=
Expand Down
4 changes: 4 additions & 0 deletions modules/api/api.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e69051e

Please sign in to comment.