Skip to content

Commit

Permalink
feat(onebot): update to latest go-cqhttp api
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Jun 21, 2021
1 parent af73653 commit 90d9dc8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
24 changes: 20 additions & 4 deletions packages/adapter-onebot/src/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export class CQBot extends Bot {
await this.$setGroupAddRequest(messageId, 'add', approve, comment)
}

async deleteFriend(userId: string) {
await this.$deleteFriend(userId)
}

async getStatus() {
if (this.status !== Bot.Status.GOOD) return this.status
try {
Expand All @@ -198,9 +202,16 @@ export class CQBot extends Bot {
}
}

function define(name: string, ...params: string[]) {
const asyncPrefixes = ['$set', '$send', '$delete', '$create', '$upload']

function prepareMethod(name: string) {
const prop = '$' + camelCase(name.replace(/^[_.]/, ''))
const isAsync = prop.startsWith('$set') || prop.startsWith('$send') || prop.startsWith('$delete')
const isAsync = asyncPrefixes.some(prefix => prop.startsWith(prefix))
return [prop, isAsync] as const
}

function define(name: string, ...params: string[]) {
const [prop, isAsync] = prepareMethod(name)
CQBot.prototype[prop] = async function (this: CQBot, ...args: any[]) {
const data = await this.get(name, Object.fromEntries(params.map((name, index) => [name, args[index]])))
if (!isAsync) return data
Expand All @@ -212,8 +223,7 @@ function define(name: string, ...params: string[]) {

function defineExtract(name: string, key: string, ...params: string[]) {
key = camelCase(key)
const prop = '$' + camelCase(name.replace(/^[_.]/, ''))
const isAsync = prop.startsWith('$set') || prop.startsWith('$send') || prop.startsWith('$delete')
const [prop, isAsync] = prepareMethod(name)
CQBot.prototype[prop] = async function (this: CQBot, ...args: any[]) {
const data = await this.get(name, Object.fromEntries(params.map((name, index) => [name, args[index]])))
return data[key]
Expand All @@ -238,6 +248,8 @@ defineExtract('.get_word_slices', 'slices', 'content')
define('get_group_msg_history', 'group_id', 'message_seq')
define('set_friend_add_request', 'flag', 'approve', 'remark')
define('set_group_add_request', 'flag', 'sub_type', 'approve', 'reason')
defineExtract('_get_model_show', 'variants', 'model')
define('_set_model_show', 'model', 'model_show')

define('set_group_kick', 'group_id', 'user_id', 'reject_add_request')
define('set_group_ban', 'group_id', 'user_id', 'duration')
Expand Down Expand Up @@ -266,10 +278,14 @@ define('get_group_file_system_info', 'group_id')
define('get_group_root_files', 'group_id')
define('get_group_files_by_folder', 'group_id', 'folder_id')
define('upload_group_file', 'group_id', 'file', 'name', 'folder')
define('create_group_file_folder', 'group_id', 'folder_id', 'name')
define('delete_group_folder', 'group_id', 'folder_id')
define('delete_group_file', 'group_id', 'folder_id', 'file_id', 'busid')
defineExtract('get_group_file_url', 'url', 'group_id', 'file_id', 'busid')
defineExtract('download_file', 'file', 'url', 'headers', 'thread_count')
defineExtract('get_online_clients', 'clients', 'no_cache')
defineExtract('check_url_safely', 'level', 'url')
define('delete_friend', 'user_id')

defineExtract('get_cookies', 'cookies', 'domain')
defineExtract('get_csrf_token', 'token')
Expand Down
14 changes: 13 additions & 1 deletion packages/adapter-onebot/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,11 @@ export interface Device {
deviceKind: string
}

export interface ModelVariant {
modelShow: string
needPay: boolean
}

export enum SafetyLevel { safe, unknown, danger }

type id = string | number
Expand All @@ -295,6 +300,8 @@ export interface API {
$getWordSlices(content: string): Promise<string[]>
$ocrImage(image: string): Promise<OcrResult>
$getGroupMsgHistory(groupId: id, messageSeq: id): Promise<Message[]>
$deleteFriend(userId: id): Promise<void>
$deleteFriendAsync(userId: id): Promise<void>
$setFriendAddRequest(flag: string, approve: boolean, remark?: string): Promise<void>
$setFriendAddRequestAsync(flag: string, approve: boolean, remark?: string): Promise<void>
$setGroupAddRequest(flag: string, subType: 'add' | 'invite', approve: boolean, reason?: string): Promise<void>
Expand Down Expand Up @@ -338,10 +345,15 @@ export interface API {
$getGroupRootFiles(groupId: id): Promise<GroupFileList>
$getGroupFilesByFolder(groupId: id, folderId: string): Promise<GroupFileList>
$getGroupFileUrl(groupId: id, fileId: string, busid: number): Promise<string>
$uploadGroupFile(groupId: id, file: string, name: string, folder?: string): Promise<void>
$downloadFile(url: string, headers?: string | string[], threadCount?: number): Promise<string>
$uploadGroupFile(groupId: id, file: string, name: string, folder?: string): Promise<void>
$createGroupFileFolder(groupId: id, folderId: string, name: string): Promise<void>
$deleteGroupFolder(groupId: id, folderId: string): Promise<void>
$deleteGroupFile(groupId: id, folderId: string, fileId: string, busid: number): Promise<void>
$getOnlineClients(noCache?: boolean): Promise<Device[]>
$checkUrlSafely(url: string): Promise<SafetyLevel>
$getModelShow(model: string): Promise<ModelVariant[]>
$setModelShow(model: string, modelShow: string): Promise<void>

$getCookies(domain?: string): Promise<string>
$getCsrfToken(): Promise<number>
Expand Down
4 changes: 4 additions & 0 deletions packages/adapter-onebot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ export function createSession(adapter: Adapter, data: any) {
session.type = session.userId === session.selfId ? 'group-added' : 'group-member-added'
session.subtype = session.userId === session.operatorId ? 'active' : 'passive'
break
case 'group_card':
session.type = 'group-member'
session.subtype = 'nickname'
break
case 'notify':
session.type = 'notice'
session.subtype = paramCase(data.sub_type)
Expand Down
1 change: 1 addition & 0 deletions packages/koishi-core/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export interface Bot<P = Platform> extends BotOptions, UserBase {
getSelf(): Promise<UserInfo>
getUser(userId: string): Promise<UserInfo>
getFriendList(): Promise<UserInfo[]>
deleteFriend(userId: string): Promise<void>

// group
getGroup(groupId: string): Promise<GroupInfo>
Expand Down
1 change: 1 addition & 0 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export namespace Session {
'group-member': {
'role': {}
'ban': {}
'nickname': {}
}
'notice': {
'poke': {}
Expand Down

0 comments on commit 90d9dc8

Please sign in to comment.