diff --git a/coolq/api.go b/coolq/api.go index f63c4e8..ffc94f3 100644 --- a/coolq/api.go +++ b/coolq/api.go @@ -5,6 +5,7 @@ import ( "encoding/hex" "encoding/json" "fmt" + "github.com/segmentio/asm/base64" "math" "os" "path" @@ -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 @@ -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) diff --git a/coolq/bot.go b/coolq/bot.go index 384697c..97dc448 100644 --- a/coolq/bot.go +++ b/coolq/bot.go @@ -34,6 +34,7 @@ type CQBot struct { friendReqCache sync.Map tempSessionCache sync.Map + nextTokenCache *utils.Cache } // Event 事件 @@ -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) diff --git a/coolq/converter.go b/coolq/converter.go index bd53eb2..e025384 100644 --- a/coolq/converter.go +++ b/coolq/converter.go @@ -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 { diff --git a/go.mod b/go.mod index 0d5410f..ebcd477 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 9ea0cba..15b2bbb 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/modules/api/api.go b/modules/api/api.go index b0a047b..490f2b1 100644 --- a/modules/api/api.go +++ b/modules/api/api.go @@ -140,6 +140,10 @@ func (c *Caller) call(action string, p Getter) global.MSG { return c.bot.CQGetGuildChannelList(p0, p1) case "get_guild_list": return c.bot.CQGetGuildList() + case "get_guild_member_list": + p0 := p.Get("guild_id").Uint() + p1 := p.Get("next_token").String() + return c.bot.CQGetGuildMembers(p0, p1) case "get_guild_meta_by_guest": p0 := p.Get("guild_id").Uint() return c.bot.CQGetGuildMetaByGuest(p0)