diff --git a/dto/pager.go b/dto/pager.go index 05dfd47..e23c2c1 100644 --- a/dto/pager.go +++ b/dto/pager.go @@ -24,6 +24,24 @@ func (g *GuildMembersPager) QueryParams() map[string]string { return query } +// GuildRoleMembersPager 分页器 +type GuildRoleMembersPager struct { + StartIndex string `json:"start_index"` + Limit string `json:"limit"` +} + +// QueryParams 转换为 query 参数 +func (g *GuildRoleMembersPager) QueryParams() map[string]string { + query := make(map[string]string) + if g.Limit != "" { + query["limit"] = g.Limit + } + if g.StartIndex != "" { + query["start_index"] = g.StartIndex + } + return query +} + // GuildPager 分页器 type GuildPager struct { Before string `json:"before"` // 读此id之前的数据 diff --git a/openapi/iface.go b/openapi/iface.go index d0434f9..f7a0ce7 100644 --- a/openapi/iface.go +++ b/openapi/iface.go @@ -79,6 +79,8 @@ type GuildAPI interface { Guild(ctx context.Context, guildID string) (*dto.Guild, error) GuildMember(ctx context.Context, guildID, userID string) (*dto.Member, error) GuildMembers(ctx context.Context, guildID string, pager *dto.GuildMembersPager) ([]*dto.Member, error) + GuildRoleMembers(ctx context.Context, guildID string, roleID string, pager *dto.GuildRoleMembersPager) ( + []*dto.Member, string, error) DeleteGuildMember(ctx context.Context, guildID, userID string, opts ...dto.MemberDeleteOption) error // 频道禁言 GuildMute(ctx context.Context, guildID string, mute *dto.UpdateGuildMute) error diff --git a/openapi/v1/member.go b/openapi/v1/member.go index c916299..c45f5f8 100644 --- a/openapi/v1/member.go +++ b/openapi/v1/member.go @@ -80,6 +80,34 @@ func (o *openAPI) GuildMembers( return members, nil } +// GuildRoleMembers 分页拉取频道内身份组成员列表 +func (o *openAPI) GuildRoleMembers( + ctx context.Context, guildID string, roleID string, pager *dto.GuildRoleMembersPager, +) ([]*dto.Member, string, error) { + if pager == nil { + return nil, "", errs.ErrPagerIsNil + } + resp, err := o.request(ctx). + SetPathParam("guild_id", guildID). + SetPathParam("role_id", roleID). + SetQueryParams(pager.QueryParams()). + Get(o.getURL(guildRoleMemberURI)) + if err != nil { + return nil, "", err + } + + type res struct { + Data []*dto.Member `json:"data"` + Next string `json:"next"` + } + var roleMembersRsp res + if err := json.Unmarshal(resp.Body(), &roleMembersRsp); err != nil { + return nil, "", err + } + + return roleMembersRsp.Data, roleMembersRsp.Next, nil +} + // DeleteGuildMember 将指定成员踢出频道 func (o *openAPI) DeleteGuildMember(ctx context.Context, guildID, userID string, opts ...dto.MemberDeleteOption) error { opt := &dto.MemberDeleteOpts{} diff --git a/openapi/v1/resource.go b/openapi/v1/resource.go index e85f2b0..55115d8 100644 --- a/openapi/v1/resource.go +++ b/openapi/v1/resource.go @@ -16,6 +16,7 @@ const ( guildURI uri = "/guilds/{guild_id}" guildMembersURI uri = "/guilds/{guild_id}/members" guildMemberURI uri = "/guilds/{guild_id}/members/{user_id}" + guildRoleMemberURI uri = "/guilds/{guild_id}/roles/{role_id}/members" guildMuteURI uri = "/guilds/{guild_id}/mute" // 频道禁言 guildMembersMuteURI uri = "/guilds/{guild_id}/members/{user_id}/mute" // 频道指定成员禁言