diff --git a/packages/adapter-cqhttp/package.json b/packages/adapter-cqhttp/package.json index 760f52d1cb..0444070612 100644 --- a/packages/adapter-cqhttp/package.json +++ b/packages/adapter-cqhttp/package.json @@ -1,7 +1,7 @@ { "name": "koishi-adapter-cqhttp", "description": "CQHTTP adapter for Koishi", - "version": "1.0.5", + "version": "1.0.6", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -31,7 +31,7 @@ "koishi" ], "peerDependencies": { - "koishi-core": "^2.3.1" + "koishi-core": "^2.3.2" }, "devDependencies": { "@types/ms": "^0.7.31", diff --git a/packages/adapter-cqhttp/src/api.ts b/packages/adapter-cqhttp/src/api.ts index 93ecbe14a4..c0bbe56fa0 100644 --- a/packages/adapter-cqhttp/src/api.ts +++ b/packages/adapter-cqhttp/src/api.ts @@ -158,6 +158,8 @@ declare module 'koishi-core/dist/server' { setRestart(cleanLog?: boolean, cleanCache?: boolean, cleanEvent?: boolean): Promise setGroupName(groupId: number, name: string): Promise setGroupNameAsync(groupId: number, name: string): Promise + setGroupPortrait(groupId: number, file: string, cache?: boolean): Promise + setGroupPortraitAsync(groupId: number, file: string, cache?: boolean): Promise getGroupMsg(messageId: number): Promise getForwardMsg(messageId: number): Promise sendGroupForwardMsg(groupId: number, messages: readonly CQNode[]): Promise @@ -412,7 +414,8 @@ interface CQNode { } } -defineAsync('set_group_name', 'group_id', 'name') +defineAsync('set_group_name', 'group_id', 'group_name') +defineAsync('set_group_portrait', 'group_id', 'file', 'cache') export function toVersion(data: VersionInfo) { const { coolqEdition, pluginVersion, goCqhttp, version } = data diff --git a/packages/adapter-tomon/package.json b/packages/adapter-tomon/package.json index 59409097c5..98e4656fe6 100644 --- a/packages/adapter-tomon/package.json +++ b/packages/adapter-tomon/package.json @@ -29,7 +29,7 @@ "koishi" ], "peerDependencies": { - "koishi-core": "^2.3.1" + "koishi-core": "^2.3.2" }, "devDependencies": { "koishi-test-utils": "^5.0.2" diff --git a/packages/koishi-core/package.json b/packages/koishi-core/package.json index d576cb95a3..292392bd4a 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": "2.3.1", + "version": "2.3.2", "main": "dist/index.js", "typings": "dist/index.d.ts", "engines": { diff --git a/packages/koishi-core/src/context.ts b/packages/koishi-core/src/context.ts index 4b50aa82c7..26b3336e35 100644 --- a/packages/koishi-core/src/context.ts +++ b/packages/koishi-core/src/context.ts @@ -333,6 +333,9 @@ export interface EventMap { 'group_recall'(session: RawSession<'notice'>): void 'friend_recall'(session: RawSession<'notice'>): void 'notify'(session: RawSession<'notice'>): void + 'notify/poke'(session: RawSession<'notice'>): void + 'notify/lucky_king'(session: RawSession<'notice'>): void + 'notify/honor'(session: RawSession<'notice'>): void 'request/friend'(session: RawSession<'request'>): void 'request/group/add'(session: RawSession<'request'>): void 'request/group/invite'(session: RawSession<'request'>): void diff --git a/packages/koishi-test-utils/package.json b/packages/koishi-test-utils/package.json index cd3654320b..d026469c08 100644 --- a/packages/koishi-test-utils/package.json +++ b/packages/koishi-test-utils/package.json @@ -40,7 +40,7 @@ "dependencies": { "chai": "^4.2.0", "chai-as-promised": "^7.1.1", - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { diff --git a/packages/koishi/ecosystem.json b/packages/koishi/ecosystem.json index 1a3e9de71d..0bd902599b 100644 --- a/packages/koishi/ecosystem.json +++ b/packages/koishi/ecosystem.json @@ -1,6 +1,6 @@ { "koishi-adapter-cqhttp": { - "version": "1.0.5", + "version": "1.0.6", "description": "CQHTTP adapter for Koishi" }, "koishi-adapter-tomon": { @@ -8,11 +8,11 @@ "description": "Tomon adapter for Koishi" }, "koishi-plugin-chess": { - "version": "2.0.0-beta.11", + "version": "2.0.0-beta.12", "description": "Chess Plugin for Koishi" }, "koishi-plugin-common": { - "version": "3.0.2", + "version": "3.0.3", "description": "Common plugins for Koishi" }, "koishi-plugin-dice": { @@ -20,7 +20,7 @@ "description": "COC / DND Tools for Koishi" }, "koishi-plugin-eval": { - "version": "2.0.2", + "version": "2.0.3", "description": "Execute JavaScript in Koishi" }, "koishi-plugin-eval-addons": { @@ -28,7 +28,7 @@ "description": "Execute JavaScript in Koishi" }, "koishi-plugin-github": { - "version": "2.1.1", + "version": "2.1.2", "description": "GitHub webhook plugin for Koishi" }, "koishi-plugin-image-search": { @@ -36,14 +36,14 @@ "description": "Image searching plugin for Koishi" }, "koishi-plugin-mongo": { - "version": "1.0.4", + "version": "1.1.0", "description": "MongoDB support for Koishi" }, "koishi-plugin-monitor": { - "version": "1.0.0-beta.15" + "version": "1.0.0-beta.16" }, "koishi-plugin-mysql": { - "version": "2.0.1", + "version": "2.0.2", "description": "MySQL support for Koishi" }, "koishi-plugin-puppeteer": { @@ -51,19 +51,19 @@ "description": "Take Screenshots in Koishi" }, "koishi-plugin-rss": { - "version": "1.0.1", + "version": "1.1.0", "description": "Subscribe RSS Url for Koishi" }, "koishi-plugin-schedule": { - "version": "2.0.3", + "version": "2.0.4", "description": "Schedule plugin for Koishi" }, "koishi-plugin-status": { - "version": "2.0.0-beta.16", + "version": "2.0.0-beta.17", "description": "Show Status of Koishi" }, "koishi-plugin-teach": { - "version": "1.1.1", + "version": "1.2.0", "description": "Teach plugin for Koishi" }, "koishi-plugin-tools": { diff --git a/packages/koishi/package.json b/packages/koishi/package.json index 50aa40ed41..b7123e2ce0 100644 --- a/packages/koishi/package.json +++ b/packages/koishi/package.json @@ -1,7 +1,7 @@ { "name": "koishi", "description": "A QQ bot framework based on CQHTTP", - "version": "2.3.1", + "version": "2.3.2", "main": "dist/index.js", "typings": "dist/index.d.ts", "engines": { @@ -40,9 +40,9 @@ "dependencies": { "cac": "^6.6.1", "kleur": "^4.1.3", - "koishi-adapter-cqhttp": "^1.0.5", - "koishi-core": "^2.3.1", - "koishi-plugin-common": "^3.0.2", + "koishi-adapter-cqhttp": "^1.0.6", + "koishi-core": "^2.3.2", + "koishi-plugin-common": "^3.0.3", "prompts": "^2.3.2" } } diff --git a/packages/plugin-chess/package.json b/packages/plugin-chess/package.json index 69fcd95023..89c83360ef 100644 --- a/packages/plugin-chess/package.json +++ b/packages/plugin-chess/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-chess", "description": "Chess Plugin for Koishi", - "version": "2.0.0-beta.11", + "version": "2.0.0-beta.12", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -33,7 +33,7 @@ "game" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-plugin-puppeteer": "^1.0.1", "koishi-utils": "^3.1.5" } diff --git a/packages/plugin-chess/src/index.ts b/packages/plugin-chess/src/index.ts index d6ed613dda..f4130f0065 100644 --- a/packages/plugin-chess/src/index.ts +++ b/packages/plugin-chess/src/index.ts @@ -1,10 +1,15 @@ -import { Context, Group } from 'koishi-core' +import { Context, extendDatabase, Group } from 'koishi-core' import { isInteger } from 'koishi-utils' import { State, MoveResult, StateData } from './state' +import MysqlDatabase from 'koishi-plugin-mysql/dist/database' import * as go from './go' import * as gomoku from './gomoku' import * as othello from './othello' +extendDatabase('koishi-plugin-mysql', ({ tables }) => { + tables.group.chess = `JSON NULL DEFAULT NULL` +}) + interface Rule { placement?: 'grid' | 'cross' create?: (this: State) => string | void diff --git a/packages/plugin-common/package.json b/packages/plugin-common/package.json index cdd3d3b2f3..734488af41 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": "3.0.2", + "version": "3.0.3", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -31,7 +31,7 @@ "plugin" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { diff --git a/packages/plugin-common/src/admin.ts b/packages/plugin-common/src/admin.ts index 5d47c7bfae..384550d84a 100644 --- a/packages/plugin-common/src/admin.ts +++ b/packages/plugin-common/src/admin.ts @@ -203,7 +203,7 @@ export function apply(ctx: Context) { ctx.command('group.assign [bot]', '受理者账号', { authority: 4 }) .groupFields(['assignee']) .adminGruop(({ session, target }, value) => { - const assignee = value ? +value : session.selfId + const assignee = value ? getTargetId(value) : session.selfId if (!isInteger(assignee) || assignee < 0) return '参数错误。' target.assignee = assignee }) diff --git a/packages/plugin-dice/package.json b/packages/plugin-dice/package.json index cde555ab5c..b658ae3f1e 100644 --- a/packages/plugin-dice/package.json +++ b/packages/plugin-dice/package.json @@ -35,7 +35,7 @@ "dice" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { diff --git a/packages/plugin-eval-addons/package.json b/packages/plugin-eval-addons/package.json index 47b480f9b6..fa17aecbb4 100644 --- a/packages/plugin-eval-addons/package.json +++ b/packages/plugin-eval-addons/package.json @@ -36,8 +36,8 @@ "code" ], "peerDependencies": { - "koishi-core": "^2.3.1", - "koishi-plugin-eval": "^2.0.2", + "koishi-core": "^2.3.2", + "koishi-plugin-eval": "^2.0.3", "koishi-utils": "^3.1.5" }, "dependencies": { diff --git a/packages/plugin-eval/package.json b/packages/plugin-eval/package.json index 4dfe87c936..cf843248ad 100644 --- a/packages/plugin-eval/package.json +++ b/packages/plugin-eval/package.json @@ -1,6 +1,6 @@ { "name": "koishi-plugin-eval", - "version": "2.0.2", + "version": "2.0.3", "description": "Execute JavaScript in Koishi", "main": "dist/index.js", "typings": "dist/index.d.ts", @@ -37,7 +37,7 @@ "code" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { diff --git a/packages/plugin-eval/src/index.ts b/packages/plugin-eval/src/index.ts index 885302c883..4314888d68 100644 --- a/packages/plugin-eval/src/index.ts +++ b/packages/plugin-eval/src/index.ts @@ -25,6 +25,7 @@ declare module 'koishi-core/dist/session' { const defaultConfig: EvalConfig = { prefix: '>', + authority: 2, timeout: 1000, setupFiles: {}, maxLogs: Infinity, @@ -38,7 +39,7 @@ const logger = new Logger('eval') export const name = 'eval' export function apply(ctx: Context, config: Config = {}) { - const { prefix } = config = { ...defaultConfig, ...config } + const { prefix, authority } = config = { ...defaultConfig, ...config } const { app } = ctx const worker = new EvalWorker(app, config) defineProperty(app, 'worker', worker) @@ -59,7 +60,7 @@ export function apply(ctx: Context, config: Config = {}) { .option('slient', '-s 不输出最后的结果') .option('restart', '-r 重启子线程', { authority: 3 }) .before((session) => { - if (!session['_redirected'] && session.$user?.authority < 2) return '权限不足。' + if (!session['_redirected'] && session.$user?.authority < authority) return '权限不足。' }) attachTraps(cmd, config, async ({ session, options, ctxOptions }, expr) => { diff --git a/packages/plugin-eval/src/main.ts b/packages/plugin-eval/src/main.ts index 1d7c486aa8..36076be86a 100644 --- a/packages/plugin-eval/src/main.ts +++ b/packages/plugin-eval/src/main.ts @@ -17,6 +17,7 @@ const logger = new Logger('eval') export interface MainConfig extends FieldOptions { prefix?: string + authority?: number timeout?: number maxLogs?: number resourceLimits?: ResourceLimits diff --git a/packages/plugin-eval/src/worker.ts b/packages/plugin-eval/src/worker.ts index 67f9a6e610..749ab0ba70 100644 --- a/packages/plugin-eval/src/worker.ts +++ b/packages/plugin-eval/src/worker.ts @@ -140,7 +140,7 @@ export class WorkerAPI { let result: any try { result = await vm.run(`{ - const { send, exec, user } = global[Symbol.for("${key}")]; + const { send, exec, user, group } = global[Symbol.for("${key}")]; delete global[Symbol.for("${key}")]; \n${source} }`, { diff --git a/packages/plugin-github/package.json b/packages/plugin-github/package.json index 6204c0fe36..b3a14320ca 100644 --- a/packages/plugin-github/package.json +++ b/packages/plugin-github/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-github", "description": "GitHub webhook plugin for Koishi", - "version": "2.1.1", + "version": "2.1.2", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -36,7 +36,7 @@ "koishi-test-utils": "^5.0.2" }, "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-plugin-puppeteer": "^1.0.1", "koishi-utils": "^3.1.5" }, diff --git a/packages/plugin-github/src/events.ts b/packages/plugin-github/src/events.ts index b385788a07..4cd0925cff 100644 --- a/packages/plugin-github/src/events.ts +++ b/packages/plugin-github/src/events.ts @@ -1,7 +1,8 @@ /* eslint-disable camelcase */ -import { EventNames } from '@octokit/webhooks' -import { GetWebhookPayloadTypeFromEvent } from '@octokit/webhooks/dist-types/generated/get-webhook-payload-type-from-event' +import { EventTypesPayload } from '@octokit/webhooks/dist-types/generated/get-webhook-payload-type-from-event' + +type WebhookEvent = Exclude export interface EventConfig { commitComment?: boolean | { @@ -105,10 +106,10 @@ export interface EventData { } } -type Payload = GetWebhookPayloadTypeFromEvent['payload'] -type EventHandler = (payload: Payload) => EventData +type Payload = EventTypesPayload[T]['payload'] +type EventHandler = (payload: Payload) => EventData -export function addListeners(on: (event: T, handler: EventHandler) => void) { +export function addListeners(on: (event: T, handler: EventHandler) => void) { function formatMarkdown(source: string) { return source .replace(/^```(.*)$/gm, '') diff --git a/packages/plugin-image-search/package.json b/packages/plugin-image-search/package.json index 6c648491a7..c39bc8883a 100644 --- a/packages/plugin-image-search/package.json +++ b/packages/plugin-image-search/package.json @@ -37,7 +37,7 @@ "pixiv" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "dependencies": { diff --git a/packages/plugin-mongo/package.json b/packages/plugin-mongo/package.json index ae37e1381d..859548b1fa 100644 --- a/packages/plugin-mongo/package.json +++ b/packages/plugin-mongo/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-mongo", "description": "MongoDB support for Koishi", - "version": "1.0.4", + "version": "1.1.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -39,7 +39,7 @@ "@types/mongodb": "^3.5.27" }, "peerDependencies": { - "koishi-core": "^2.3.1" + "koishi-core": "^2.3.2" }, "dependencies": { "mongodb": "^3.6.2" diff --git a/packages/plugin-mongo/src/database.ts b/packages/plugin-mongo/src/database.ts index 504e947f95..7440ff4343 100644 --- a/packages/plugin-mongo/src/database.ts +++ b/packages/plugin-mongo/src/database.ts @@ -1,5 +1,5 @@ import { MongoClient, Db, Collection } from 'mongodb' -import { App, User, Group, TableType } from 'koishi-core' +import { App, TableType } from 'koishi-core' export interface Config { username?: string @@ -11,17 +11,13 @@ export interface Config { prefix?: string } -interface Document { - _id: number -} - export default class MongoDatabase { public config: Config public client: MongoClient public db: Db - user: Collection - group: Collection + user: Collection + group: Collection constructor(public app: App, config: Config) { this.config = config diff --git a/packages/plugin-mongo/src/index.ts b/packages/plugin-mongo/src/index.ts index 6bb403afcb..28f90c9dbc 100644 --- a/packages/plugin-mongo/src/index.ts +++ b/packages/plugin-mongo/src/index.ts @@ -13,54 +13,35 @@ extendDatabase(MongoDatabase, { const authority = typeof args[0] === 'number' ? args.shift() as number : 0 const fields = args[0] ? args[0] as any : User.fields if (fields && !fields.length) return {} as any - const data = await this.user.findOne({ _id: userId }) - let fallback: User + const data: Partial = (await this.user.findOne({ _id: userId })) || {} if (authority < 0) return null - if (!data) { - fallback = User.create(userId, authority) - if (authority) { - this.user.updateOne( - { _id: userId }, - { - $set: { authority }, - $setOnInsert: { - id: userId, - flag: 0, - name: `${userId}`, - usage: {}, - timers: {}, - }, - }, - { upsert: true }, - ) + const fallback = User.create(userId, authority) + if (authority && [undefined, null].includes(data.authority)) await this.user.updateOne({ _id: userId }, { $set: { authority } }, { upsert: true }) + if (data.timers) { + if (data.timers._date) { + data.timers.$date = data.timers._date + delete data.timers._date } - } else { - if (data.timers) { - if (data.timers._date) { - data.timers.$date = data.timers._date - delete data.timers._date - } - for (const key in data.timers) { - if (key.includes('_')) { - data.timers[key.replace(/_/gmi, '.')] = data.timers[key] - delete data.timers[key] - } + for (const key in data.timers) { + if (key.includes('_')) { + data.timers[key.replace(/_/gmi, '.')] = data.timers[key] + delete data.timers[key] } } - if (data.usage) { - if (data.usage._date) { - data.usage.$date = data.usage._date - delete data.usage._date - } - for (const key in data.usage) { - if (key.includes('_')) { - data.usage[key.replace(/_/gmi, '.')] = data.usage[key] - delete data.usage[key] - } + } + if (data.usage) { + if (data.usage._date) { + data.usage.$date = data.usage._date + delete data.usage._date + } + for (const key in data.usage) { + if (key.includes('_')) { + data.usage[key.replace(/_/gmi, '.')] = data.usage[key] + delete data.usage[key] } } } - return data || fallback + return { ...fallback, ...data } }, async getUsers(...args) { @@ -133,24 +114,9 @@ extendDatabase(MongoDatabase, { const f = {} for (const field of fields) f[field] = 1 const [data] = await this.group.find({ _id: groupId }).project(f).toArray() - let fallback: Group - if (!data) { - fallback = Group.create(groupId, selfId) - if (selfId && groupId) { - this.group.updateOne( - { _id: groupId }, - { - $set: { assignee: selfId }, - $setOnInsert: { - id: groupId, - flag: 0, - }, - }, - { upsert: true }, - ) - } - } - return data || fallback + const fallback = Group.create(groupId, selfId) + if (!data && selfId && groupId) this.group.insertOne({ _id: groupId, selfId, groupId }) + return { ...fallback, ...data } }, async getAllGroups(...args) { diff --git a/packages/plugin-monitor/package.json b/packages/plugin-monitor/package.json index 5881a78b5e..fa2373b9fa 100644 --- a/packages/plugin-monitor/package.json +++ b/packages/plugin-monitor/package.json @@ -1,6 +1,6 @@ { "name": "koishi-plugin-monitor", - "version": "1.0.0-beta.15", + "version": "1.0.0-beta.16", "main": "dist/index.js", "typings": "dist/index.d.ts", "author": "Shigma <1700011071@pku.edu.cn>", @@ -21,7 +21,7 @@ }, "homepage": "https://github.com/koishijs/koishi#readme", "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" } } diff --git a/packages/plugin-monitor/src/database.ts b/packages/plugin-monitor/src/database.ts index 9996fc749f..15fa43d785 100644 --- a/packages/plugin-monitor/src/database.ts +++ b/packages/plugin-monitor/src/database.ts @@ -75,8 +75,9 @@ extendDatabase('koishi-plugin-mysql', { }, }) -extendDatabase('koishi-plugin-mysql', ({ listFields }) => { +extendDatabase('koishi-plugin-mysql', ({ listFields, tables }) => { listFields.push('subscribe.names') + tables.group.subscribe = `JSON NULL DEFAULT NULL` }) extendDatabase('koishi-plugin-mongo', { diff --git a/packages/plugin-mysql/package.json b/packages/plugin-mysql/package.json index c2e2b606a8..fb085c94a6 100644 --- a/packages/plugin-mysql/package.json +++ b/packages/plugin-mysql/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-mysql", "description": "MySQL support for Koishi", - "version": "2.0.1", + "version": "2.0.2", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -36,7 +36,7 @@ "@types/mysql": "^2.15.15" }, "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "dependencies": { diff --git a/packages/plugin-mysql/src/database.ts b/packages/plugin-mysql/src/database.ts index 6635953b89..0fdde24cb0 100644 --- a/packages/plugin-mysql/src/database.ts +++ b/packages/plugin-mysql/src/database.ts @@ -22,8 +22,8 @@ export default class MysqlDatabase { public pool: Pool public config: Config - escape = escape - escapeId = escapeId + escape: typeof escape + escapeId: typeof escapeId constructor(public app: App, config: Config) { this.config = { @@ -50,7 +50,7 @@ export default class MysqlDatabase { async start() { this.pool = createPool(this.config) const tables = await this.select('information_schema.tables', ['TABLE_NAME'], 'TABLE_SCHEMA = ?', [this.config.database]) - const names = new Set(tables.map(data => data.TABLE_NAME)) + const names = new Set(tables.map(data => data.TABLE_NAME)) for (const name of Object.keys(MysqlDatabase.tables) as TableType[]) { if (names.has(name)) return const table = MysqlDatabase.tables[name] @@ -158,3 +158,6 @@ export default class MysqlDatabase { this.pool.end() } } + +MysqlDatabase.prototype.escape = escape +MysqlDatabase.prototype.escapeId = escapeId diff --git a/packages/plugin-puppeteer/package.json b/packages/plugin-puppeteer/package.json index 0424a53a0c..1d34f57551 100644 --- a/packages/plugin-puppeteer/package.json +++ b/packages/plugin-puppeteer/package.json @@ -39,7 +39,7 @@ "koishi-test-utils": "^5.0.2" }, "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "dependencies": { diff --git a/packages/plugin-rss/package.json b/packages/plugin-rss/package.json index f3a44b4823..3c8143485e 100644 --- a/packages/plugin-rss/package.json +++ b/packages/plugin-rss/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-rss", "description": "Subscribe RSS Url for Koishi", - "version": "1.0.1", + "version": "1.1.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -36,7 +36,7 @@ "rss" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { diff --git a/packages/plugin-rss/src/index.ts b/packages/plugin-rss/src/index.ts index 6b9df56090..73108c133f 100644 --- a/packages/plugin-rss/src/index.ts +++ b/packages/plugin-rss/src/index.ts @@ -1,4 +1,4 @@ -import { Context, Group, extendDatabase } from 'koishi-core' +import { Context, Group, Session, extendDatabase } from 'koishi-core' import { Logger, Time } from 'koishi-utils' import MysqlDatabase from 'koishi-plugin-mysql/dist/database' import RssFeedEmitter from 'rss-feed-emitter' @@ -13,8 +13,9 @@ Group.extend(() => ({ rss: [], })) -extendDatabase('koishi-plugin-mysql', ({ listFields }) => { +extendDatabase('koishi-plugin-mysql', ({ listFields, tables }) => { listFields.push('group.rss') + tables.group.rss = `TEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'` }) export interface Config { @@ -36,8 +37,9 @@ export function apply(ctx: Context, config: Config = {}) { if (url in feedMap) { feedMap[url].add(groupId) } else { - feedMap[url] = new Set() + feedMap[url] = new Set([groupId]) feeder.add({ url, refresh }) + logger.debug('subscribe', url) } } @@ -46,6 +48,7 @@ export function apply(ctx: Context, config: Config = {}) { if (!feedMap[url].size) { delete feedMap[url] feeder.remove(url) + logger.debug('unsubscribe', url) } } @@ -62,6 +65,7 @@ export function apply(ctx: Context, config: Config = {}) { } feeder.on('new-item', async (payload) => { + logger.debug('receive', payload.title) const source = payload.meta.link.toLowerCase() if (!feedMap[source]) return const message = `${payload.meta.title} (${payload.author})\n${payload.title}` @@ -70,13 +74,17 @@ export function apply(ctx: Context, config: Config = {}) { }) const validators: Record> = {} - function validate(url: string) { - if (validators[url]) return validators[url] + async function validate(url: string, session: Session) { + if (validators[url]) { + await session.$send('正在尝试连接……') + return validators[url] + } + let timer: NodeJS.Timeout const feeder = new RssFeedEmitter({ userAgent }) return validators[url] = new Promise((resolve, reject) => { // rss-feed-emitter's typings suck - feeder.add({ url, refresh: Number.MAX_SAFE_INTEGER }) + feeder.add({ url, refresh: 1 << 30 }) feeder.on('new-item', resolve) feeder.on('error', reject) timer = setTimeout(() => reject(new Error('connect timeout')), timeout) @@ -87,14 +95,15 @@ export function apply(ctx: Context, config: Config = {}) { }) } - ctx.group().command('rss ', 'Subscribe a rss url') + ctx.group().command('rss ', '订阅 RSS 链接') .groupFields(['rss', 'id']) .option('remove', '-r, --remove 取消订阅') - .action(async ({ session: { $group }, options }, url) => { + .action(async ({ session, options }, url) => { url = url.toLowerCase() - + const { $group } = session const index = $group.rss.indexOf(url) - if (!options.remove) { + + if (options.remove) { if (index < 0) return '未订阅此链接。' $group.rss.splice(index, 1) unsubscribe(url, $group.id) @@ -102,7 +111,7 @@ export function apply(ctx: Context, config: Config = {}) { } if (index >= 0) return '已订阅此链接。' - return validate(url).then(() => { + return validate(url, session).then(() => { subscribe(url, $group.id) if (!$group.rss.includes(url)) { $group.rss.push(url) diff --git a/packages/plugin-schedule/package.json b/packages/plugin-schedule/package.json index 920384bc95..c367e82eb2 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": "2.0.3", + "version": "2.0.4", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -34,12 +34,12 @@ "task" ], "devDependencies": { - "koishi-plugin-mongo": "^1.0.4", - "koishi-plugin-mysql": "^2.0.1", + "koishi-plugin-mongo": "^1.1.0", + "koishi-plugin-mysql": "^2.0.2", "koishi-test-utils": "^5.0.2" }, "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" } } diff --git a/packages/plugin-schedule/src/database.ts b/packages/plugin-schedule/src/database.ts index 69802f95d3..379d65625c 100644 --- a/packages/plugin-schedule/src/database.ts +++ b/packages/plugin-schedule/src/database.ts @@ -24,6 +24,19 @@ export interface Schedule { session: Session } +extendDatabase('koishi-plugin-mysql', (Database) => { + Object.assign(Database.tables.schedule = [ + 'PRIMARY KEY (`id`) USING BTREE', + ], { + id: `INT(10) UNSIGNED NOT NULL AUTO_INCREMENT`, + assignee: `BIGINT(20) NOT NULL DEFAULT '0'`, + time: `TIMESTAMP NULL DEFAULT NULL`, + interval: `BIGINT(20) UNSIGNED NOT NULL DEFAULT '0'`, + command: `MEDIUMTEXT NOT NULL COLLATE 'utf8mb4_general_ci'`, + session: `JSON NOT NULL`, + }) +}) + extendDatabase('koishi-plugin-mysql', { createSchedule(time, interval, command, session) { return this.create('schedule', { time, assignee: session.selfId, interval, command, session }) diff --git a/packages/plugin-status/package.json b/packages/plugin-status/package.json index ab76e4c5a5..9d38649118 100644 --- a/packages/plugin-status/package.json +++ b/packages/plugin-status/package.json @@ -1,7 +1,7 @@ { "name": "koishi-plugin-status", "description": "Show Status of Koishi", - "version": "2.0.0-beta.16", + "version": "2.0.0-beta.17", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ @@ -32,12 +32,12 @@ "status" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { - "koishi-plugin-mongo": "^1.0.4", - "koishi-plugin-mysql": "^2.0.1", + "koishi-plugin-mongo": "^1.1.0", + "koishi-plugin-mysql": "^2.0.2", "koishi-test-utils": "^5.0.2" } } diff --git a/packages/plugin-status/src/index.ts b/packages/plugin-status/src/index.ts index adaea72b97..9ee808e6e4 100644 --- a/packages/plugin-status/src/index.ts +++ b/packages/plugin-status/src/index.ts @@ -5,12 +5,6 @@ import { ActiveData } from './database' export * from './database' -declare module 'koishi-core/dist/context' { - interface EventMap { - 'status'(status: Status, output: string[]): void - } -} - declare module 'koishi-core/dist/server' { interface BotOptions { label?: string @@ -23,6 +17,7 @@ declare module 'koishi-core/dist/server' { export interface Config { refresh?: number + output?: (status: Status) => string } let usage = getCpuUsage() @@ -93,11 +88,34 @@ export function extendStatus(callback: StatusCallback, local = false) { const startTime = Date.now() +const defaultConfig: Config = { + refresh: Time.minute, + output({ bots, cpu, memory, startTime, activeUsers, activeGroups }) { + const output = bots + .filter(bot => bot.code !== BotStatusCode.BOT_IDLE) + .map(({ label, selfId, code, rate }) => { + return `${label || selfId}:${code ? '无法连接' : `工作中(${rate}/min)`}` + }) + + output.push('==========') + + output.push( + `活跃用户数量:${activeUsers}`, + `活跃群数量:${activeGroups}`, + `启动时间:${new Date(startTime).toLocaleString('zh-CN', { hour12: false })}`, + `CPU 使用率:${(cpu.app * 100).toFixed()}% / ${(cpu.total * 100).toFixed()}%`, + `内存使用率:${(memory.app * 100).toFixed()}% / ${(memory.total * 100).toFixed()}%`, + ) + + return output.join('\n') + }, +} + export const name = 'status' export function apply(ctx: Context, config: Config = {}) { const app = ctx.app - const { refresh = Time.minute } = config + const { refresh, output } = { ...defaultConfig, ...config } app.on('before-command', ({ session }) => { session.$user['lastCall'] = new Date() @@ -148,27 +166,7 @@ export function apply(ctx: Context, config: Config = {}) { .shortcut('运行情况', { prefix: true }) .shortcut('运行状态', { prefix: true }) .action(async () => { - const status = await getStatus() - const { bots, cpu, memory, startTime, activeUsers, activeGroups } = status - - const output = bots - .filter(bot => bot.code !== BotStatusCode.BOT_IDLE) - .map(({ label, selfId, code, rate }) => { - return `${label || selfId}:${code ? '无法连接' : `工作中(${rate}/min)`}` - }) - - output.push('==========') - - output.push( - `活跃用户数量:${activeUsers}`, - `活跃群数量:${activeGroups}`, - `启动时间:${new Date(startTime).toLocaleString('zh-CN', { hour12: false })}`, - `CPU 使用率:${(cpu.app * 100).toFixed()}% / ${(cpu.total * 100).toFixed()}%`, - `内存使用率:${(memory.app * 100).toFixed()}% / ${(memory.total * 100).toFixed()}%`, - ) - - ctx.emit('status', status, output) - return output.join('\n') + return output(await getStatus()) }) async function _getStatus(extend: boolean) { diff --git a/packages/plugin-teach/package.json b/packages/plugin-teach/package.json index b5714a6c24..6b85da3fb4 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": "1.1.1", + "version": "1.2.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "engines": { @@ -38,12 +38,12 @@ "conversation" ], "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "devDependencies": { - "koishi-plugin-mongo": "^1.0.4", - "koishi-plugin-mysql": "^2.0.1", + "koishi-plugin-mongo": "^1.1.0", + "koishi-plugin-mysql": "^2.0.2", "koishi-test-utils": "^5.0.2" }, "dependencies": { diff --git a/packages/plugin-teach/src/database/mysql.ts b/packages/plugin-teach/src/database/mysql.ts index 031085910d..9b8c2346f2 100644 --- a/packages/plugin-teach/src/database/mysql.ts +++ b/packages/plugin-teach/src/database/mysql.ts @@ -1,8 +1,7 @@ import { Context, extendDatabase } from 'koishi-core' import { clone, defineProperty, Observed, pick } from 'koishi-utils' import { Dialogue, equal, DialogueTest } from '../utils' -import { escape } from 'mysql' -import MysqlDatabase from 'koishi-plugin-mysql/dist/database' +import type MysqlDatabase from 'koishi-plugin-mysql/dist/database' declare module 'koishi-core/dist/context' { interface EventMap { @@ -93,8 +92,26 @@ extendDatabase('koishi-plugin-mysql', { }, }) -extendDatabase('koishi-plugin-mysql', ({ listFields }) => { +extendDatabase('koishi-plugin-mysql', ({ listFields, tables }) => { listFields.push('dialogue.groups', 'dialogue.predecessors') + + Object.assign(tables.dialogue = [ + 'PRIMARY KEY (`id`) USING BTREE', + ], { + id: `INT(11) UNSIGNED NOT NULL AUTO_INCREMENT`, + flag: `INT(10) UNSIGNED NOT NULL DEFAULT '0'`, + probS: `DECIMAL(4,3) UNSIGNED NOT NULL DEFAULT '1.000'`, + probA: `DECIMAL(4,3) UNSIGNED NOT NULL DEFAULT '0.000'`, + startTime: `INT(10) NOT NULL DEFAULT '0'`, + endTime: `INT(10) NOT NULL DEFAULT '0'`, + groups: `TINYTEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'`, + original: `TINYTEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'`, + question: `TINYTEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'`, + answer: `TEXT(65535) NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'`, + predecessors: `TINYTEXT NULL DEFAULT NULL COLLATE 'utf8mb4_general_ci'`, + successorTimeout: `INT(10) UNSIGNED NOT NULL DEFAULT '0'`, + writer: `BIGINT(20) UNSIGNED NOT NULL DEFAULT '0'`, + }) }) export default function apply(ctx: Context, config: Dialogue.Config) { @@ -127,6 +144,8 @@ export default function apply(ctx: Context, config: Dialogue.Config) { }) ctx.on('dialogue/mysql', ({ regexp, answer, question, original }, conditionals) => { + const { escape } = require('koishi-plugin-mysql/dist/database').default.prototype as MysqlDatabase + if (regexp) { if (answer !== undefined) conditionals.push('`answer` REGEXP ' + escape(answer)) if (question !== undefined) conditionals.push('`question` REGEXP ' + escape(original)) diff --git a/packages/plugin-teach/src/internal.ts b/packages/plugin-teach/src/internal.ts index 3dabeac6bc..16710a6759 100644 --- a/packages/plugin-teach/src/internal.ts +++ b/packages/plugin-teach/src/internal.ts @@ -59,11 +59,13 @@ export default function apply(ctx: Context, config: Dialogue.Config) { return Message.Teach.ProhibitedCQCode } - const { unprefixed, prefixed, appellative } = config._stripQuestion(options.question) + const { unprefixed, prefixed, appellative } = options.regexp + ? { unprefixed: question, prefixed: question, appellative: false } + : config._stripQuestion(question) argv.appellative = appellative Object.defineProperty(options, '_original', { value: prefixed }) if (unprefixed) { - options.original = options.question + options.original = question options.question = unprefixed } else { delete options.question diff --git a/packages/plugin-teach/src/receiver.ts b/packages/plugin-teach/src/receiver.ts index 64f588c73f..2cf05b4dd1 100644 --- a/packages/plugin-teach/src/receiver.ts +++ b/packages/plugin-teach/src/receiver.ts @@ -268,11 +268,20 @@ export default function (ctx: Context, config: Dialogue.Config) { } ctx.group().middleware(async (session, next) => { - return session.$execute({ - command: 'dialogue', - args: [session.message], - next, - }) + return triggerDialogue(ctx, session, next) + }) + + ctx.on('notify/poke', (session) => { + if (session.targetId !== session.selfId) return + session.message = 'hook:poke' + triggerDialogue(ctx, session) + }) + + ctx.on('notify/honor', async (session) => { + const { assignee } = await session.$observeGroup(['assignee']) + if (assignee !== session.selfId) return + session.message = 'hook:' + session.honorType + triggerDialogue(ctx, session) }) ctx.on('dialogue/receive', ({ session, test }) => { @@ -307,7 +316,7 @@ export default function (ctx: Context, config: Dialogue.Config) { } function prepareSource(source: string) { - return CQCode.stringifyAll(CQCode.parseAll(source || '').map((code, index, arr) => { + return CQCode.stringifyAll(CQCode.parseAll(source).map((code, index, arr) => { if (typeof code !== 'string') return code let message = simplify(CQCode.unescape('' + code)) .toLowerCase() diff --git a/packages/plugin-tools/package.json b/packages/plugin-tools/package.json index 43027c0fa2..9ab5279477 100644 --- a/packages/plugin-tools/package.json +++ b/packages/plugin-tools/package.json @@ -25,7 +25,7 @@ "@types/qrcode": "^1.3.5" }, "peerDependencies": { - "koishi-core": "^2.3.1", + "koishi-core": "^2.3.2", "koishi-utils": "^3.1.5" }, "dependencies": {