diff --git a/packages/koishi-core/src/app.ts b/packages/koishi-core/src/app.ts index 5b00caa005..d90a9bd328 100644 --- a/packages/koishi-core/src/app.ts +++ b/packages/koishi-core/src/app.ts @@ -1,4 +1,4 @@ -import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray, noop, template, trimSlash, merge } from 'koishi-utils' +import { simplify, defineProperty, Time, Observed, coerce, escapeRegExp, makeArray, template, trimSlash, merge } from 'koishi-utils' import { Context, Middleware, NextFunction, Plugin } from './context' import { Argv } from './parser' import { BotOptions, Adapter, createBots } from './adapter' @@ -198,19 +198,10 @@ export class App extends Context { } private async _process(session: Session, next: NextFunction) { - let content = this.options.processMessage(session.content) - let capture: RegExpMatchArray let atSelf = false, appel = false, prefix: string = null const pattern = /^\[CQ:(\w+)((,\w+=[^,\]]*)*)\]/ - if ((capture = content.match(pattern)) && capture[1] === 'quote') { - content = content.slice(capture[0].length).trimStart() - for (const str of capture[2].slice(1).split(',')) { - if (!str.startsWith('id=')) continue - session.quote = await session.bot.getMessage(session.channelId, str.slice(3)).catch(noop) - break - } - } + let content = await session.preprocess() // strip prefix if (session.subtype !== 'private' && (capture = content.match(pattern)) && capture[1] === 'at' && capture[2].includes('id=' + session.selfId)) { diff --git a/packages/koishi-core/src/session.ts b/packages/koishi-core/src/session.ts index cf3ecb0c01..7e8101be44 100644 --- a/packages/koishi-core/src/session.ts +++ b/packages/koishi-core/src/session.ts @@ -2,7 +2,7 @@ import LruCache from 'lru-cache' import { distance } from 'fastest-levenshtein' import { User, Channel, TableType, Tables } from './database' import { Command } from './command' -import { contain, observe, Logger, defineProperty, Random, template, remove } from 'koishi-utils' +import { contain, observe, Logger, defineProperty, Random, template, remove, noop } from 'koishi-utils' import { Argv } from './parser' import { Middleware, NextFunction } from './context' import { App } from './app' @@ -109,6 +109,7 @@ export class Session< private _delay?: number private _queued: Promise private _hooks: (() => void)[] + private _promise: Promise static readonly send = Symbol.for('koishi.session.send') @@ -133,6 +134,25 @@ export class Session< })) } + private async _preprocess() { + let capture: RegExpMatchArray + let content = this.app.options.processMessage(this.content) + const pattern = /^\[CQ:(\w+)((,\w+=[^,\]]*)*)\]/ + if ((capture = this.content.match(pattern)) && capture[1] === 'quote') { + content = content.slice(capture[0].length).trimStart() + for (const str of capture[2].slice(1).split(',')) { + if (!str.startsWith('id=')) continue + this.quote = await this.bot.getMessage(this.channelId, str.slice(3)).catch(noop) + break + } + } + return content + } + + async preprocess() { + return this._promise ||= this._preprocess() + } + get username(): string { const defaultName = this.user && this.user['name'] ? this.user['name'] diff --git a/packages/plugin-chat/client/chat.vue b/packages/plugin-chat/client/chat.vue index 388e090e34..e2e90dae81 100644 --- a/packages/plugin-chat/client/chat.vue +++ b/packages/plugin-chat/client/chat.vue @@ -1,16 +1,24 @@