Skip to content

Commit

Permalink
Merge pull request #37 from koishijs/develop
Browse files Browse the repository at this point in the history
v1.12.0
  • Loading branch information
shigma authored Apr 18, 2020
2 parents acaffac + ad4c0fa commit de0ebc0
Show file tree
Hide file tree
Showing 22 changed files with 108 additions and 71 deletions.
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
12 changes: 6 additions & 6 deletions packages/database-level/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/database-memory/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions packages/database-mysql/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down Expand Up @@ -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",
Expand Down
6 changes: 3 additions & 3 deletions packages/database-sqlite/package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions packages/koishi-cli/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": "1.11.2",
"version": "1.12.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -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"
}
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-cli/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}
}
Expand Down
10 changes: 5 additions & 5 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": "1.11.2",
"version": "1.12.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions packages/koishi-core/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 2 additions & 0 deletions packages/koishi-core/src/meta.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -61,6 +62,7 @@ export interface Meta <T extends PostType = PostType> {
$ctxType?: ContextType

// other properties
$app?: App
$argv?: ParsedCommandLine
$parsed?: ParsedMessage

Expand Down
17 changes: 12 additions & 5 deletions packages/koishi-core/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions packages/koishi-core/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand Down Expand Up @@ -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)
}
})
Expand Down
4 changes: 2 additions & 2 deletions packages/koishi-utils/src/cqCode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
type CQCodeData = Record<string, string | number>
type CQCodeData = Record<string, string | number | boolean>

interface CQCode {
type: string
Expand All @@ -25,7 +25,7 @@ namespace CQCode {
.replace(/&amp;/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) {
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-common/package.json
Original file line number Diff line number Diff line change
@@ -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>",
Expand All @@ -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"
}
}
9 changes: 9 additions & 0 deletions packages/plugin-common/src/contextify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 })
Expand Down
9 changes: 9 additions & 0 deletions packages/plugin-common/src/echo.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { MetaTypeMap, Context } from 'koishi-core'
import { CQCode } from 'koishi-utils'

export default function (ctx: Context) {
ctx.command('echo <message...>', '向多个上下文发送广播', { authority: 2 })
.option('-a, --anonymous', '匿名发送消息')
.option('-A, --force-anonymous', '匿名发送消息')
.option('-u, --user <id>', '指定信息发送的目标 QQ 号', { isString: true, authority: 4 })
.option('-g, --group <id>', '指定信息发送的目标群号', { isString: true, authority: 4 })
.option('-d, --discuss <id>', '指定信息发送的目标讨论组号', { isString: true, authority: 4 })
Expand All @@ -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)),
Expand Down
Loading

0 comments on commit de0ebc0

Please sign in to comment.