Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Sep 13, 2020
2 parents 1e1f0b3 + 04b2a60 commit ec3b26c
Show file tree
Hide file tree
Showing 44 changed files with 681 additions and 445 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ koishi run
- [Mrs4s/go-cqhttp](https://github.com/Mrs4s/go-cqhttp)
- [yyuueexxiinngg/cqhttp-mirai](https://github.com/yyuueexxiinngg/cqhttp-mirai)

请注意:尽管 Koishi 使用的协议是 [MIT](https://choosealicense.com/licenses/mit/) 协议,但上面陈述的三种途径的相关框架都使用了基于 [AGPL 3.0](https://choosealicense.com/licenses/agpl-3.0/) 的协议。因此如果你使用 koishi-adapter-cqhttp 运行你的机器人,你将可能受到 AGPL 3.0 协议的限制,**必须将你的代码开源并保持同协议**。Koishi 及其作者对使用上述框架或违反上述限制的行为所可能造成的一切后果概不负责。
请注意:尽管 Koishi 使用的协议是 [MIT](https://choosealicense.com/licenses/mit/) 协议,但上面陈述的三种途径的相关框架都使用了基于 [AGPL 3.0](https://choosealicense.com/licenses/agpl-3.0/) 的协议。因此**如果你使用 koishi-adapter-cqhttp 运行你的机器人,你将可能受到 AGPL 3.0 协议的限制,必须将你的代码开源并保持同协议**。Koishi 及其作者对使用上述框架或违反上述限制的行为所可能造成的一切后果概不负责。

### [koishi-adapter-tomon](./packages/adapter-tomon) [![npm](https://img.shields.io/npm/v/koishi-adapter-tomon?style=flat-square)](https://www.npmjs.com/package/koishi-adapter-tomon)

**[开发中]** [Tomon](https://tomon.fun) 平台支持。

## 数据库支持

Expand All @@ -79,7 +83,7 @@ MySQL 5.7 支持。

### [koishi-plugin-chess](./packages/plugin-chess) [![npm](https://img.shields.io/npm/v/koishi-plugin-chess/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-chess)

### [koishi-plugin-common](./packages/plugin-common) [![npm](https://img.shields.io/npm/v/koishi-plugin-common/next?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-common)
### [koishi-plugin-common](./packages/plugin-common) [![npm](https://img.shields.io/npm/v/koishi-plugin-common?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-common)

koishi-plugin-common 包含了一些常用功能,它们在你使用 koishi 库时是默认安装的。包含下列功能:

Expand Down
4 changes: 2 additions & 2 deletions packages/adapter-cqhttp/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-adapter-cqhttp",
"description": "CQHTTP adapter for Koishi",
"version": "1.0.3",
"version": "1.0.4",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -31,7 +31,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.2.2"
"koishi-core": "^2.3.0"
},
"devDependencies": {
"@types/ms": "^0.7.31",
Expand Down
23 changes: 23 additions & 0 deletions packages/adapter-cqhttp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,27 @@ export interface VersionInfo {
pluginBuildConfiguration: 'debug' | 'release'
}

export type HonorType = 'talkative' | 'performer' | 'legend' | 'strong_newbie' | 'emotion'

export interface TalkativeMemberInfo extends AccountInfo {
avatar: string
dayCount: number
}

export interface HonoredMemberInfo {
avatar: string
description: string
}

export interface HonorInfo {
currentTalkative: TalkativeMemberInfo
talkativeList: HonoredMemberInfo[]
performerList: HonoredMemberInfo[]
legendList: HonoredMemberInfo[]
strongNewbieList: HonoredMemberInfo[]
emotionList: HonoredMemberInfo[]
}

declare module 'koishi-core/dist/server' {
interface Bot {
_request?(action: string, params: Record<string, any>): Promise<CQResponse>
Expand Down Expand Up @@ -116,6 +137,7 @@ declare module 'koishi-core/dist/server' {
getGroupInfo(groupId: number, noCache?: boolean): Promise<GroupInfo>
getGroupMemberInfo(groupId: number, userId: number, noCache?: boolean): Promise<GroupMemberInfo>
getGroupMemberList(groupId: number): Promise<GroupMemberInfo[]>
getGroupHonorInfo(groupId: number, type: HonorType): Promise<HonorInfo>
getCookies(domain?: string): Promise<string>
getCsrfToken(): Promise<number>
getCredentials(domain?: string): Promise<Credentials>
Expand Down Expand Up @@ -301,6 +323,7 @@ defineSync('get_group_list')
defineSync('get_group_info', 'group_id', 'no_cache')
defineSync('get_group_member_info', 'group_id', 'user_id', 'no_cache')
defineSync('get_group_member_list', 'group_id')
defineSync('get_group_honor_info', 'group_id', 'type')
defineExtract('get_cookies', 'cookies', 'domain')
defineExtract('get_csrf_token', 'token')
defineSync('get_credentials', 'domain')
Expand Down
444 changes: 222 additions & 222 deletions packages/adapter-cqhttp/tests/sender.spec.ts

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions packages/adapter-tomon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "koishi-adapter-tomon",
"description": "Tomon adapter for Koishi",
"version": "0.1.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist"
],
"author": "Shigma <1700011071@pku.edu.cn>",
"license": "MIT",
"scripts": {
"lint": "eslint src --ext .ts",
"precommit": "yarn lint",
"prepack": "tsc -b"
},
"repository": {
"type": "git",
"url": "git+https://github.com/koishijs/koishi.git"
},
"bugs": {
"url": "https://github.com/koishijs/koishi/issues"
},
"homepage": "https://github.com/koishijs/koishi/tree/master/packages/koishi-core#readme",
"keywords": [
"bot",
"tomon",
"chatbot",
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.3.0"
},
"devDependencies": {
"koishi-test-utils": "^5.0.1"
},
"dependencies": {
"koishi-utils": "^3.1.5",
"tomon-sdk": "^0.2.0"
}
}
13 changes: 13 additions & 0 deletions packages/adapter-tomon/src/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Bot, Session } from 'koishi-core'

Bot.prototype.sendGroupMsg = Bot.prototype.sendPrivateMsg = async function (this: Bot, channelId, content) {
if (!content) return
const data = await this.tomon.api.route(`/channels/${channelId}/messages`).post({ data: { content } })
console.log(data)
return 0
}

Session.prototype.$send = async function $send(this: Session, message: string) {
if (!message) return
await this.$bot.sendPrivateMsg(this.channelId, message)
}
49 changes: 49 additions & 0 deletions packages/adapter-tomon/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Tomon from 'tomon-sdk'
import { Bot, Server } from 'koishi-core'

export * from './api'

declare module 'koishi-core/dist/server' {
interface BotOptions {
token?: string
}

interface Bot {
tomon?: Tomon
}
}

declare module 'koishi-core/dist/session' {
interface Session {
channelId?: number
}
}

Server.types.tomon = class TomonServer extends Server {
async __listen(bot: Bot) {
const tomon = bot.tomon = new Tomon()
await tomon.start(bot.token)
bot.ready = true
const selfId = bot.selfId = +tomon.discriminator
tomon.on('MESSAGE_CREATE', async ({ d }) => {
const userId = +d.author.discriminator
if (userId === selfId) return
const session = this.prepare({
...d,
selfId,
userId,
message: d.content,
postType: 'message',
messageType: d['guild_id'] ? 'group' : 'private',
groupId: d['guild_id'],
})
this.dispatch(session)
})
}

async _listen() {
await Promise.all(this.bots.map(bot => this.__listen(bot)))
}

_close() {}
}
10 changes: 10 additions & 0 deletions packages/adapter-tomon/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.base",
"compilerOptions": {
"outDir": "dist",
"rootDir": "src",
},
"include": [
"src",
],
}
2 changes: 1 addition & 1 deletion packages/koishi-core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-core",
"description": "Core features for Koishi",
"version": "2.2.2",
"version": "2.3.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down
100 changes: 51 additions & 49 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,57 +308,59 @@ export interface EventMap {
[Context.MIDDLEWARE_EVENT]: Middleware

// CQHTTP events
'message' (session: RawSession<'message'>): void
'message/normal' (session: RawSession<'message'>): void
'message/notice' (session: RawSession<'message'>): void
'message/anonymous' (session: RawSession<'message'>): void
'message/friend' (session: RawSession<'message'>): void
'message/group' (session: RawSession<'message'>): void
'message/other' (session: RawSession<'message'>): void
'friend-add' (session: RawSession<'notice'>): void
'group-increase' (session: RawSession<'notice'>): void
'group-increase/invite' (session: RawSession<'notice'>): void
'group-increase/approve' (session: RawSession<'notice'>): void
'group-decrease' (session: RawSession<'notice'>): void
'group-decrease/leave' (session: RawSession<'notice'>): void
'group-decrease/kick' (session: RawSession<'notice'>): void
'group-decrease/kick-me' (session: RawSession<'notice'>): void
'group-upload' (session: RawSession<'notice'>): void
'group-admin' (session: RawSession<'notice'>): void
'group-admin/set' (session: RawSession<'notice'>): void
'group-admin/unset' (session: RawSession<'notice'>): void
'group-ban' (session: RawSession<'notice'>): void
'group-ban/ban' (session: RawSession<'notice'>): void
'group-ban/lift-ban' (session: RawSession<'notice'>): void
'group_recall' (session: RawSession<'notice'>): void
'request/friend' (session: RawSession<'request'>): void
'request/group/add' (session: RawSession<'request'>): void
'request/group/invite' (session: RawSession<'request'>): void
'heartbeat' (session: RawSession<'meta_event'>): void
'lifecycle' (session: RawSession<'meta_event'>): void
'lifecycle/enable' (session: RawSession<'meta_event'>): void
'lifecycle/disable' (session: RawSession<'meta_event'>): void
'lifecycle/connect' (session: RawSession<'meta_event'>): void
'message'(session: RawSession<'message'>): void
'message/normal'(session: RawSession<'message'>): void
'message/notice'(session: RawSession<'message'>): void
'message/anonymous'(session: RawSession<'message'>): void
'message/friend'(session: RawSession<'message'>): void
'message/group'(session: RawSession<'message'>): void
'message/other'(session: RawSession<'message'>): void
'friend-add'(session: RawSession<'notice'>): void
'group-increase'(session: RawSession<'notice'>): void
'group-increase/invite'(session: RawSession<'notice'>): void
'group-increase/approve'(session: RawSession<'notice'>): void
'group-decrease'(session: RawSession<'notice'>): void
'group-decrease/leave'(session: RawSession<'notice'>): void
'group-decrease/kick'(session: RawSession<'notice'>): void
'group-decrease/kick-me'(session: RawSession<'notice'>): void
'group-upload'(session: RawSession<'notice'>): void
'group-admin'(session: RawSession<'notice'>): void
'group-admin/set'(session: RawSession<'notice'>): void
'group-admin/unset'(session: RawSession<'notice'>): void
'group-ban'(session: RawSession<'notice'>): void
'group-ban/ban'(session: RawSession<'notice'>): void
'group-ban/lift-ban'(session: RawSession<'notice'>): void
'group_recall'(session: RawSession<'notice'>): void
'friend_recall'(session: RawSession<'notice'>): void
'notify'(session: RawSession<'notice'>): void
'request/friend'(session: RawSession<'request'>): void
'request/group/add'(session: RawSession<'request'>): void
'request/group/invite'(session: RawSession<'request'>): void
'heartbeat'(session: RawSession<'meta_event'>): void
'lifecycle'(session: RawSession<'meta_event'>): void
'lifecycle/enable'(session: RawSession<'meta_event'>): void
'lifecycle/disable'(session: RawSession<'meta_event'>): void
'lifecycle/connect'(session: RawSession<'meta_event'>): void

// Koishi events
'parse' (message: string, session: Session, builtin: boolean, terminator: string): void | ExecuteArgv
'before-attach-user' (session: Session, fields: Set<User.Field>): void
'before-attach-group' (session: Session, fields: Set<Group.Field>): void
'attach-user' (session: Session): void | boolean | Promise<void | boolean>
'attach-group' (session: Session): void | boolean | Promise<void | boolean>
'attach' (session: Session): void | Promise<void>
'send' (session: Session): void | Promise<void>
'before-send' (session: Session): void | boolean
'before-command' (argv: ParsedArgv): void | string | Promise<void | string>
'command' (argv: ParsedArgv): void | Promise<void>
'middleware' (session: Session): void
'new-command' (cmd: Command): void
'remove-command' (cmd: Command): void
'before-connect' (): void | Promise<void>
'connect' (): void
'before-disconnect' (): void | Promise<void>
'disconnect' (): void
'dispose' (): void
'parse'(message: string, session: Session, builtin: boolean, terminator: string): void | ExecuteArgv
'before-attach-user'(session: Session, fields: Set<User.Field>): void
'before-attach-group'(session: Session, fields: Set<Group.Field>): void
'attach-user'(session: Session): void | boolean | Promise<void | boolean>
'attach-group'(session: Session): void | boolean | Promise<void | boolean>
'attach'(session: Session): void | Promise<void>
'send'(session: Session): void | Promise<void>
'before-send'(session: Session): void | boolean
'before-command'(argv: ParsedArgv): void | string | Promise<void | string>
'command'(argv: ParsedArgv): void | Promise<void>
'middleware'(session: Session): void
'new-command'(cmd: Command): void
'remove-command'(cmd: Command): void
'before-connect'(): void | Promise<void>
'connect'(): void
'before-disconnect'(): void | Promise<void>
'disconnect'(): void
'dispose'(): void
}

export type Events = keyof EventMap
14 changes: 10 additions & 4 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type PostType = 'message' | 'notice' | 'request' | 'meta_event' | 'send'
export type MessageType = 'private' | 'group'
export type NoticeType =
| 'group_upload' | 'group_admin' | 'group_increase' | 'group_decrease'
| 'group_ban' | 'friend_add' | 'group_recall'
| 'group_ban' | 'friend_add' | 'group_recall' | 'friend_recall' | 'notify'
export type RequestType = 'friend' | 'group'
export type MetaEventType = 'lifecycle' | 'heartbeat'

Expand All @@ -23,7 +23,9 @@ export interface MetaTypeMap {

export interface SubTypeMap {
message: 'friend' | 'group' | 'other' | 'normal' | 'anonymous' | 'notice'
notice: 'set' | 'unset' | 'approve' | 'invite' | 'leave' | 'kick' | 'kick_me' | 'ban' | 'lift_ban'
notice:
| 'set' | 'unset' | 'approve' | 'invite' | 'leave' | 'kick' | 'kick_me'
| 'ban' | 'lift_ban' | 'poke' | 'lucky_king' | 'honor'
request: 'add' | 'invite'
// eslint-disable-next-line camelcase
meta_event: 'enable' | 'disable' | 'connect'
Expand Down Expand Up @@ -57,8 +59,10 @@ export interface Meta<P extends PostType = PostType> {

// notice event
operatorId?: number
targetId?: number
duration?: number
file?: FileInfo
honorType?: 'talkative' | 'performer' | 'emotion'

// request event
comment?: string
Expand All @@ -85,11 +89,13 @@ export class Session<U extends User.Field = never, G extends Group.Field = never
$uuid = Random.uuid()

private _delay?: number
private _queued = Promise.resolve()
private _hooks?: (() => void)[] = []
private _queued: Promise<void>
private _hooks: (() => void)[]

constructor(app: App, session: Partial<Session>) {
defineProperty(this, '$app', app)
defineProperty(this, '_queued', Promise.resolve())
defineProperty(this, '_hooks', [])
Object.assign(this, session)
}

Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"koishi-core": "^2.2.2",
"koishi-core": "^2.3.0",
"koishi-utils": "^3.1.5"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit ec3b26c

Please sign in to comment.