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 6, 2020
2 parents ab6072b + 27d3468 commit 572d976
Show file tree
Hide file tree
Showing 61 changed files with 912 additions and 917 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ rules:
- after
import/export: off
keyword-spacing: off
max-len:
- warn
- 160
# https://github.com/typescript-eslint/typescript-eslint/issues/618
no-dupe-class-members: off
no-ex-assign: off
Expand Down
2 changes: 2 additions & 0 deletions .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module.exports = {
'packages/koishi-core/tests/*.spec.ts',
'packages/koishi-utils/tests/*.spec.ts',
'packages/koishi-test-utils/tests/*.spec.ts',
'packages/plugin-common/tests/admin.spec.ts',
'packages/plugin-common/tests/sender.spec.ts',
'packages/plugin-eval/tests/*.spec.ts',
'packages/plugin-github/tests/*.spec.ts',
'packages/plugin-teach/tests/*.spec.ts',
Expand Down
29 changes: 2 additions & 27 deletions build/publish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PackageJson, getWorkspaces, spawnAsync, spawnSync } from './utils'
import { gt, prerelease } from 'semver'
import { Octokit } from '@octokit/rest'
import { draft } from './release'
import latest from 'latest-version'
import ora from 'ora'

Expand All @@ -11,15 +12,6 @@ if (CI && (GITHUB_REF !== 'refs/heads/master' || GITHUB_EVENT_NAME !== 'push'))
process.exit(0)
}

const headerMap = {
feat: 'Features',
fix: 'Bug Fixes',
dep: 'Dependencies',
}

const prefixes = Object.keys(headerMap)
const prefixRegExp = new RegExp(`^(${prefixes.join('|')})(?:\\((\\S+)\\))?: (.+)$`)

;(async () => {
let folders = await getWorkspaces()
if (process.argv[2]) {
Expand Down Expand Up @@ -66,24 +58,7 @@ const prefixRegExp = new RegExp(`^(${prefixes.join('|')})(?:\\((\\S+)\\))?: (.+)
return console.log(`Tag ${version} already exists.`)
}

const updates = {}
const lastTag = tags[tags.length - 1]
const commits = spawnSync(`git log ${lastTag}..HEAD --format=%H%s`).split(/\r?\n/).reverse()
for (const commit of commits) {
const hash = commit.slice(0, 40)
const details = prefixRegExp.exec(commit.slice(40))
if (!details) continue
let message = details[3]
if (details[2]) message = `**${details[2]}:** ${message}`
if (!updates[details[1]]) updates[details[1]] = ''
updates[details[1]] += `- ${message} (${hash})\n`
}

let body = ''
for (const type in headerMap) {
if (!updates[type]) continue
body += `## ${headerMap[type]}\n\n${updates[type]}\n`
}
const body = draft(tags[tags.length - 1])

console.log(`Start to release a new version with tag ${version} ...`)
await github.repos.createRelease({
Expand Down
36 changes: 36 additions & 0 deletions build/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { spawnSync } from './utils'

const headerMap = {
feat: 'Features',
fix: 'Bug Fixes',
dep: 'Dependencies',
}

const prefixes = Object.keys(headerMap)
const prefixRegExp = new RegExp(`^(${prefixes.join('|')})(?:\\((\\S+)\\))?: (.+)$`)

export function draft(base: string) {
const updates = {}
const commits = spawnSync(`git log ${base}..HEAD --format=%H%s`).split(/\r?\n/).reverse()
for (const commit of commits) {
const hash = commit.slice(0, 40)
const details = prefixRegExp.exec(commit.slice(40))
if (!details) continue
let message = details[3]
if (details[2]) message = `**${details[2]}:** ${message}`
if (!updates[details[1]]) updates[details[1]] = ''
updates[details[1]] += `- ${message} (${hash})\n`
}

let body = ''
for (const type in headerMap) {
if (!updates[type]) continue
body += `## ${headerMap[type]}\n\n${updates[type]}\n`
}
return body
}

if (require.main === module) {
const tags = spawnSync('git tag -l').split(/\r?\n/)
console.log(draft(tags[tags.length - 1]))
}
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
"bump": "ts-node build/bump",
"dep": "ts-node build/dep",
"docs": "cd docs && yarn dev",
"draft": "ts-node build/release",
"test": "cross-env TS_NODE_PROJECT=tsconfig.test.json mocha --experimental-vm-modules --enable-source-maps",
"test:json": "c8 -r json yarn test",
"test:lcov": "rimraf coverage && c8 -r lcov yarn test",
"test:html": "rimraf coverage && c8 -r html yarn test",
"test:text": "c8 -r text yarn test",
"lint": "eslint packages/*/src/**/*.ts --fix --cache",
"pub": "ts-node build/publish",
Expand All @@ -31,14 +32,14 @@
"version": "1.0.0",
"license": "MIT",
"devDependencies": {
"@octokit/rest": "^18.0.4",
"@octokit/rest": "^18.0.5",
"@sinonjs/fake-timers": "^6.0.1",
"@types/chai": "^4.2.12",
"@types/chai-as-promised": "^7.1.3",
"@types/cross-spawn": "^6.0.2",
"@types/fs-extra": "^9.0.1",
"@types/mocha": "^8.0.3",
"@types/node": "^14.6.3",
"@types/node": "^14.6.4",
"@types/semver": "^7.3.3",
"@types/sinonjs__fake-timers": "^6.0.1",
"@typescript-eslint/eslint-plugin": "^3.10.1",
Expand All @@ -50,7 +51,7 @@
"cross-env": "^7.0.2",
"cross-spawn": "^7.0.3",
"del": "^5.1.0",
"eslint": "^7.8.0",
"eslint": "^7.8.1",
"eslint-config-standard": "^14.1.1",
"eslint-import-resolver-typescript": "^2.3.0",
"eslint-plugin-import": "^2.22.0",
Expand Down
6 changes: 3 additions & 3 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.2",
"version": "1.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -31,7 +31,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^2.2.0"
"koishi-core": "^2.2.1"
},
"devDependencies": {
"@types/ms": "^0.7.31",
Expand All @@ -43,6 +43,6 @@
"axios": "^0.20.0",
"ms": "^2.1.2",
"ws": "^7.3.1",
"koishi-utils": "^3.1.3"
"koishi-utils": "^3.1.4"
}
}
28 changes: 12 additions & 16 deletions packages/adapter-cqhttp/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,14 @@ declare module 'koishi-core/dist/server' {
getAsync(action: string, params?: Record<string, any>): Promise<void>
sendGroupMsgAsync(groupId: number, message: string, autoEscape?: boolean): Promise<void>
sendPrivateMsgAsync(userId: number, message: string, autoEscape?: boolean): Promise<void>
setGroupAnonymousBan(groupId: number, anonymous: object, duration?: number): Promise<void>
setGroupAnonymousBan(groupId: number, flag: string, duration?: number): Promise<void>
setGroupAnonymousBanAsync(groupId: number, anonymous: object, duration?: number): Promise<void>
setGroupAnonymousBanAsync(groupId: number, flag: string, duration?: number): Promise<void>
setGroupAnonymousBan(groupId: number, anonymous: string | object, duration?: number): Promise<void>
setGroupAnonymousBanAsync(groupId: number, anonymous: string | object, duration?: number): Promise<void>
setFriendAddRequest(flag: string, approve?: boolean): Promise<void>
setFriendAddRequest(flag: string, remark?: string): Promise<void>
setFriendAddRequestAsync(flag: string, approve?: boolean): Promise<void>
setFriendAddRequestAsync(flag: string, remark?: string): Promise<void>
setGroupAddRequest(flag: string, subType: 'add' | 'invite', approve?: boolean): Promise<void>
setGroupAddRequest(flag: string, subType: 'add' | 'invite', reason?: string): Promise<void>
setGroupAddRequestAsync(flag: string, subType: 'add' | 'invite', approve?: boolean): Promise<void>
setGroupAddRequestAsync(flag: string, subType: 'add' | 'invite', reason?: string): Promise<void>
setGroupAddRequest(flag: string, subType: 'add' | 'invite', approve?: string | boolean): Promise<void>
setGroupAddRequestAsync(flag: string, subType: 'add' | 'invite', approve?: string | boolean): Promise<void>
deleteMsg(messageId: number): Promise<void>
deleteMsgAsync(messageId: number): Promise<void>
sendLike(userId: number, times?: number): Promise<void>
Expand Down Expand Up @@ -169,7 +165,7 @@ Bot.prototype.sendGroupMsg = async function (this: Bot, groupId, message, autoEs
if (!message) return
const session = this.createSession('group', 'group', groupId, message)
if (this.app.bail(session, 'before-send', session)) return
const { messageId } = await this.get<MessageResponse>('send_group_msg', { groupId, message, autoEscape })
const { messageId } = await this.get<MessageResponse>('send_group_msg', { groupId, message: session.message, autoEscape })
session.messageId = messageId
this.app.emit(session, 'send', session)
return messageId
Expand All @@ -179,14 +175,14 @@ Bot.prototype.sendGroupMsgAsync = function (this: Bot, groupId, message, autoEsc
if (!message) return
const session = this.createSession('group', 'group', groupId, message)
if (this.app.bail(session, 'before-send', session)) return
return this.getAsync('send_group_msg', { groupId, message, autoEscape })
return this.getAsync('send_group_msg', { groupId, message: session.message, autoEscape })
}

Bot.prototype.sendPrivateMsg = async function (this: Bot, userId, message, autoEscape = false) {
if (!message) return
const session = this.createSession('private', 'user', userId, message)
if (this.app.bail(session, 'before-send', session)) return
const { messageId } = await this.get<MessageResponse>('send_private_msg', { userId, message, autoEscape })
const { messageId } = await this.get<MessageResponse>('send_private_msg', { userId, message: session.message, autoEscape })
session.messageId = messageId
this.app.emit(session, 'send', session)
return messageId
Expand All @@ -196,16 +192,16 @@ Bot.prototype.sendPrivateMsgAsync = function (this: Bot, userId, message, autoEs
if (!message) return
const session = this.createSession('private', 'user', userId, message)
if (this.app.bail(session, 'before-send', session)) return
return this.getAsync('send_private_msg', { userId, message, autoEscape })
return this.getAsync('send_private_msg', { userId, message: session.message, autoEscape })
}

Bot.prototype.setGroupAnonymousBan = async function (this: Bot, groupId: number, meta: object | string, duration?: number) {
Bot.prototype.setGroupAnonymousBan = async function (this: Bot, groupId, meta, duration) {
const args = { groupId, duration } as any
args[typeof meta === 'string' ? 'flag' : 'anonymous'] = meta
await this.get('set_group_anonymous_ban', args)
}

Bot.prototype.setGroupAnonymousBanAsync = function (this: Bot, groupId: number, meta: object | string, duration?: number) {
Bot.prototype.setGroupAnonymousBanAsync = function (this: Bot, groupId, meta, duration) {
const args = { groupId, duration } as any
args[typeof meta === 'string' ? 'flag' : 'anonymous'] = meta
return this.getAsync('set_group_anonymous_ban', args)
Expand All @@ -227,15 +223,15 @@ Bot.prototype.setFriendAddRequestAsync = function (this: Bot, flag: string, info
}
}

Bot.prototype.setGroupAddRequest = async function (this: Bot, flag: string, subType: 'add' | 'invite', info: string | boolean = true) {
Bot.prototype.setGroupAddRequest = async function (this: Bot, flag, subType, info = true) {
if (typeof info === 'string') {
await this.get('set_group_add_request', { flag, subType, approve: false, reason: info })
} else {
await this.get('set_group_add_request', { flag, subType, approve: info })
}
}

Bot.prototype.setGroupAddRequestAsync = function (this: Bot, flag: string, subType: 'add' | 'invite', info: string | boolean = true) {
Bot.prototype.setGroupAddRequestAsync = function (this: Bot, flag, subType, info = true) {
if (typeof info === 'string') {
return this.getAsync('set_group_add_request', { flag, subType, approve: false, reason: info })
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-cqhttp/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ Session.prototype.$send = async function $send(this: Session, message: string, a
if (this._response) {
const session = this.$bot.createSession(this.messageType, ctxType, ctxId, message)
if (this.$app.bail(this, 'before-send', session)) return
return this._response({ reply: message, autoEscape, atSender: false })
return this._response({ reply: session.message, autoEscape, atSender: false })
}
return ctxType === 'group'
? this.$bot.sendGroupMsgAsync(ctxId, message, autoEscape)
Expand Down
4 changes: 2 additions & 2 deletions 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.0",
"version": "2.2.1",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"engines": {
Expand Down Expand Up @@ -44,7 +44,7 @@
"koa": "^2.13.0",
"koa-bodyparser": "^4.3.0",
"koa-router": "^9.4.0",
"koishi-utils": "^3.1.3",
"koishi-utils": "^3.1.4",
"leven": "^3.1.0",
"lru-cache": "^6.0.0"
}
Expand Down
3 changes: 2 additions & 1 deletion packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,9 @@ export class App extends Context {
await session.$group?._update()
}

private _parse(message: string, { $reply, $prefix, $appel, messageType }: Session, builtin: boolean, terminator = '') {
private _parse(message: string, session: Session, builtin: boolean, terminator = '') {
// group message should have prefix or appel to be interpreted as a command call
const { $reply, $prefix, $appel, messageType } = session
if (builtin && ($reply || messageType !== 'private' && $prefix === null && !$appel)) return
terminator = escapeRegExp(terminator)
const name = message.split(new RegExp(`[\\s${terminator}]`), 1)[0]
Expand Down
15 changes: 8 additions & 7 deletions packages/koishi-core/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ export interface ParsedLine<O extends {} = {}> {
options: O
}

export interface ParsedArgv<U extends User.Field = never, G extends Group.Field = never, O extends {} = {}> extends Partial<ParsedLine<O>> {
export interface ParsedArgv<U extends User.Field = never, G extends Group.Field = never, O extends {} = {}>
extends Partial<ParsedLine<O>> {
command: Command<U, G, O>
session: Session<U, G, O>
next?: NextFunction
Expand Down Expand Up @@ -268,14 +269,14 @@ export class Command<U extends User.Field = never, G extends Group.Field = never
}
}

option<K extends string>(name: K, description: string, config: StringOptionConfig): Command<U, G, Extend<O, K, string>>
option<K extends string>(name: K, description: string, config: NumberOptionConfig): Command<U, G, Extend<O, K, number>>
option<K extends string>(name: K, description: string, config: BooleanOptionConfig): Command<U, G, Extend<O, K, boolean>>
option<K extends string>(name: K, description: string, config?: OptionConfig): Command<U, G, Extend<O, K, any>>
option<K extends string>(name: K, description: string, config: OptionConfig = {}) {
option<K extends string>(name: K, desc: string, config: StringOptionConfig): Command<U, G, Extend<O, K, string>>
option<K extends string>(name: K, desc: string, config: NumberOptionConfig): Command<U, G, Extend<O, K, number>>
option<K extends string>(name: K, desc: string, config: BooleanOptionConfig): Command<U, G, Extend<O, K, boolean>>
option<K extends string>(name: K, desc: string, config?: OptionConfig): Command<U, G, Extend<O, K, any>>
option<K extends string>(name: K, desc: string, config: OptionConfig = {}) {
const fallbackType = typeof config.fallback as never
const type = config['type'] || supportedType.includes(fallbackType) && fallbackType
return this._registerOption(name, description, { ...config, type }) as any
return this._registerOption(name, desc, { ...config, type }) as any
}

removeOption<K extends string & keyof O>(name: K) {
Expand Down
3 changes: 2 additions & 1 deletion packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ export class Context {
}

removeListener<K extends keyof EventMap>(name: K, listener: EventMap[K]) {
const index = (this.app._hooks[name] || []).findIndex(([context, callback]) => context === this && callback === listener)
const index = (this.app._hooks[name] || [])
.findIndex(([context, callback]) => context === this && callback === listener)
if (index >= 0) {
this.app._hooks[name].splice(index, 1)
return true
Expand Down
3 changes: 2 additions & 1 deletion packages/koishi-core/src/plugins/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ function getOptions(command: Command, session: Session<ValidationField>, maxUsag
if (command.config.hideOptions && !config.showHidden) return []
const options = config.showHidden
? Object.values(command._options)
: Object.values(command._options).filter(option => !option.hidden && (!session.$user || option.authority <= session.$user.authority))
: Object.values(command._options)
.filter(option => !option.hidden && (!session.$user || option.authority <= session.$user.authority))
if (!options.length) return []

const output = config.authority && options.some(o => o.authority)
Expand Down
11 changes: 8 additions & 3 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ import { App } from './app'

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'
export type RequestType = 'friend' | 'group'
export type MetaEventType = 'lifecycle' | 'heartbeat'

export interface MetaTypeMap {
message: MessageType
notice: 'group_upload' | 'group_admin' | 'group_increase' | 'group_decrease' | 'group_ban' | 'friend_add' | 'group_recall'
request: 'friend' | 'group'
notice: NoticeType
request: RequestType
// eslint-disable-next-line camelcase
meta_event: 'lifecycle' | 'heartbeat'
meta_event: MetaEventType
send: null
}

Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
"dependencies": {
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"koishi-core": "^2.2.0",
"koishi-utils": "^3.1.3"
"koishi-core": "^2.2.1",
"koishi-utils": "^3.1.4"
},
"devDependencies": {
"@types/chai": "^4.2.12",
Expand Down
1 change: 1 addition & 0 deletions packages/koishi-test-utils/src/memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ extendDatabase(MemoryDatabase, {
selfId = typeof selfId === 'number' ? selfId : 0
const data = table[groupId]
if (data) return clone(data)
if (selfId < 0) return null
const fallback = Group.create(groupId, selfId)
if (selfId) table[groupId] = fallback
return clone(fallback)
Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-utils/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi-utils",
"description": "Utilities for Koishi",
"version": "3.1.3",
"version": "3.1.4",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
Loading

0 comments on commit 572d976

Please sign in to comment.