Skip to content

Commit

Permalink
feat(utils): support find options for segment.from()
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 9, 2021
1 parent 91247e1 commit 6c59d86
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
16 changes: 6 additions & 10 deletions packages/koishi-core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, noop } from 'koishi-utils'
import { contain, observe, Logger, defineProperty, Random, template, remove, noop, segment } from 'koishi-utils'
import { Argv } from './parser'
import { Middleware, NextFunction } from './context'
import { App } from './app'
Expand Down Expand Up @@ -135,16 +135,12 @@ export class Session<
}

private async _preprocess() {
let capture: RegExpMatchArray
let node: segment.Parsed
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
}
// eslint-disable-next-line no-cond-assign
if (node = segment.from(content, { type: 'quote', caret: true })) {
content = content.slice(node.capture[0].length).trimStart()
this.quote = await this.bot.getMessage(node.data.channelId || this.channelId, node.data.id).catch(noop)
}
return content
}
Expand Down
11 changes: 9 additions & 2 deletions packages/koishi-utils/src/segment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ export namespace segment {
return codes.map(code => segment(code.type, code.data)).join('')
}

export function from(source: string, typeRegExp = '\\w+'): segment.Parsed {
const capture = new RegExp(`\\[CQ:(${typeRegExp})((,\\w+=[^,\\]]*)*)\\]`).exec(source)
export interface FindOptions {
type?: string
caret?: boolean
}

export function from(source: string, options: FindOptions = {}): segment.Parsed {
let regExpSource = `\\[CQ:(${options.type || '\\w+'})((,\\w+=[^,\\]]*)*)\\]`
if (options.caret) regExpSource = '^' + regExpSource
const capture = new RegExp(regExpSource).exec(source)
if (!capture) return null
const [, type, attrs] = capture
const data: Record<string, string> = {}
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-image-search/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ export function apply(ctx: Context, config: Config = {}) {
const id = session.channelId
if (pendings.has(id)) return '存在正在进行的查询,请稍后再试。'

const code = segment.from(session.content, 'image')
const code = segment.from(session.content, { type: 'image' })
if (code && code.data.url) {
pendings.add(id)
return searchUrl(session, code.data.url, callback)
}

const dispose = session.middleware(({ content }, next) => {
dispose()
const code = segment.from(content, 'image')
const code = segment.from(content, { type: 'image' })
if (!code || !code.data.url) return next()
return searchUrl(session, code.data.url, callback)
})
Expand Down

0 comments on commit 6c59d86

Please sign in to comment.