diff --git a/package.json b/package.json index 854351c6c1..d35be29dc8 100644 --- a/package.json +++ b/package.json @@ -23,37 +23,37 @@ "version": "1.0.0", "license": "MIT", "devDependencies": { - "@octokit/rest": "^17.1.3", + "@octokit/rest": "^17.3.0", "@types/cross-spawn": "^6.0.1", "@types/fs-extra": "^8.1.0", - "@types/jest": "^25.1.4", - "@types/node": "^13.9.1", + "@types/jest": "^25.2.1", + "@types/node": "^13.13.0", "@types/semver": "^7.1.0", - "@typescript-eslint/eslint-plugin": "^2.25.0", - "@typescript-eslint/parser": "^2.25.0", + "@typescript-eslint/eslint-plugin": "^2.28.0", + "@typescript-eslint/parser": "^2.28.0", "cac": "^6.5.8", - "cross-spawn": "^7.0.1", + "cross-spawn": "^7.0.2", "del": "^5.1.0", "eslint": "^6.8.0", "eslint-config-standard": "^14.1.1", - "eslint-plugin-import": "^2.20.1", + "eslint-plugin-import": "^2.20.2", "eslint-plugin-jest": "^23.8.2", - "eslint-plugin-node": "^11.0.0", + "eslint-plugin-node": "^11.1.0", "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.1", "express": "^4.17.1", "fs-extra": "^9.0.0", "globby": "^11.0.0", - "jest": "^25.2.1", + "jest": "^25.3.0", "kleur": "^3.0.3", "latest-version": "^5.1.0", "open": "^7.0.3", "ora": "^4.0.3", "p-map": "^4.0.0", "prompts": "^2.3.2", - "semver": "^7.1.3", - "ts-jest": "^25.2.1", - "ts-node": "^8.8.1", + "semver": "^7.3.2", + "ts-jest": "^25.4.0", + "ts-node": "^8.8.2", "typescript": "^3.8.3" } } diff --git a/packages/database-level/package.json b/packages/database-level/package.json index 5d1d3d5d0b..24e5f42daa 100644 --- a/packages/database-level/package.json +++ b/packages/database-level/package.json @@ -1,7 +1,7 @@ { "name": "koishi-database-level", "description": "Leveldb support for Koishi", - "version": "1.1.7", + "version": "1.2.0", "main": "dist/index.js", "files": [ "dist" @@ -33,17 +33,17 @@ "leveldb" ], "devDependencies": { - "koishi-test-utils": "^3.2.2" + "koishi-test-utils": "^3.3.0" }, "peerDependencies": { - "koishi-core": "^1.11.2" + "koishi-core": "^1.12.0" }, "dependencies": { "@types/leveldown": "^4.0.2", "@types/levelup": "^4.3.0", "koishi-utils": "^1.0.4", - "leveldown": "^5.5.1", - "levelup": "^4.3.2", - "subleveldown": "^4.1.4" + "leveldown": "^5.6.0", + "levelup": "^4.4.0", + "subleveldown": "^5.0.0" } } diff --git a/packages/database-memory/package.json b/packages/database-memory/package.json index 46a701858d..b288d92388 100644 --- a/packages/database-memory/package.json +++ b/packages/database-memory/package.json @@ -1,7 +1,7 @@ { "name": "koishi-database-memory", "description": "An in-memory database implementation for Koishi", - "version": "1.1.7", + "version": "1.2.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -22,7 +22,7 @@ }, "homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-memory#readme", "peerDependencies": { - "koishi-core": "^1.11.2" + "koishi-core": "^1.12.0" }, "dependencies": { "koishi-utils": "^1.0.4" diff --git a/packages/database-mysql/package.json b/packages/database-mysql/package.json index a35faf8fc0..a3c12fbffe 100644 --- a/packages/database-mysql/package.json +++ b/packages/database-mysql/package.json @@ -1,7 +1,7 @@ { "name": "koishi-database-mysql", "description": "MySQL support for Koishi", - "version": "1.1.7", + "version": "1.2.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -32,10 +32,10 @@ "mysql" ], "devDependencies": { - "@types/mysql": "^2.15.9" + "@types/mysql": "^2.15.10" }, "peerDependencies": { - "koishi-core": "^1.11.2" + "koishi-core": "^1.12.0" }, "dependencies": { "koishi-utils": "^1.0.4", diff --git a/packages/database-sqlite/package.json b/packages/database-sqlite/package.json index c2e656c4f6..7e925875be 100644 --- a/packages/database-sqlite/package.json +++ b/packages/database-sqlite/package.json @@ -1,6 +1,6 @@ { "name": "koishi-database-sqlite", - "version": "1.0.0-alpha.13", + "version": "1.0.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -22,10 +22,10 @@ "homepage": "https://github.com/koishijs/koishi/tree/master/packages/database-sqlite#readme", "devDependencies": { "@types/sqlite3": "^3.1.6", - "koishi-test-utils": "^3.2.2" + "koishi-test-utils": "^3.3.0" }, "peerDependencies": { - "koishi-core": "^1.11.2" + "koishi-core": "^1.12.0" }, "dependencies": { "koishi-utils": "^1.0.4", diff --git a/packages/koishi-cli/package.json b/packages/koishi-cli/package.json index 611277b1b5..a55ab285d5 100644 --- a/packages/koishi-cli/package.json +++ b/packages/koishi-cli/package.json @@ -1,7 +1,7 @@ { "name": "koishi", "description": "A QQ bot framework based on CQHTTP", - "version": "1.11.2", + "version": "1.12.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -38,9 +38,9 @@ "cac": "^6.5.8", "js-yaml": "^3.13.1", "kleur": "^3.0.3", - "koishi-core": "^1.11.2", - "koishi-plugin-common": "^2.1.8", - "koishi-plugin-schedule": "^1.0.13", + "koishi-core": "^1.12.0", + "koishi-plugin-common": "^2.2.0", + "koishi-plugin-schedule": "^1.1.0", "koishi-utils": "^1.0.4", "prompts": "^2.3.2" } diff --git a/packages/koishi-cli/src/worker.ts b/packages/koishi-cli/src/worker.ts index af2c13d960..962341447e 100644 --- a/packages/koishi-cli/src/worker.ts +++ b/packages/koishi-cli/src/worker.ts @@ -4,7 +4,7 @@ import { capitalize } from 'koishi-utils' import { performance } from 'perf_hooks' import { cyan, yellow } from 'kleur' import { logger } from './utils' -import { format, types } from 'util' +import { format } from 'util' import { readFileSync } from 'fs' import { safeLoad } from 'js-yaml' @@ -72,7 +72,7 @@ function loadEcosystem (type: string, name: string) { try { return require(name) } catch (error) { - if (error.code !== 'MODULE_NOT_FOUND') { + if (error.code !== 'MODULE_NOT_FOUND' || error.requireStack[0] !== __filename) { throw error } } diff --git a/packages/koishi-core/package.json b/packages/koishi-core/package.json index 93dc617791..c930c6c386 100644 --- a/packages/koishi-core/package.json +++ b/packages/koishi-core/package.json @@ -1,7 +1,7 @@ { "name": "koishi-core", "description": "Core features for Koishi", - "version": "1.11.2", + "version": "1.12.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -33,15 +33,15 @@ ], "devDependencies": { "@types/debug": "^4.1.5", - "@types/ws": "^7.2.3", + "@types/ws": "^7.2.4", "get-port": "^5.1.1", - "koishi-database-memory": "^1.1.7", - "koishi-test-utils": "^3.2.2" + "koishi-database-memory": "^1.2.0", + "koishi-test-utils": "^3.3.0" }, "dependencies": { "axios": "^0.19.2", "debug": "^4.1.1", - "escape-string-regexp": "^2.0.0", + "escape-string-regexp": "^3.0.0", "koishi-utils": "^1.0.4", "leven": "^3.1.0", "ms": "^2.1.2", diff --git a/packages/koishi-core/src/app.ts b/packages/koishi-core/src/app.ts index 52facd7bc1..6128c80368 100644 --- a/packages/koishi-core/src/app.ts +++ b/packages/koishi-core/src/app.ts @@ -4,10 +4,10 @@ import { Sender } from './sender' import { Server, createServer, ServerType } from './server' import { Command, ShortcutConfig, ParsedCommandLine } from './command' import { Context, Middleware, NextFunction, ContextScope, Events, EventMap } from './context' -import { GroupFlag, UserFlag, UserField, createDatabase, DatabaseConfig, GroupField } from './database' +import { GroupFlag, UserFlag, UserField, createDatabase, DatabaseConfig, GroupField, createUser } from './database' import { showSuggestions } from './utils' import { Meta } from './meta' -import { simplify, noop } from 'koishi-utils' +import { simplify, noop, observe } from 'koishi-utils' import { errors, messages } from './messages' import { ParsedLine } from './parser' @@ -444,7 +444,9 @@ export class App extends Context { const defaultAuthority = typeof this.options.defaultAuthority === 'function' ? this.options.defaultAuthority(meta) : this.options.defaultAuthority || 0 - const user = await this.database.observeUser(meta.userId, defaultAuthority, Array.from(userFields)) + const user = meta.anonymous + ? observe(createUser(meta.userId, defaultAuthority)) + : await this.database.observeUser(meta.userId, defaultAuthority, Array.from(userFields)) Object.defineProperty(meta, '$user', { value: user, writable: true }) return user } diff --git a/packages/koishi-core/src/meta.ts b/packages/koishi-core/src/meta.ts index 8bdae80613..2f4d12f5d6 100644 --- a/packages/koishi-core/src/meta.ts +++ b/packages/koishi-core/src/meta.ts @@ -1,5 +1,6 @@ import { User, Group } from './database' import { ParsedCommandLine } from './command' +import { App } from './app' export type PostType = 'message' | 'notice' | 'request' | 'meta_event' | 'send' export type MessageType = 'private' | 'group' | 'discuss' @@ -61,6 +62,7 @@ export interface Meta { $ctxType?: ContextType // other properties + $app?: App $argv?: ParsedCommandLine $parsed?: ParsedMessage diff --git a/packages/koishi-core/src/parser.ts b/packages/koishi-core/src/parser.ts index 0d3b207c6e..5e9c3154a8 100644 --- a/packages/koishi-core/src/parser.ts +++ b/packages/koishi-core/src/parser.ts @@ -109,9 +109,16 @@ interface ParsedArg0 { quoted: boolean } +// eslint-disable-next-line quotes +const quotes = `"'“”‘’` + +function parseRest (source: string) { + if (quotes.includes(source[0]) && quotes.includes(source[source.length - 1])) return source.slice(1, -1) + return source +} + function parseArg0 (source: string): ParsedArg0 { - const char0 = source[0] - if (char0 === '"' || char0 === "'" || char0 === '“' || char0 === '”') { + if (quotes.includes(source[0])) { const [content] = source.slice(1).split(/["'“”](?=\s|$)/, 1) return { quoted: true, @@ -170,7 +177,7 @@ export function parseLine (source: string, argsDef: CommandArgument[], optsDef: while (source) { // long argument if (source[0] !== '-' && argsDef[args.length] && argsDef[args.length].noSegment) { - args.push(source) + args.push(parseRest(source)) break } @@ -184,7 +191,7 @@ export function parseLine (source: string, argsDef: CommandArgument[], optsDef: continue } else if (arg === '--') { // rest part - rest = arg0.rest + rest = parseRest(arg0.rest) break } @@ -193,7 +200,7 @@ export function parseLine (source: string, argsDef: CommandArgument[], optsDef: for (; i < arg.length; ++i) { if (arg.charCodeAt(i) !== 45) break } - if (arg.slice(i, i + 3) === 'no-') { + if (arg.slice(i, i + 3) === 'no-' && !optsDef[arg.slice(i)]) { name = arg.slice(i + 3) handleOption(name, true, false) continue diff --git a/packages/koishi-core/src/server.ts b/packages/koishi-core/src/server.ts index ef9290340d..0c19bdc483 100644 --- a/packages/koishi-core/src/server.ts +++ b/packages/koishi-core/src/server.ts @@ -103,6 +103,7 @@ export abstract class Server { Object.defineProperty(meta, '$ctxType', { value: ctxType }) const app = this.appMap[meta.selfId] + Object.defineProperty(meta, '$app', { value: app }) // add context properties if (meta.postType === 'message') { @@ -343,6 +344,7 @@ export class WsClient extends Server { this.version = camelCase(parsed.data) this.debug('connect to ws server:', this.app.options.server) resolve() + } else { this._listeners[parsed.echo]?.(parsed) } }) diff --git a/packages/koishi-utils/src/cqCode.ts b/packages/koishi-utils/src/cqCode.ts index 0910d5ed1a..6f9704448d 100644 --- a/packages/koishi-utils/src/cqCode.ts +++ b/packages/koishi-utils/src/cqCode.ts @@ -1,4 +1,4 @@ -type CQCodeData = Record +type CQCodeData = Record interface CQCode { type: string @@ -25,7 +25,7 @@ namespace CQCode { .replace(/&/g, '&') } - export function stringify (type: string, data: CQCodeData) { + export function stringify (type: string, data: CQCodeData = {}) { if (type === 'text') return '' + data.text let output = '[CQ:' + type for (const key in data) { diff --git a/packages/plugin-common/package.json b/packages/plugin-common/package.json index b73b3698a5..ada749a620 100644 --- a/packages/plugin-common/package.json +++ b/packages/plugin-common/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-common", "description": "Common plugins for Koishi", - "version": "2.1.8", + "version": "2.2.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -28,11 +28,11 @@ "plugin" ], "devDependencies": { - "koishi-database-memory": "^1.1.7", - "koishi-test-utils": "^3.2.2" + "koishi-database-memory": "^1.2.0", + "koishi-test-utils": "^3.3.0" }, "dependencies": { - "koishi-core": "^1.11.2", + "koishi-core": "^1.12.0", "koishi-utils": "^1.0.4" } } diff --git a/packages/plugin-common/src/contextify.ts b/packages/plugin-common/src/contextify.ts index c9e50f0b2f..274ef1348d 100644 --- a/packages/plugin-common/src/contextify.ts +++ b/packages/plugin-common/src/contextify.ts @@ -47,6 +47,7 @@ export default function apply (ctx: Context) { } } + Object.defineProperty(newMeta, '$app', { value: ctx.app }) Object.defineProperty(newMeta, '$user', { value: user, writable: true }) delete newMeta.groupId @@ -72,6 +73,14 @@ export default function apply (ctx: Context) { newMeta.subType = options.type || 'other' } + if (options.group) { + const info = await ctx.sender.getGroupMemberInfo(ctxId, newMeta.userId).catch(() => ({})) + Object.assign(newMeta.sender, info) + } else if (options.user) { + const info = await ctx.sender.getStrangerInfo(newMeta.userId).catch(() => ({})) + Object.assign(newMeta.sender, info) + } + // generate path Object.defineProperty(newMeta, '$ctxId', { value: ctxId }) Object.defineProperty(newMeta, '$ctxType', { value: ctxType }) diff --git a/packages/plugin-common/src/echo.ts b/packages/plugin-common/src/echo.ts index 2452eed1d2..f8b48454a3 100644 --- a/packages/plugin-common/src/echo.ts +++ b/packages/plugin-common/src/echo.ts @@ -1,7 +1,10 @@ import { MetaTypeMap, Context } from 'koishi-core' +import { CQCode } from 'koishi-utils' export default function (ctx: Context) { ctx.command('echo ', '向多个上下文发送广播', { authority: 2 }) + .option('-a, --anonymous', '匿名发送消息') + .option('-A, --force-anonymous', '匿名发送消息') .option('-u, --user ', '指定信息发送的目标 QQ 号', { isString: true, authority: 4 }) .option('-g, --group ', '指定信息发送的目标群号', { isString: true, authority: 4 }) .option('-d, --discuss ', '指定信息发送的目标讨论组号', { isString: true, authority: 4 }) @@ -19,6 +22,12 @@ export default function (ctx: Context) { channels[meta.messageType].push(meta.messageType === 'private' ? meta.userId : meta[meta.messageType + 'Id']) } + if (options.forceAnonymous) { + message = CQCode.stringify('anonymous') + message + } else if (options.anonymous) { + message = CQCode.stringify('anonymous', { ignore: true }) + message + } + // send messages return Promise.all([ ...channels.private.map(id => ctx.sender.sendPrivateMsgAsync(+id, message)), diff --git a/packages/plugin-nlp/package.json b/packages/plugin-nlp/package.json index c4b3bdc676..414d1ecc62 100644 --- a/packages/plugin-nlp/package.json +++ b/packages/plugin-nlp/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-nlp", "description": "Natural Language Processor for Koishi", - "version": "1.0.8", + "version": "1.1.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -30,10 +30,10 @@ "jieba" ], "devDependencies": { - "koishi-test-utils": "^3.2.2" + "koishi-test-utils": "^3.3.0" }, "dependencies": { - "koishi-core": "^1.11.2", + "koishi-core": "^1.12.0", "koishi-utils": "^1.0.4", "nodejieba": "^2.4.1" } diff --git a/packages/plugin-recorder/package.json b/packages/plugin-recorder/package.json index a902f5a74d..f1d44d23b7 100644 --- a/packages/plugin-recorder/package.json +++ b/packages/plugin-recorder/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-recorder", "description": "Save Chat Records for Koishi", - "version": "1.0.0-alpha.11", + "version": "1.0.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -31,10 +31,10 @@ ], "devDependencies": { "del": "^5.1.0", - "koishi-test-utils": "^3.2.2" + "koishi-test-utils": "^3.3.0" }, "dependencies": { - "koishi-core": "^1.11.2", + "koishi-core": "^1.12.0", "koishi-utils": "^1.0.4" } } diff --git a/packages/plugin-schedule/package.json b/packages/plugin-schedule/package.json index 73df0039b8..0414a9b186 100644 --- a/packages/plugin-schedule/package.json +++ b/packages/plugin-schedule/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-schedule", "description": "Schedule plugin for Koishi", - "version": "1.0.13", + "version": "1.1.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -31,12 +31,12 @@ ], "devDependencies": { "@types/ms": "^0.7.31", - "koishi-database-level": "^1.1.7", - "koishi-database-mysql": "^1.1.7", - "koishi-test-utils": "^3.2.2" + "koishi-database-level": "^1.2.0", + "koishi-database-mysql": "^1.2.0", + "koishi-test-utils": "^3.3.0" }, "dependencies": { - "koishi-core": "^1.11.2", + "koishi-core": "^1.12.0", "koishi-utils": "^1.0.4", "ms": "^2.1.2" } diff --git a/packages/plugin-teach/package.json b/packages/plugin-teach/package.json index dc02427f52..a9b54c9e12 100644 --- a/packages/plugin-teach/package.json +++ b/packages/plugin-teach/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-teach", "description": "Teach plugin for Koishi", - "version": "0.1.22", + "version": "0.2.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -31,12 +31,12 @@ "conversation" ], "devDependencies": { - "koishi-database-level": "^1.1.7", - "koishi-database-mysql": "^1.1.7", - "koishi-test-utils": "^3.2.2" + "koishi-database-level": "^1.2.0", + "koishi-database-mysql": "^1.2.0", + "koishi-test-utils": "^3.3.0" }, "dependencies": { - "koishi-core": "^1.11.2", + "koishi-core": "^1.12.0", "koishi-utils": "^1.0.4" } } diff --git a/packages/test-utils/package.json b/packages/test-utils/package.json index 1b419c582d..229a80f42f 100644 --- a/packages/test-utils/package.json +++ b/packages/test-utils/package.json @@ -1,7 +1,7 @@ { "name": "koishi-test-utils", "description": "Test utilities for Koishi", - "version": "3.2.2", + "version": "3.3.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -33,17 +33,17 @@ "utilities" ], "peerDependencies": { - "jest": "^25.2.1" + "jest": "^25.3.0" }, "devDependencies": { "@types/debug": "^4.1.5", - "@types/jest": "^25.1.4" + "@types/jest": "^25.2.1" }, "dependencies": { "axios": "^0.19.2", "debug": "^4.1.1", "get-port": "^5.1.1", - "koishi-core": "^1.11.2", + "koishi-core": "^1.12.0", "koishi-utils": "^1.0.4" } } diff --git a/packages/test-utils/src/session.ts b/packages/test-utils/src/session.ts index 1c9f94658a..494e206919 100644 --- a/packages/test-utils/src/session.ts +++ b/packages/test-utils/src/session.ts @@ -7,6 +7,12 @@ export const createMessageMeta = (type: ContextType, message: string, userId: nu messageType: type === 'user' ? 'private' : type, message, userId, + sender: { + sex: 'unknown', + age: 0, + userId, + nickname: '' + userId, + }, }) export class Session {