Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 21, 2021
2 parents 1f4f187 + 564889a commit fb7ff55
Show file tree
Hide file tree
Showing 67 changed files with 753 additions and 382 deletions.
2 changes: 1 addition & 1 deletion build/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const KOISHI_VERSION = JSON.stringify(version)
}],
}

if (name === 'koishi') {
if (name === 'koishi' || name === 'plugin-puppeteer') {
entryPoints.push(base + '/src/worker.ts')
} else if (name === 'plugin-eval') {
const loaders = await readdir(base + '/src/loaders')
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@
"@types/chai-as-promised": "^7.1.3",
"@types/cross-spawn": "^6.0.2",
"@types/estree": "^0.0.47",
"@types/fs-extra": "^9.0.10",
"@types/fs-extra": "^9.0.11",
"@types/mocha": "^8.2.2",
"@types/node": "^14.14.37",
"@types/node": "^14.14.41",
"@types/rimraf": "^3.0.0",
"@types/semver": "^7.3.4",
"@types/sinonjs__fake-timers": "^6.0.2",
"@types/source-map-support": "^0.5.3",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"c8": "^7.7.0",
"c8": "^7.7.1",
"cac": "^6.7.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"cross-env": "^7.0.3",
"cross-spawn": "^7.0.3",
"del": "^6.0.0",
"esbuild": "^0.11.6",
"eslint": "^7.23.0",
"esbuild": "^0.11.12",
"eslint": "^7.24.0",
"eslint-config-standard": "^16.0.2",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-import": "^2.22.1",
Expand All @@ -74,15 +74,15 @@
"latest-version": "^5.1.0",
"mocha": "^8.3.2",
"nock": "^13.0.11",
"open": "^8.0.5",
"open": "^8.0.6",
"ora": "^5.4.0",
"p-map": "^4.0.0",
"p-map": "^5.0.0",
"prompts": "^2.4.1",
"rimraf": "^3.0.2",
"semver": "^7.3.5",
"source-map-support": "^0.5.19",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.2.3"
"typescript": "^4.2.4"
}
}
4 changes: 2 additions & 2 deletions packages/adapter-discord/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.9.1"
"koishi-core": "^3.9.2"
},
"devDependencies": {
"@types/ws": "^7.4.1",
Expand All @@ -38,6 +38,6 @@
"axios": "^0.21.1",
"file-type": "^16.3.0",
"form-data": "^4.0.0",
"ws": "^7.4.4"
"ws": "^7.4.5"
}
}
6 changes: 3 additions & 3 deletions packages/adapter-kaiheila/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.9.1"
"koishi-core": "^3.9.2"
},
"devDependencies": {
"koishi-test-utils": "^6.0.0-beta.11"
},
"dependencies": {
"axios": "^0.21.1",
"koishi-utils": "^4.2.0",
"ws": "^7.4.4"
"koishi-utils": "^4.2.1",
"ws": "^7.4.5"
}
}
6 changes: 3 additions & 3 deletions packages/adapter-onebot/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.9.1"
"koishi-core": "^3.9.2"
},
"devDependencies": {
"@types/ws": "^7.4.1",
Expand All @@ -36,7 +36,7 @@
},
"dependencies": {
"axios": "^0.21.1",
"koishi-utils": "^4.2.0",
"ws": "^7.4.4"
"koishi-utils": "^4.2.1",
"ws": "^7.4.5"
}
}
4 changes: 2 additions & 2 deletions packages/adapter-telegram/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.9.1"
"koishi-core": "^3.9.2"
},
"devDependencies": {
"koishi-test-utils": "^6.0.0-beta.11"
},
"dependencies": {
"axios": "^0.21.1",
"form-data": "^4.0.0",
"koishi-utils": "^4.2.0"
"koishi-utils": "^4.2.1"
}
}
6 changes: 3 additions & 3 deletions packages/adapter-tomon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
"koishi"
],
"peerDependencies": {
"koishi-core": "^3.9.1"
"koishi-core": "^3.9.2"
},
"devDependencies": {
"@types/pako": "^1.0.1",
"koishi-test-utils": "^6.0.0-beta.11"
},
"dependencies": {
"axios": "^0.21.1",
"koishi-utils": "^4.2.0",
"koishi-utils": "^4.2.1",
"pako": "^2.0.3",
"ws": "^7.4.4"
"ws": "^7.4.5"
}
}
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": "3.9.1",
"version": "3.9.2",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"engines": {
Expand Down Expand Up @@ -42,7 +42,7 @@
"fastest-levenshtein": "^1.0.12",
"koa": "^2.13.1",
"koa-bodyparser": "^4.3.0",
"koishi-utils": "^4.2.0",
"koishi-utils": "^4.2.1",
"lru-cache": "^6.0.0"
}
}
24 changes: 17 additions & 7 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function createLeadingRE(patterns: string[], prefix = '', suffix = '') {
return patterns.length ? new RegExp(`^${prefix}(${patterns.map(escapeRegExp).join('|')})${suffix}`) : /$^/
}

interface CommandMap extends Map<string, Command> {
resolve(key: string): Command
}

export class App extends Context {
public app = this
public options: AppOptions
Expand All @@ -52,7 +56,7 @@ export class App extends Context {

_bots = createBots('sid')
_commandList: Command[] = []
_commands = new Map<string, Command>()
_commands: CommandMap = new Map<string, Command>() as never
_shortcuts: Command.Shortcut[] = []
_hooks: Record<keyof any, [Context, (...args: any[]) => any][]> = {}
_userCache: Record<string, LruCache<string, Observed<Partial<User>, Promise<void>>>>
Expand Down Expand Up @@ -102,6 +106,16 @@ export class App extends Context {
Adapter.from(this, bot).create(bot)
}

this._commands.resolve = (key) => {
if (!key) return
const segments = key.split('.')
let i = 1, name = segments[0], cmd: Command
while ((cmd = this._commands.get(name)) && i < segments.length) {
name = cmd.name + '.' + segments[i++]
}
return cmd
}

this.prepare()

// bind built-in event listeners
Expand All @@ -118,14 +132,10 @@ export class App extends Context {
// group message should have prefix or appel to be interpreted as a command call
if (argv.root && subtype !== 'private' && parsed.prefix === null && !parsed.appel) return
if (!argv.tokens.length) return
const segments = argv.tokens[0].content.split('.')
let i = 1, name = segments[0], cmd: Command
while ((cmd = this._commands.get(name)) && i < segments.length) {
name = cmd.name + '.' + segments[i++]
}
const cmd = this._commands.resolve(argv.tokens[0].content)
if (cmd) {
argv.tokens.shift()
return name
return cmd.name
}
})

Expand Down
9 changes: 2 additions & 7 deletions packages/koishi-core/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,8 @@ export class Context {

async transformAssets(content: string, assets = this.assets) {
if (!assets) return content
const urlMap: Record<string, string> = {}
await Promise.all(segment.parse(content).map(async ({ type, data }) => {
if (!assets.types.includes(type as Assets.Type)) return
urlMap[data.url] = await assets.upload(data.url, data.file)
}))
return segment.transform(content, Object.fromEntries(assets.types.map((type) => {
return [type, (data) => segment(type, { url: urlMap[data.url] })]
return segment.transformAsync(content, Object.fromEntries(assets.types.map((type) => {
return [type, async (data) => segment(type, { url: await assets.upload(data.url, data.file) })]
})))
}

Expand Down
2 changes: 1 addition & 1 deletion packages/koishi-core/src/help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default function apply(ctx: Context) {

const app = ctx.app
function findCommand(target: string) {
const command = app._commands.get(target)
const command = app._commands.resolve(target)
if (command) return command
const shortcut = app._shortcuts.find(({ name }) => {
return typeof name === 'string' ? name === target : name.test(target)
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 @@ -37,8 +37,8 @@
"dependencies": {
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"koishi-core": "^3.9.1",
"koishi-utils": "^4.2.0"
"koishi-core": "^3.9.2",
"koishi-utils": "^4.2.1"
},
"devDependencies": {
"@types/chai": "^4.2.16",
Expand Down
6 changes: 3 additions & 3 deletions 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": "4.2.0",
"version": "4.2.1",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
"files": [
Expand All @@ -28,10 +28,10 @@
"utilities"
],
"devDependencies": {
"@types/supports-color": "^7.2.1",
"@types/supports-color": "^8.1.0",
"koishi-test-utils": "^6.0.0-beta.11"
},
"dependencies": {
"supports-color": "^8.1.1"
"supports-color": "^8.1.0"
}
}
16 changes: 14 additions & 2 deletions packages/koishi-utils/src/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export namespace segment {
export type Chain = segment.Parsed[]
export type Data = Record<string, primitive>
export type Transformer = string | ((data: Record<string, string>, index: number, chain: Chain) => string)
export type AsyncTransformer = string | ((data: Record<string, string>, index: number, chain: Chain) => string | Promise<string>)

export interface Parsed extends segment {
data: Record<string, string>
Expand All @@ -44,8 +45,8 @@ export namespace segment {
.replace(/&amp;/g, '&')
}

export function join(codes: segment[]) {
return codes.map(code => segment(code.type, code.data)).join('')
export function join(chain: segment[]) {
return chain.map(node => segment(node.type, node.data)).join('')
}

export interface FindOptions {
Expand Down Expand Up @@ -91,6 +92,17 @@ export namespace segment {
}).join('')
}

export async function transformAsync(source: string, rules: Record<string, AsyncTransformer>) {
const chain = segment.parse(source)
const cache = new Map<Parsed, string>()
await Promise.all(chain.map(async (node, index, chain) => {
const transformer = rules[node.type]
if (!transformer) return
cache.set(node, typeof transformer === 'string' ? transformer : await transformer(node.data, index, chain))
}))
return chain.map(node => cache.get(node) || segment(node.type, node.data)).join('')
}

export type Factory<T> = (value: T, data?: segment.Data) => string

function createFactory(type: string, key: string): Factory<primitive> {
Expand Down
14 changes: 7 additions & 7 deletions packages/koishi/ecosystem.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
"description": "Asset provider plugin for Koishi"
},
"koishi-plugin-chat": {
"version": "0.3.1",
"version": "0.4.0",
"description": "Chat plugin for Koishi"
},
"koishi-plugin-chess": {
"version": "3.0.1",
"version": "3.0.2",
"description": "Chess Plugin for Koishi"
},
"koishi-plugin-common": {
Expand All @@ -44,11 +44,11 @@
"description": "COC / DND Tools for Koishi"
},
"koishi-plugin-eval": {
"version": "3.1.0",
"version": "3.1.1",
"description": "Execute JavaScript in Koishi"
},
"koishi-plugin-github": {
"version": "3.0.3",
"version": "3.0.4",
"description": "GitHub webhook plugin for Koishi"
},
"koishi-plugin-image-search": {
Expand All @@ -60,14 +60,14 @@
"description": "MongoDB support for Koishi"
},
"koishi-plugin-monitor": {
"version": "1.0.0-beta.29"
"version": "1.0.0-beta.30"
},
"koishi-plugin-mysql": {
"version": "3.3.1",
"description": "MySQL support for Koishi"
},
"koishi-plugin-puppeteer": {
"version": "2.0.2",
"version": "2.1.0",
"description": "Take Screenshots in Koishi"
},
"koishi-plugin-rss": {
Expand All @@ -87,7 +87,7 @@
"description": "Some simple tools for Koishi"
},
"koishi-plugin-webui": {
"version": "4.3.0",
"version": "4.4.0",
"description": "Show Web UI of Koishi"
}
}
8 changes: 4 additions & 4 deletions packages/koishi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "koishi",
"description": "A QQ bot framework based on CQHTTP",
"version": "3.9.1",
"version": "3.9.2",
"main": "index.js",
"typings": "index.d.ts",
"engines": {
Expand Down Expand Up @@ -38,10 +38,10 @@
"dependencies": {
"cac": "^6.7.2",
"chokidar": "^3.5.1",
"js-yaml": "^4.0.0",
"js-yaml": "^4.1.0",
"kleur": "^4.1.4",
"koishi-core": "^3.9.1",
"koishi-utils": "^4.2.0",
"koishi-core": "^3.9.2",
"koishi-utils": "^4.2.1",
"prompts": "^2.4.1"
}
}
Loading

0 comments on commit fb7ff55

Please sign in to comment.