github.com/fumiama/NanoBot@v0.0.0-20231122134259-c22d8183efca/openapi_channel.go (about)

     1  package nano
     2  
     3  // ChannelType https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html#channeltype
     4  type ChannelType int
     5  
     6  const (
     7  	ChannelTypeText        ChannelType    = iota // 文字子频道
     8  	ChannelTypeReserved1                         // 保留,不可用
     9  	ChannelTypeAudio                             // 语音子频道
    10  	ChannelTypeReserved2                         // 保留,不可用
    11  	ChannelTypeSubchannel                        // 子频道分组
    12  	ChannelTypeLive        = 10000 + iota        // 直播子频道
    13  	ChannelTypeApplication                       // 应用子频道
    14  	ChannelTypeForum                             // 论坛子频道
    15  )
    16  
    17  // ChannelSubType https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html#channelsubtype
    18  type ChannelSubType int
    19  
    20  const (
    21  	ChannelSubTypeChat     ChannelSubType = iota // 闲聊
    22  	ChannelSubTypeAnnounce                       // 公告
    23  	ChannelSubTypeKouryaku                       // 攻略
    24  	ChannelSubTypeGame                           // 开黑
    25  )
    26  
    27  // PrivateType https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html#privatetype
    28  type PrivateType int
    29  
    30  const (
    31  	PrivateTypePublic         PrivateType = iota // 公开频道
    32  	PrivateTypeOnlyAdmin                         // 群主管理员可见
    33  	PrivateTypeAdminAndShimei                    // 群主管理员+指定成员,可使用 修改子频道权限接口 指定成员
    34  )
    35  
    36  // SpeakPermission https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html#speakpermission
    37  type SpeakPermission int
    38  
    39  const (
    40  	SpeakPermissionInvalid        = iota // 无效类型
    41  	SpeakPermissionAll                   // 所有人
    42  	SpeakPermissionAdminAndShimei        // 群主管理员+指定成员,可使用 修改子频道权限接口 指定成员
    43  )
    44  
    45  // Channel 子频道对象
    46  //
    47  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html
    48  type Channel struct {
    49  	ID              string          `json:"id"`
    50  	GuildID         string          `json:"guild_id"`
    51  	Name            string          `json:"name"`
    52  	Type            ChannelType     `json:"type"`
    53  	SubType         ChannelSubType  `json:"sub_type"`
    54  	Position        int             `json:"position"`
    55  	ParentID        string          `json:"parent_id"`
    56  	OwnerID         string          `json:"owner_id"`
    57  	PrivateType     PrivateType     `json:"private_type"`
    58  	SpeakPermission SpeakPermission `json:"speak_permission"`
    59  	ApplicationID   string          `json:"application_id"` // ApplicationID see https://bot.q.qq.com/wiki/develop/api/openapi/channel/model.html#%E5%BA%94%E7%94%A8%E5%AD%90%E9%A2%91%E9%81%93%E7%9A%84%E5%BA%94%E7%94%A8%E7%B1%BB%E5%9E%8B
    60  	Permissions     string          `json:"permissions"`
    61  	OpUserID        string          `json:"op_user_id"` // https://bot.q.qq.com/wiki/develop/api/gateway/channel.html#%E5%86%85%E5%AE%B9
    62  }
    63  
    64  // GetChannelsOfGuild 获取 guild_id 指定的频道下的子频道列表
    65  //
    66  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channels.html
    67  func (bot *Bot) GetChannelsOfGuild(id string) (channels []Channel, err error) {
    68  	err = bot.GetOpenAPI("/guilds/"+id+"/channels", "", &channels)
    69  	return
    70  }
    71  
    72  // GetChannelByID 用于获取 channel_id 指定的子频道的详情
    73  //
    74  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_channel.html
    75  func (bot *Bot) GetChannelByID(id string) (*Channel, error) {
    76  	return bot.getOpenAPIofChannel("/channels/" + id)
    77  }
    78  
    79  // ChannelPost 子频道 post 操作所用对象
    80  //
    81  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/post_channels.html
    82  type ChannelPost struct {
    83  	Name            string          `json:"name"`
    84  	Type            ChannelType     `json:"type"`
    85  	SubType         ChannelSubType  `json:"sub_type"`
    86  	Position        int             `json:"position"`
    87  	ParentID        string          `json:"parent_id"`
    88  	OwnerID         string          `json:"owner_id,omitempty"`
    89  	PrivateType     PrivateType     `json:"private_type"`
    90  	PrivateUserIDs  []string        `json:"private_user_ids,omitempty"`
    91  	SpeakPermission SpeakPermission `json:"speak_permission,omitempty"`
    92  	ApplicationID   string          `json:"application_id,omitempty"`
    93  }
    94  
    95  // CreateChannelInGuild 用于在 guild_id 指定的频道下创建一个子频道
    96  //
    97  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/post_channels.html
    98  func (bot *Bot) CreateChannelInGuild(id string, config *ChannelPost) (*Channel, error) {
    99  	return bot.postOpenAPIofChannel("/guilds/"+id+"/channels", "", WriteBodyFromJSON(config))
   100  }
   101  
   102  // ChannelPatch 子频道 patch 操作所用对象
   103  //
   104  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/patch_channel.html
   105  type ChannelPatch struct {
   106  	Name            string           `json:"name,omitempty"`
   107  	Position        int              `json:"position,omitempty"`
   108  	ParentID        *string          `json:"parent_id,omitempty"`
   109  	PrivateType     *PrivateType     `json:"private_type,omitempty"`
   110  	SpeakPermission *SpeakPermission `json:"speak_permission,omitempty"`
   111  }
   112  
   113  // PatchChannelOf 修改 channel_id 指定的子频道的信息
   114  //
   115  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/patch_channel.html
   116  func (bot *Bot) PatchChannelOf(id string, config *ChannelPatch) (*Channel, error) {
   117  	return bot.patchOpenAPIofChannel("/channels/"+id, WriteBodyFromJSON(config))
   118  }
   119  
   120  // DeleteChannelOf 删除 channel_id 指定的子频道
   121  //
   122  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/delete_channel.html
   123  func (bot *Bot) DeleteChannelOf(id string) error {
   124  	return bot.DeleteOpenAPI("/channels/"+id, "", nil)
   125  }
   126  
   127  // GetOnlineNumsInChannel 查询音视频/直播子频道 channel_id 的在线成员数
   128  //
   129  // https://bot.q.qq.com/wiki/develop/api/openapi/channel/get_online_nums.html
   130  func (bot *Bot) GetOnlineNumsInChannel(id string) (int, error) {
   131  	resp := struct {
   132  		CodeMessageBase
   133  		N int `json:"online_nums"`
   134  	}{}
   135  	err := bot.GetOpenAPI("/channels/"+id+"/online_nums", "", &resp)
   136  	return resp.N, err
   137  }
   138  
   139  // ChannelPermissions 子频道权限对象
   140  //
   141  // https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/model.html
   142  type ChannelPermissions struct {
   143  	ChannelID   string `json:"channel_id"`
   144  	UserID      string `json:"user_id"` // UserID 不与 RoleID 同时出现
   145  	RoleID      string `json:"role_id"` // RoleID 不与 UserID 同时出现
   146  	Permissions string `json:"permissions"`
   147  }
   148  
   149  // GetChannelPermissionsOfUser 获取子频道 channel_id 下用户 user_id 的权限
   150  //
   151  // https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/get_channel_permissions.html
   152  func (bot *Bot) GetChannelPermissionsOfUser(channelid, userid string) (*ChannelPermissions, error) {
   153  	return bot.getOpenAPIofChannelPermissions("/channels/" + channelid + "/members/" + userid + "/permissions")
   154  }
   155  
   156  // SetChannelPermissionsOfUser 修改子频道 channel_id 下用户 user_id 的权限
   157  //
   158  // https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/put_channel_permissions.html
   159  func (bot *Bot) SetChannelPermissionsOfUser(channelid, userid string, add, remove string) error {
   160  	return bot.PutOpenAPI("/channels/"+channelid+"/members/"+userid+"/permissions", "", nil, WriteBodyFromJSON(&struct {
   161  		A string `json:"add"`
   162  		R string `json:"remove"`
   163  	}{add, remove}))
   164  }
   165  
   166  // GetChannelPermissionsOfRole 获取子频道 channel_id 下身份组 role_id 的权限
   167  //
   168  // https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/get_channel_roles_permissions.html
   169  func (bot *Bot) GetChannelPermissionsOfRole(channelid, roleid string) (*ChannelPermissions, error) {
   170  	return bot.getOpenAPIofChannelPermissions("/channels/" + channelid + "/roles/" + roleid + "/permissions")
   171  }
   172  
   173  // SetChannelPermissionsOfRole 修改子频道 channel_id 下身份组 role_id 的权限
   174  //
   175  // https://bot.q.qq.com/wiki/develop/api/openapi/channel_permissions/put_channel_roles_permissions.html
   176  func (bot *Bot) SetChannelPermissionsOfRole(channelid, roleid string, add, remove string) error {
   177  	return bot.PutOpenAPI("/channels/"+channelid+"/roles/"+roleid+"/permissions", "", nil, WriteBodyFromJSON(&struct {
   178  		A string `json:"add"`
   179  		R string `json:"remove"`
   180  	}{add, remove}))
   181  }